I have two activities, Activity 1
and Activity 2
. Activity 1
contains 8 buttons, each button sends some data to Activity 2
through intents and Activity 2
takes data and shows in listview
. I want to show data in listview based on the condition so in Activity 2
I put every button data in a if
block
if(condition?)
to know which button is clicked in Activity 1
Activity 1
public void hmoral(View v){ moralref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()){ List<String> moraldata = new ArrayList<>(); Map<String,Object> map = task.getResult().getData(); for (Map.Entry<String,Object> entry:map.entrySet()){ moraldata.add(entry.getKey()); Log.d(TAG,entry.getKey()); } Intent intent1 = new Intent(Activity1.this,Activity2.class); intent1.putExtra("moraldata", (Serializable) moraldata); startActivity(intent1); } } }); } public void hhorror(View v){ horrorref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()){ List<String> horrordata = new ArrayList<>(); Map<String,Object> map = task.getResult().getData(); for (Map.Entry<String,Object> entry:map.entrySet()){ horrordata.add(entry.getKey()); Log.d(TAG,entry.getKey()); } Intent intent = new Intent(Activity1.this,Activity2.class); intent.putExtra("horrordata", (Serializable) horrordata); startActivity(intent); } } }); } }
code of Activity2
below:
public class ListAcivity extends AppCompatActivity { ListView originallist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_acivity); originallist = findViewById(R.id.originallist); if (){ ArrayList<String> morallist = getIntent().getStringArrayListExtra("moraldata"); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,morallist); originallist.setAdapter(arrayAdapter); } if (){ ArrayList<String> horrorlist = getIntent().getStringArrayListExtra("horrordata"); ArrayAdapter<String>arrayAdapter1 = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,horrorlist); originallist.setAdapter(arrayAdapter1); } }
how to know which button is clicked in other activity
Send Button id using intent to next Activity :
intent.putExtra("clicked_view_id", view.getId());
Now use clicked_view_id
with switch case to compare with Button ids:
int clickedButtonID = getIntent().getIntExtra("clicked_view_id", 0);
switch(clickedButtonID){
case R.id.button1:
...
break;
case R.id.button2:
...
break;
}
In your activity1 :
Private String buttonText ="";
yourButtonClick.setOnClickListener(this);
@Override
public void onClick(View view) {
buttonText = getIntent().getStringExtra("btn_text");
switch (view.getId()) {
case R.id.button1:
buttonText = "buttonOne";
break;
case R.id.button2:
buttonText = "buttonTwo";
break;
case R.id.button8:
buttonText = "buttonEight";
break;
}
}
}
Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("btnClicked", buttonText);
startActivity(intent);
// In list activity
String btnClicked = getIntent().getStringArrayListExtra("btnClicked");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.