I have the following problem. I want to read data from a Database in my ActivityA and send one selected String to ActivityB where it should be displayed in a ListView. I’m using intents to do so an it works fine, but I can only display one String. I guess that after reloading ActivityA, the listView is cleared.
So I tried following:
In a separate class called ArrayLst, I defined these two global variables:
public static ArrayList<String> arrayList = new ArrayList<String>();
public static MyAdapter arrayAdapter=null;
And in ActivityA, I’m doing this in the onClick Function:
ArrayLst.arrayList.add(0, myEditText.getText().toString());
ArrayLst.arrayAdapter.notifyDataSetChanged();
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
In ActivityB, I want to show the List, so I’m calling this:
lstView.setAdapter(ArrayLst.arrayAdapter);
However, when I click the Button in ActivityA, my App crashes. LogCat says its because of the line
ArrayLst.arrayAdapter.notifyDataSetChanged();
If I delete this line, it doesn’t crash, but I don’t get any results. What am I doing wrong?
Thanks for any help.
ps: since I’m new to Android, please keep the answers simple, so that I can understand it:)
I’d imagine it’s crashing because you never initialize arrayAdapter.
Why are you using a custom adapter (MyAdapter arrayAdapter) ?
In ActivityB you should have an static ArrayList (which it seems you have)
Then you’ll need an ArrayAdapter on ActivityB declared in your class global scope
Then in your onCreate you’ll initilize the adapter
If ActivityB extends ListActivity then you’ll call
If ActivityB extends Activity then you’ll call
the following under setContentView()
Anytime you add data to the ArrayList then you’ll call ActivityB.adapter.notifyDataSetChanged()
Without knowing what specifically you’re doing I cannot suggest a better idea because it may not be in line with what you’re looking to accomplish.