2

我有一个活动堆栈,如:

activity A make something

activity A call Activity B (without finish();)

activity B make something

activity B call activity C

activity B finish();

activity C make something

activity C finish();

我想更新活动 A 的视图,因为活动 C 所做的修改已经更新了数据库,也许我有不同的东西要在活动 A 中列出。

如果我的主要活动是这样的:

public class MainActivity extends ListActivity {
     private pollDataSource datasource;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        datasource = new pollDataSource(this);
        datasource.open();

        Cursor values = datasource.getAllCategorie();

        String[] categorieColumns =
            {
                MySQLiteHelper.COLUMN_NOME   // Contract class constant containing the word column name

            };


            int[] mWordListItems = { R.id.categoria_label };


        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                getApplicationContext(),               // The application's Context object
                R.layout.single_list_item,             // A layout in XML for one row in the ListView
                values,                                // The result from the query
                categorieColumns,                      // A string array of column names in the cursor
                mWordListItems,                        // An integer array of view IDs in the row layout
                0);                                    // Flags (usually none are needed)


        setListAdapter(adapter);
    }


    @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {

          TextView textView = (TextView) v.findViewById(R.id.categoria_label); 
          String text = textView.getText().toString(); 

          Intent myIntent = new Intent(MainActivity.this, domandeDiCategoria.class);
          myIntent.putExtra("categoriaName", text);
          myIntent.putExtra("categoriaId", id);
          datasource.close();
          MainActivity.this.startActivity(myIntent);

        }   




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }




}

我必须写 onResume() 吗?并使其类似于 onCreate?或者我可以使用相同的光标、相同的适配器等来“更新”视图?什么是更好的?

4

2 回答 2

2

1)在A活动中定义

public static int UPDATE_A_ACTIVITY  = 1;

2) startActivityForResult(intentB, A.UPDATE_A_ACTIVITY); // start B from A

3) startActivityForResult(intentC, A.UPDATE_A_ACTIVITY); // start C from B

4)当C活动一切正常时,完成C活动并通知A活动,他应该更新一些东西

在 C 活动中:

 finish();
 setResult(RESULT_OK);

5)调用setResult(RESULT_OK);后将调用A中的下一个方法(在A活动中写这个):

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case UPDATE_A_ACTIVITY:
        if (resultCode == RESULT_OK) {
            // update what u want
        }
            break;
    }
}
于 2012-12-21T14:03:00.603 回答
1

在活动 C中有一个标志。

boolean update = false;

如果活动 C更改了数据库中的某些内容,则使其成为现实。

活动 A

onResume(){

if(update){

//repopulate the list with new data
}
}
于 2012-12-21T13:54:02.263 回答