ListView、GridView更新显示数据问题,我设置了一个按钮,想点下按钮就更新ListView显示的数据,现在我这个代码,点击了按钮之后,必须要滑动ListView才能将已显示的内容改变。如何实现我点了按钮之后就直接更改显示的内容呢?
以下是主要代码,我自定义了一个MyAdapter类,继承了BaseAdapter类,想要达到的效果如下两图所示:
想要达到的效果是我点击了改变ListView按钮,就改变页面所显示的内容,希望大神能讲讲,困惑太久了,谢谢各位
package com.example.baseadapterdemo;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import com.example.bean.*;
public class MainActivity extends Activity {
private List<ItemBean> itemBeanList;
private ListView listview;
private MyAdapter myAdapter;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
itemBeanList = new ArrayList<ItemBean>();
for(int i=0;i<10;i++){
itemBeanList.add(new ItemBean(
R.drawable.ic_launcher,
"标题" + i,
"内容:这是新闻页面" + i
));
}
myAdapter = new MyAdapter(this, itemBeanList);
listview.setAdapter(myAdapter);
handler = new Handler(){
public void handleMessage(android.os.Message msg) {
myAdapter = new MyAdapter(MainActivity.this, itemBeanList);
myAdapter.notifyDataSetChanged();
listview.setAdapter(myAdapter);
listview.invalidate();
};
};
}
public void ChangeList(View v){
itemBeanList.clear();
for(int i=0;i<10;i++){
itemBeanList.add(new ItemBean(
R.drawable.ic_launcher,
"更新的标题" + i,
"内容:这是新闻页面" + i
));
}
new Thread(){
public void run() {
handler.sendEmptyMessage(1);
};
};
}
}
_LQH
相关分类