1

I want to call a listview adapter from another activies after insert record to sqllite db.

I cant call the adapter.notifyDataSetChanged() method. What is the best way to do it.

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();   
}
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
RedLEON
  • 281
  • 2
  • 7
  • 19
  • if there is no instance to that activity, how can you change data set? You can start the activity manually and in the activity's onCreate, update the list items from DB.\ – Li3ro Jul 21 '12 at 11:32

2 Answers2

1

Just

After finish() Current Activity, Call adapter.notifyDataSetChanged() from onActivityResult()in previous Activity (As I assumes your Listview in this previous Activity).

Also start your Current Activity from Previous Activity using startActivityForResult();

Or if you have reference of List Adapter in this Activity, then using that reference call adapter.notifyDataSetChanged(). (But I think its not a good practice)

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Dear @user370305 I have two activity, ItemsActivity and ItemsAddActivity and I'm start the ItemsAddActivity this codes: `private void prepareListener() { listener_open_add_item = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(getBaseContext(), ItemsAddActivity.class); startActivity(intent); } }; } ` I tried adapter.notifyDataSetChanged() after startActivity(intent); line. But it didn't work. Cas the line is run before AddItemActivity finished. For the reason I try to run code after finish(); in that method. – RedLEON Jul 21 '12 at 11:48
  • Now instead of `startActivity(intent)` use `startActivityForResult(intent,0);` and add `onActivityResult()` method in ItemsActivity. in `onActivityResult()` method write `adapter.notifyDataSetChanged();` – user370305 Jul 21 '12 at 11:50
  • I added this codes but it seems onActivityResult method couldn't call. I still trying... – RedLEON Jul 21 '12 at 12:18
  • Debug your code of `onActivityResult()` because if you start an activity with `startActivityForResult()` and when this Activity finish() then method `onActivityResult()` of previous activity definitely called. – user370305 Jul 21 '12 at 12:25
  • I tried but activity has never called. I create a button and after insert a record I clicked the button. adapter.notifyDataSetChanged(); command doesn't run. I think I try to reload ListView component or I try to add inserted record to adapter. Thank you for your useful advices – RedLEON Jul 21 '12 at 20:55
  • onActivityResult called now with this code: `@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { adapter.notifyDataSetChanged(); Log.d("RWatcher", "notify ok"); }` But ListView is still same. It doesn't refresh. :) – RedLEON Jul 22 '12 at 10:11
  • Then just setAdapter() to ListView again in onActivityResult() with modified data list. – user370305 Jul 22 '12 at 15:07
  • It's work now :) `@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { getItemList(); } public void getItemList() { ArrayList itemArray = new ArrayList(); itemArray = db.getAllItems(); adapter = new ItemAdapter(this, R.layout.items_row, itemArray); ilistViewItems.setAdapter(adapter); }` – RedLEON Jul 22 '12 at 20:09
1

The better option would be re-loading your ListView with new data inside onResume() of your Activity where you have ListView. There is no need to call notifyDataSetChanged() from another class as you don't have your List visible in your another Activity.

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

    //fill your Collection with new data
    // setAdapter or notify your adapter
}
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • thx for your suggestion. It's work fine. But I think I re-loading ListView when call the method. Cas notifyDataSetChanged isn't work. – RedLEON Jul 22 '12 at 10:19
  • `@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { adapter.notifyDataSetChanged() } public void getItemList() { ArrayList itemArray = new ArrayList(); itemArray = db.getAllItems(); adapter = new ItemAdapter(this, R.layout.items_row, itemArray); ilistViewItems.setAdapter(adapter); }` – RedLEON Jul 23 '12 at 22:45
  • Well I insisted to notify your adapter inside `onResume()` – Lalit Poptani Jul 24 '12 at 02:43
  • I tried one more time. The result is negative. Listview doesn't refresh. `@Override protected void onResume() { super.onResume(); adapter.notifyDataSetChanged(); Log.d("RWatcher", "notify ok"); }` – RedLEON Jul 25 '12 at 15:45
  • make sure that you add new data to collection before calling `notifyDataSetChanged()` – Lalit Poptani Jul 25 '12 at 17:23
  • I sure new data added before calling. I tested diffirant way. I put a button for refresh data with `notifyDataSetChanged()`. But listview didn't refresh again. Maybe the reason is the listView about database adapter. – RedLEON Jul 25 '12 at 19:16
  • are you populating List from database? – Lalit Poptani Jul 26 '12 at 04:13