我制作了ListActivity:
public class Remove1 extends ListActivity{
ArrayList<String> listaE = new ArrayList<String>();
ArrayList<String> listaL = new ArrayList<String>();
int resh=0;
InputStream instream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lexo(); //Fill list from file
String[] mStringArray = new String[listaE.size()];
mStringArray = listaE.toArray(mStringArray); //change list to String[] Array
setListAdapter(new ArrayAdapter<String>(Remove1.this,android.R.layout.simple_list_item_1,mStringArray)); //Fill List
}
所以当我点击项目时,项目会从列表中删除,但列表不刷新? 当我完成的时候();然后再次启动活动而不是删除一条线,但我想立即删除..
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
listaE.remove(position); //Remove Line
listaL.remove(position);
write(); // write to file changes
lexo(); // read from file changes and fill lists
}
谢谢!
答案 0 :(得分:0)
<强>尝试强>
public class Remove1 extends ListActivity{
ArrayList<String> listaE = new ArrayList<String>();
ArrayList<String> listaL = new ArrayList<String>();
int resh=0;
InputStream instream;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lexo(); //Fill list from file
String[] mStringArray = new String[listaE.size()];
mStringArray = listaE.toArray(mStringArray); //change list to String[] Array
adapter = new ArrayAdapter<String>(Remove1.this,android.R.layout.simple_list_item_1,mStringArray);
setListAdapter(adapter); //Fill List
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
listaE.remove(position); //Remove Line
listaL.remove(position);
write(); // write to file changes
lexo(); // read from file changes and fill lists
adapter.notifyDataSetChanged();
}
答案 1 :(得分:0)
ListView由适配器支持。你的适配器由String []支持,从这一行:
String[] mStringArray = new String[listaE.size()];
您需要从适配器中删除对象,而不是列表。在你的onListItemClick中执行以下操作:
ArrayAdapter<String> adapter = (ArrayAdapter<String>) l.getAdapter();
String item = (String) listaE.remove(position);
adapter.remove(item);
adapter.notifyDataSetChanged();
如果要从List对象中删除该项,您可以,但不确定您的用例。也许你想保留它,以便你可以在某个时候恢复ListView。
答案 2 :(得分:0)
从mStringArray
中移除与列表中已删除项目相对应的字符串,并在notifyDataSetChanged()
上调用ArrayAdapter
(您必须先将其存储在某个变量中)。