在另一个活动更新后刷新的ListView [英] Refresh ListView after updating in another Activity

查看:117
本文介绍了在另一个活动更新后刷新的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个简单的活动 -
活动1:ListView控件从数组
活性2:EditText上为活动1编辑点击行

当我在活性2编辑值并返回到活动1,ListView中不会重新加载新的值。

我要刷新的ListView控件,当我从活性2返回或恢复活性1或东西,将更新列表。

我的code:

 静态ArrayAdapter<串GT; DataAdapter的;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
            //装载值列出阵列
        的String [] [] = fulllist loadArrays();
        的String [] =名单新的String [fulllist.length]
        的for(int i = 0; I< fulllist.length;我++){
            名单[I] = fulllist [I] [1];
            }
            // --------------------------------            DataAdapter的=新ArrayAdapter<串GT;(这一点,
                                android.R.layout.simple_list_item_1,
                                清单);
            setListAdapter(DataAdapter的);
            }
@覆盖
公共无效onResume(){
        super.onResume();
        //装载值列出阵列
        的String [] [] = fulllist loadArrays();
        的String [] =名单新的String [fulllist.length]
        的for(int i = 0; I< fulllist.length;我++){
            名单[I] = fulllist [I] [1];
            }
        // --------------------------------        DataAdapter的=新ArrayAdapter<串GT;(这一点,
                            android.R.layout.simple_list_item_1,
                            清单);
        dataAdapter.notifyDataSetChanged();
}

loadArrays()就是这样从共享preferences到字符串数组转换方法。活性2保存在共享preferences和比活性1的新数据可以读取它(具有新的数据)。
如果我回到主要活动(它不是活性1),比回来活性1 - 新的数据显示,但我想,当我从活性2立即返回该数据将被更新。

loadArrays()方法:pastebin.com/MHwNC0jK

请多关照!


解决方案

在点击你的第一个活动项目,以 startActivityForResult()

然后在第二个活动,在的EditText 进入后,有可能是一个按钮。而在的onClick 该按钮调用,

  intent.putExtra(edittextvalue,findViewById(R.id.edittext).getText()的toString());
的setResult(RESULT_OK,意向);
完();

现在你回到你的第一个活动,在这里你必须执行的onActivityResult()回调。您可以提取从该意图的额外数据和数组中设置相应的项目,并调用 notifyDataSetChanged()

这是最好你应该怎么做。

如果你想在如何使用startActivityForResult更多信息()尝试此链接 - <一个href=\"http://manisivapuram.blogspot.in/2011/06/how-to-use-startactivityforresult.html\">http://manisivapuram.blogspot.in/2011/06/how-to-use-startactivityforresult.html

I have two simple Activities - Activity1: ListView from an Array Activity2: EditText for editing the clicked row in Activity1

When I edit the value in Activity2 and returning to Activity1, the ListView doesn't reload the new value.

I want to refresh the ListView when I return from Activity2 or resume Activity1 or something that will update the list.

My code:

static ArrayAdapter<String> dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            // Loading the values to list array
        String[][] fulllist = loadArrays();
        String[] list = new String[fulllist.length];
        for(int i = 0; i<fulllist.length; i++) {
            list[i] =  fulllist[i][1];
            }
            // --------------------------------

            dataAdapter = new ArrayAdapter<String>(this,
                                android.R.layout.simple_list_item_1,
                                list);
            setListAdapter(dataAdapter);
            }
@Override
public void onResume() {
        super.onResume();
        // Loading the values to list array
        String[][] fulllist = loadArrays();
        String[] list = new String[fulllist.length];
        for(int i = 0; i<fulllist.length; i++) {
            list[i] =  fulllist[i][1];
            }
        // --------------------------------

        dataAdapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1,
                            list);
        dataAdapter.notifyDataSetChanged();
}

loadArrays() is just method that converts from SharedPreferences to String Array. Activity2 saves the new data in SharedPreferences and than Activity1 can read it (with the new data). If I return to the "main activity" (it is not Activity1) and than come back to Activity1 - the new data is shown, but I want this data will be updated when I return from Activity2 immediately.

loadArrays() method: pastebin.com/MHwNC0jK

Thanking you in advance!

解决方案

On clicking the item in your first Activity, start your second Activity with startActivityForResult()

And then in Second Activity, after entering in EditText, probably there is a button. And in onClick of that button call,

intent.putExtra("edittextvalue", findViewById(R.id.edittext).getText().toString());
setResult(RESULT_OK, intent);
finish();

Now you come back to your first Activity and here you have to implement onActivityResult() callback. You can extract data from that intent's extras and set that respective item in your array and call notifyDataSetChanged().

This is ideally how you should be doing it.

If you want more info on how to use startActivityForResult() try this link - http://manisivapuram.blogspot.in/2011/06/how-to-use-startactivityforresult.html

这篇关于在另一个活动更新后刷新的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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