安卓:刷新的ListView中的第一项活动从次活动的回报 [英] Android: Refresh ListView in first activity on return from second activity

查看:139
本文介绍了安卓:刷新的ListView中的第一项活动从次活动的回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表刷新一个问题,当用户关闭一个活动并返回到previous。我看问题是很常见的,但我解决不了的。

我重写onResume方法:

  @覆盖
公共无效onResume(){
    super.onResume();
    populateList();
}
 

populateList()是我的ListView填充用字符串列表的方法:

  arrayAdapter =新CustomArrayAdapter(这一点,R.layout.symbol_item,清​​单);
listView.setAdapter(arrayAdapter);
 

现在的问题是,当次活动被关闭,新项目只是再次添加在ListView,所以我有每个项目增加了一倍。像它不会刷新。

如果我把notifyDataSetChanged()在onResume(),它抛出我的NullPointerException,因为当活动开始第一次没有适配器初始化时活动第一次开始了。

我不知道如何处理这个问题。

 公共类testActivity延伸活动{


    私人诠释ID = 1;
    私人的ListView ListView的;
    私人CustomArrayAdapter arrayAdapter;
    私人的ArrayList<字符串>名单=新的ArrayList<字符串>();
    ArrayList的<项目>链表=新的ArrayList<项目>();

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_test);

    }

    @覆盖
    公共无效onResume(){
        super.onResume();
        populateList();
    }

    私人无效populateList(){
        尝试 {
            链表=新GetAsyncTask(id)的.execute();
        }赶上(InterruptedException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }赶上(为ExecutionException E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }赶上(TimeoutException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        INT大小= objectList.size();
        字符串名称;

        的for(int i = 0; I<大小;我++){
            名= objectList.get(ⅰ).getName();
            list.add(名称);
        }

        arrayAdapter =新CustomArrayAdapter(这一点,R.layout.symbol_item,
                清单);
        listView.setAdapter(arrayAdapter);
    }
}
 

解决方案

以及马上蝙蝠,你可以很容易敲这个客场与执行命令的简单条件语句仅在适配器是不是

 如果(适配器!= NULL){
        adapter.notifyDataSetChanged();
    }
 

不过,这似乎对我来说,在更深的层次,你的code可以重新分解某种程度上更有效率,虽然不一定更多的功能。


做到这一点是这样的:

 私人INT ID = 1;
私人的ListView ListView的;
私人CustomArrayAdapter arrayAdapter;
私人的ArrayList<字符串>名单=新的ArrayList<字符串>();

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_test);
}

@覆盖
公共无效onResume(){
    super.onResume();
    populateList();
}

私人无效populateList(){

    ArrayList的<项目>链表;
    尝试 {
        链表=新GetAsyncTask(id)的.execute();
    }赶上(InterruptedException异常E){
        e.printStackTrace();
    }赶上(为ExecutionException E){
        e.printStackTrace();
    }赶上(TimeoutException异常E){
        e.printStackTrace();
    }

    list.clear();
    的for(int i = 0; I< objectList.size();我++){
        字符串名称= objectList.get(我).getName();
        list.add(名称);
    }
    如果(arrayAdapter == NULL){
        arrayAdapter =新CustomArrayAdapter(这一点,R.layout.symbol_item,清​​单);
        listView.setAdapter(arrayAdapter);
    } 其他 {
        arrayAdapter.notifyDataSetChanged();
    }
}
 

I have a problem with list refresh when user closes one activity and returns to previous. I see that problem is very common but I can't solve it.

I overridden onResume method:

@Override
public void onResume() {
    super.onResume();
    populateList();
}

populateList() is a method where I populate listView with list of Strings:

arrayAdapter = new CustomArrayAdapter(this, R.layout.symbol_item,list);
listView.setAdapter(arrayAdapter);

The problem is that when second activity is closed, new items are just added again in the ListView so I have every item doubled. Like it's not refreshed.

If I put notifyDataSetChanged() in onResume() it throws me nullPointerException because when activity is started first time there is no adapter initialized when activity is first time started.

I'm not sure how to handle this.

public class testActivity extends Activity {


    private int id=1;
    private ListView listView;
    private CustomArrayAdapter arrayAdapter;
    private ArrayList<String> list = new ArrayList<String>();
    ArrayList<Item> objectList = new ArrayList<Item>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

    }

    @Override
    public void onResume() {
        super.onResume();
        populateList();
    }

    private void populateList() {
        try {
            objectList = new GetAsyncTask(id).execute();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int size = objectList.size();
        String name;

        for (int i = 0; i < size; i++) {
            name = objectList.get(i).getName();
            list.add(name);
        }

        arrayAdapter = new CustomArrayAdapter(this, R.layout.symbol_item,
                list);
        listView.setAdapter(arrayAdapter);
    }
}

解决方案

well right off the bat, you could easily knock this away with a simple conditional statement that performs the command only if the adapter isn't null:

    if (adapter != null) {
        adapter.notifyDataSetChanged();
    }

But this seems to me like, at a deeper level, your code could be re-factored somewhat to be more efficient, though not necessarily more functional.


do it like this:

private int id = 1;
private ListView listView;
private CustomArrayAdapter arrayAdapter;
private ArrayList<String> list = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
}

@Override
public void onResume() {
    super.onResume();
    populateList();
}

private void populateList() {

    ArrayList<Item> objectList;
    try {
        objectList = new GetAsyncTask(id).execute();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }

    list.clear();
    for (int i = 0; i <objectList.size(); i++) {
        String name = objectList.get(i).getName();
        list.add(name);
    }
    if (arrayAdapter == null) {
        arrayAdapter = new CustomArrayAdapter(this, R.layout.symbol_item, list);
        listView.setAdapter(arrayAdapter);
    } else {
        arrayAdapter.notifyDataSetChanged();            
    }
}

这篇关于安卓:刷新的ListView中的第一项活动从次活动的回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆