我有两个活动,Activity 1
和Activity 2
。 Activity 1
包含8个按钮,每个按钮通过意图将一些数据发送到Activity 2
,而Activity 2
接收数据并显示在listview
中。我想根据条件在列表视图中显示数据,因此在Activity 2
中,我将每个按钮数据放在if
块中
if(condition?)
中放置什么条件才能知道在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);
}
}
});
}
}
下面Activity2
的代码:
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);
}
}
答案 0 :(得分:4)
如何知道在其他活动中单击了哪个按钮
使用意图将按钮ID发送到下一个活动:
intent.putExtra("clicked_view_id", view.getId());
现在使用clicked_view_id
和开关盒与按钮ID进行比较:
int clickedButtonID = getIntent().getIntExtra("clicked_view_id", 0);
switch(clickedButtonID){
case R.id.button1:
...
break;
case R.id.button2:
...
break;
}
答案 1 :(得分:1)
在您的活动1中:
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);
//在列表活动中
String btnClicked = getIntent().getStringArrayListExtra("btnClicked");