简体   繁体   English

在另一个Activity中更新后刷新ListView

[英]Refresh ListView after updating in another Activity

I have two simple Activities - Activity1: ListView from an Array Activity2: EditText for editing the clicked row in Activity1 我有两个简单的活动 - 活动1:来自Array Activity2的ListView:EditText,用于编辑Activity1中单击的行

When I edit the value in Activity2 and returning to Activity1, the ListView doesn't reload the new value. 当我在Activity2中编辑值并返回到Activity1时,ListView不会重新加载新值。

I want to refresh the ListView when I return from Activity2 or resume Activity1 or something that will update the list. 我想从Activity2返回或恢复Activity1或更新列表的东西时刷新ListView。

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. loadArrays()只是从SharedPreferences转换为String Array的方法。 Activity2 saves the new data in SharedPreferences and than Activity1 can read it (with the new data). Activity2将新数据保存在SharedPreferences中,而Activity1可以读取它(使用新数据)。 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. 如果我返回“主要活动”(它不是Activity1)而不是返回到Activity1 - 会显示新数据,但我希望当我立即从Activity2返回时,这些数据会被更新。

loadArrays() method: pastebin.com/MHwNC0jK loadArrays()方法:pastebin.com/MHwNC0jK

Thanking you in advance! 提前感谢你!

On clicking the item in your first Activity, start your second Activity with startActivityForResult() 单击第一个Activity中的项目时,使用startActivityForResult()启动第二个Activity

And then in Second Activity, after entering in EditText , probably there is a button. 然后在第二个活动中,进入EditText ,可能有一个按钮。 And in onClick of that button call, 而在onClick该按钮调用的,

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. 现在您回到第一个Activity,在这里您必须实现onActivityResult()回调。 You can extract data from that intent's extras and set that respective item in your array and call notifyDataSetChanged() . 您可以从该intent的附加内容中提取数据,并在数组中设置相应的项并调用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 如果您想了解有关如何使用startActivityForResult()的更多信息,请尝试此链接 - http://manisivapuram.blogspot.in/2011/06/how-to-use-startactivityforresult.html

1) Get reference ListView 1)获取参考ListView

mListView = (ListView)findViewById(R.id.auto_listview);

2) Create adapter One more time with changed values 2)创建适配器再次更改值

MyAdapter myAdapter = new MyAdapter(getApplicationContext(), 
                                    R.layout.locations_list_item_layout,dataArray;
mListView.setAdapter(myAdapter);

setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> adapterView,
                        View view, int i, long l) {

myAdapter = new MyAdapter(getApplicationContext(), 
    //pass changed values vlues array                               R.layout.locations_list_item_layout,dataArray;
mListView.setAdapter(myAdapter);

}

                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });

将fulllist声明为Globle Variable并使用静态arraylist。

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

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