I have 2 activities – Say Activity A and B.
My main activity(A) has 2 buttons. I start another activity when Button 1 is clicked. The second activity(B) creates a listview which uses a string array to populate custom list items.
Now when Button 2 in Activity A is clicked, I want to populate the list view using the same code in Activity B but use a different string array. How do I do that? I don’t want to create another activity just to replace the string arryay for the ListView.
/*ACTIVITY A /
public class mainmenu extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent i = new Intent(getApplication(), ActivityB.class);
startActivity(i);
});
Button button2 = (Button) findViewById(R.id.Button02);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplication(), ActivityB.class);
startActivity(i);
}
});
/* ACTIVITY B****
public class anotheractivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] names = getResources().getStringArray(R.array.heading_name);
String[] descr = getResources().getStringArray(R.array.heading_desc);
this.setListAdapter(new myArrayAdapter(this, names, descr));
ListView lv = getListView();
Resources res = getResources();
Drawable sm = res.getDrawable(R.drawable.mydivider);
lv.setDivider(sm);
lv.setDividerHeight(1);
}
You can pass in data to your Activity B using putExtra. Please see this question for a good example. In your Activity B you can get this data, and populate your list according to what was passed in.
For activity A:
For activity B you would do something like this in the onCreate: