简体   繁体   English

在ListActivity Android上刷新列表

[英]Refresh list on ListActivity Android

I made and ListActivity : 我做了和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
    }

So When I click item, item is removed from list, but the list is not refreshing ? 因此,当我单击项目时,项目已从列表中删除,但列表没有刷新? When I finish(); 当我完成的时候(); and start activity again than it removes a line,, but I want to be removed instantly.. 然后重新开始活动,而不是删除一行,但是我想立即删除。

@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
    }

Thank you ! 谢谢 !

Try 尝试

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();
    } 

A ListView is backed by an adapter. ListView由适配器支持。 Your adapter is backed by a String[], from this line: 您的适配器由String []支持,它来自以下行:

String[] mStringArray = new String[listaE.size()];

You need to remove the object from your adapter, not your list. 您需要从适配器而不是列表中删除对象。 In your onListItemClick do this: 在您的onListItemClick中执行以下操作:

ArrayAdapter<String> adapter = (ArrayAdapter<String>) l.getAdapter();
String item = (String) listaE.remove(position);
adapter.remove(item);
adapter.notifyDataSetChanged();

If you want to remove the item from your List object, you can, just not sure of your use case. 如果要从List对象中删除该项目,则可以不确定用例。 Perhaps you'd like to keep it so you can restore the ListView at some point. 也许您想保留它,以便可以在某个时候还原ListView。

mStringArray删除与列表中已删除项目相对应的字符串,并在ArrayAdapter上调用notifyDataSetChanged() (必须先将其存储在某个变量中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM