如何从另一个活动调用ListView适配器进行刷新 - notifyDataSetChanged()

时间:2012-07-21 11:23:54

标签: android sqlite listview call adapter

我想在将记录插入到sqllite db之后从另一个活动中调用listview适配器。

我无法调用adapter.notifyDataSetChanged()方法。什么是最好的方法。

public void onClick(View v) {
    TextView item_name;
    item_name=(TextView) findViewById(R.id.eItemName);         
    Log.d("Insert: ", "Inserting ..");
    db.add_item(new Item(item_name.getText().toString()), getApplicationContext());

    // I want to call the method HERE!!!! 

    finish();   
}

2 个答案:

答案 0 :(得分:1)

只需

finish()当前活动之后,请在之前的活动中从adapter.notifyDataSetChanged()致电onActivityResult()(因为我在上一个活动中假设您的列表视图)。

还可以使用startActivityForResult();

从上一个活动开始当前活动

或者,如果您在此活动中引用了列表适配器,则使用该引用调用adapter.notifyDataSetChanged()。 (但我认为这不是一个好习惯)

答案 1 :(得分:1)

更好的选择是使用您的ListView的ListView内的新数据重新加载onResume()。无需从其他课程拨打notifyDataSetChanged(),因为您的另一个活动中没有您的列表。

@Override
protected void onResume() {
    super.onResume();

    //fill your Collection with new data
    // setAdapter or notify your adapter
}