如何实现listview的刷新? 我是在一个Activity中定义的listview,有一个删除指定条目操作,要求删除后要马上刷新把消失的条目去除了,没有用数据库,用的SD卡存储。问题是:在sd卡上,文件已经删除了,但是,列表不更新。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Adapter.notifyDataSetChanged();或者notifyDataSetInvalidated Adapter.notifyDataSetChanged();这个试过了,好像没有作用 大哥,你要把adapter里面的数据源去年啊,不然怎么删除的掉类!remove掉~~~ 用Adapter.notifyDataSetChanged(),但是前提是你的确定list里的数据发生了变化,才会器作用。 你删除或者添加某一行 之后adapter里的数据同时改变,然后调用Adapter.notifyDataSetChanged()就ok啦 恩 把他去了以后 然后Adapter.notifyDataSetChanged(); SD卡里的文件肯定去掉了,至于数据源的arraylist改变了没有就不好说了,我看了好像是已经没有了 大哥们啊,我能力实在有限了,贴出相关代码,如果有时间,请帮忙看看吧: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LastOne = (TextView)this.findViewById(R.id.lastone); Month = (TextView)this.findViewById(R.id.month); NextOne = (TextView)this.findViewById(R.id.nextone); NewOne = (TextView)this.findViewById(R.id.newone); OpenView = (TextView)this.findViewById(R.id.open); EditView = (TextView)this.findViewById(R.id.edit); DelView = (TextView)this.findViewById(R.id.delete); listview = (ListView)this.findViewById(R.id.listview); MonthDateStr = Just4MonthFormat.format(date); Month.setText(MonthDateStr); /*DiaryListAda = new SimpleAdapter(this, this.getData(), R.layout.listview_xml, new String[]{"index", "title", "content"}, new int[]{R.id.indexinlist, R.id.titleinlist, R.id.contentinlist}); listview.setAdapter(DiaryListAda);*/ this.reloadList(listview, simpleAdapter); listview.setOnItemClickListener(new OnItemClickListener () { //注意此处是OnItemClickListener public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { index = position; //index作为成员变量,比较巧妙 listview.setBackgroundColor(Color.BLACK); //暂时不完善 view.setBackgroundColor(Color.GRAY); FormerViewID = position; } }); NewOne.setOnClickListener(new NewOneListener ()); OpenView.setOnClickListener(new OpenListener ()); EditView.setOnClickListener(new EditListener ()); DelView.setOnClickListener(new DelListener ()); }以上是oncreat下边有reloadlist函数:public void reloadList (ListView listView, SimpleAdapter simpleAdapter) { this.simpleAdapter = simpleAdapter; int id = 0; String path = Environment.getExternalStorageDirectory() + "/" + "Diaries" + "/" + Just4MonthFormat.format(date); HashMap<String, Object> map = new HashMap<String, Object>(); File dir = new File(path); for (File f:dir.listFiles()) { map = new HashMap<String, Object>(); if (f.isFile()) { map.put("index", id+1); map.put("title", f.getName().replace(".txt", "")); map.put("content", getContent(f)); id++; } DataList.add(map); } simpleAdapter = new SimpleAdapter(this, DataList, R.layout.listview_xml, new String[]{"index", "title", "content"}, new int[]{R.id.indexinlist, R.id.titleinlist, R.id.contentinlist}); listView.setAdapter(simpleAdapter); }下边是删除textview的listener:class DelListener implements OnClickListener { public void onClick(View v) { if (index == -1) { Toast.makeText(DiaryBookAct.this, R.string.unchoosed, Toast.LENGTH_SHORT).show(); }else { String filename = DataList.get(index).get("title").toString(); String path_and_filename = Environment.getExternalStorageDirectory() + "/" + "Diaries" + "/" + MonthDateStr + "/" + filename + ".txt"; File file = new File(path_and_filename); if (file.exists()) { file.delete(); Toast.makeText(DiaryBookAct.this, R.string.delsuce, Toast.LENGTH_SHORT).show(); SimpleAdapter sAdapter = (SimpleAdapter)listview.getAdapter(); //listview = new ListView(DiaryBookAct.this); sAdapter.notifyDataSetChanged(); sAdapter.notifyDataSetInvalidated(); reloadList(listview, sAdapter); //System.out.println("not work"); System.out.println(DataList.get(0).get("index").toString()); //listview.invalidate(); } else { Toast.makeText(DiaryBookAct.this, R.string.delfaile, Toast.LENGTH_SHORT).show(); } } } } 这个函数里simpleAdapter的访问有问题:public void reloadList (ListView listView, SimpleAdapter simpleAdapter) {this.simpleAdapter = simpleAdapter;int id = 0;String path = Environment.getExternalStorageDirectory() + "/" + "Diaries" + "/" + Just4MonthFormat.format(date);HashMap<String, Object> map = new HashMap<String, Object>();File dir = new File(path);for (File f:dir.listFiles()) {map = new HashMap<String, Object>();if (f.isFile()) {map.put("index", id+1);map.put("title", f.getName().replace(".txt", ""));map.put("content", getContent(f));id++;}DataList.add(map);}// !!! Here !!! , you may want to write : this.simpleAdapter.simpleAdapter/*Here you change the reference of input parameter*/ = new SimpleAdapter(this, DataList, R.layout.listview_xml, new String[]{"index", "title", "content"}, new int[]{R.id.indexinlist, R.id.titleinlist, R.id.contentinlist});listView.setAdapter(simpleAdapter);} 这个顺序写的也有问题:SimpleAdapter sAdapter = (SimpleAdapter)listview.getAdapter();sAdapter.notifyDataSetChanged();sAdapter.notifyDataSetInvalidated();reloadList(listview, sAdapter);应该改为:SimpleAdapter sAdapter = (SimpleAdapter)listview.getAdapter();reloadList(listview, sAdapter);listview.setAdapter(this.simpleAdapter);sAdapter.notifyDataSetChanged();// No need to call notifyDataSetInvalidated.或者把reloadList返回新的AdapterSimpleAdapter sAdapter = (SimpleAdapter)listview.getAdapter();SimpleAdapter newAdapter = reloadList(listview, sAdapter);listview.setAdapter(newAdapter);sAdapter.notifyDataSetChanged();// No need to call notifyDataSetInvalidated.另外:你的编码风格太差,变量首字母应小写(DataList)。 android求教,急 关于SQLiteTableLockedException 双击DDMS.bat无法运行 关于View的尺寸比Layout大的问题 Android开发环境还要装jdk呢? 如何实时监视sd卡的状态啊,比如SD卡被拔掉 如何在eclipse中新建一个android类 Android老油条请进 Tab显示不出东西 findViewById()问题,求解答... 振动器的触控反馈没有效果,请问应该去哪里找? android ndk在cygwin下编译找不到iconv.h
或者notifyDataSetInvalidated
remove掉~~~
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LastOne = (TextView)this.findViewById(R.id.lastone);
Month = (TextView)this.findViewById(R.id.month);
NextOne = (TextView)this.findViewById(R.id.nextone);
NewOne = (TextView)this.findViewById(R.id.newone);
OpenView = (TextView)this.findViewById(R.id.open);
EditView = (TextView)this.findViewById(R.id.edit);
DelView = (TextView)this.findViewById(R.id.delete);
listview = (ListView)this.findViewById(R.id.listview);
MonthDateStr = Just4MonthFormat.format(date);
Month.setText(MonthDateStr);
/*DiaryListAda = new SimpleAdapter(this, this.getData(), R.layout.listview_xml,
new String[]{"index", "title", "content"},
new int[]{R.id.indexinlist, R.id.titleinlist, R.id.contentinlist});
listview.setAdapter(DiaryListAda);*/
this.reloadList(listview, simpleAdapter);
listview.setOnItemClickListener(new OnItemClickListener () {
//注意此处是OnItemClickListener public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
index = position;
//index作为成员变量,比较巧妙
listview.setBackgroundColor(Color.BLACK); //暂时不完善
view.setBackgroundColor(Color.GRAY);
FormerViewID = position;
}
});
NewOne.setOnClickListener(new NewOneListener ());
OpenView.setOnClickListener(new OpenListener ());
EditView.setOnClickListener(new EditListener ());
DelView.setOnClickListener(new DelListener ());
}
以上是oncreat
下边有reloadlist函数:
public void reloadList (ListView listView, SimpleAdapter simpleAdapter) {
this.simpleAdapter = simpleAdapter;
int id = 0;
String path = Environment.getExternalStorageDirectory() + "/" + "Diaries" + "/" +
Just4MonthFormat.format(date);
HashMap<String, Object> map = new HashMap<String, Object>();
File dir = new File(path);
for (File f:dir.listFiles()) {
map = new HashMap<String, Object>();
if (f.isFile()) {
map.put("index", id+1);
map.put("title", f.getName().replace(".txt", ""));
map.put("content", getContent(f));
id++;
}
DataList.add(map);
}
simpleAdapter = new SimpleAdapter(this, DataList, R.layout.listview_xml,
new String[]{"index", "title", "content"},
new int[]{R.id.indexinlist, R.id.titleinlist, R.id.contentinlist});
listView.setAdapter(simpleAdapter);
}
下边是删除textview的listener:
class DelListener implements OnClickListener { public void onClick(View v) {
if (index == -1) {
Toast.makeText(DiaryBookAct.this, R.string.unchoosed, Toast.LENGTH_SHORT).show();
}else {
String filename = DataList.get(index).get("title").toString();
String path_and_filename = Environment.getExternalStorageDirectory() + "/"
+ "Diaries" + "/" + MonthDateStr + "/" + filename + ".txt";
File file = new File(path_and_filename);
if (file.exists()) {
file.delete();
Toast.makeText(DiaryBookAct.this, R.string.delsuce, Toast.LENGTH_SHORT).show();
SimpleAdapter sAdapter = (SimpleAdapter)listview.getAdapter();
//listview = new ListView(DiaryBookAct.this);
sAdapter.notifyDataSetChanged();
sAdapter.notifyDataSetInvalidated();
reloadList(listview, sAdapter);
//System.out.println("not work");
System.out.println(DataList.get(0).get("index").toString());
//listview.invalidate();
} else {
Toast.makeText(DiaryBookAct.this, R.string.delfaile, Toast.LENGTH_SHORT).show();
}
}
}
}
this.simpleAdapter = simpleAdapter;int id = 0;
String path = Environment.getExternalStorageDirectory() + "/" + "Diaries" + "/" +
Just4MonthFormat.format(date);
HashMap<String, Object> map = new HashMap<String, Object>();
File dir = new File(path);for (File f:dir.listFiles()) {
map = new HashMap<String, Object>();
if (f.isFile()) {
map.put("index", id+1);
map.put("title", f.getName().replace(".txt", ""));
map.put("content", getContent(f));
id++;
}
DataList.add(map);}// !!! Here !!! , you may want to write : this.simpleAdapter.
simpleAdapter/*Here you change the reference of input parameter*/ = new SimpleAdapter(this, DataList, R.layout.listview_xml,
new String[]{"index", "title", "content"},
new int[]{R.id.indexinlist, R.id.titleinlist, R.id.contentinlist});
listView.setAdapter(simpleAdapter);
}
SimpleAdapter sAdapter = (SimpleAdapter)listview.getAdapter();
sAdapter.notifyDataSetChanged();
sAdapter.notifyDataSetInvalidated();
reloadList(listview, sAdapter);应该改为:
SimpleAdapter sAdapter = (SimpleAdapter)listview.getAdapter();
reloadList(listview, sAdapter);
listview.setAdapter(this.simpleAdapter);
sAdapter.notifyDataSetChanged();
// No need to call notifyDataSetInvalidated.或者把reloadList返回新的Adapter
SimpleAdapter sAdapter = (SimpleAdapter)listview.getAdapter();
SimpleAdapter newAdapter = reloadList(listview, sAdapter);
listview.setAdapter(newAdapter);
sAdapter.notifyDataSetChanged();
// No need to call notifyDataSetInvalidated.
另外:你的编码风格太差,变量首字母应小写(DataList)。