Determine en qué botón se hizo clic para completar la vista de lista de Android

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);
}

preguntado el 27 de agosto de 11 a las 19:08

1 Respuestas

You can pass in data to your Activity B using putExtra. Please see este 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:

Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra("arrayToUse", 1);
startActivity(i);

For activity B you would do something like this in the onCreate:

Bundle extras = icicle.getExtras();
int whichArrayToUse = extras.getInt("arrayToUse");

contestado el 23 de mayo de 17 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.