我在活动A中有一个列表视图。单击列表项后,它将打开另一个activityB。活动B有一个按钮,当点击时,该按钮显示活动B在点击作为Toast消息时产生的列表项。这是可能的。 请帮帮我
这是我在活动B中尝试过的代码
Button btn = (Button) findViewById(R.id.singleitemviewButton);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
List< MyList> mlist = null;
MyList codes = (MyList) getItem(position);
}
private Object getItem(int position)
{
return mlist.get(position);
}
});
我在找到未知变量位置
的位置时出错活动a点击听众
@Override
public void onItemClick(AdapterView<?> p1, View view, int position, long p4)
{
Codelist codes = (CodeList) get(position);
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("selected item" , codes);
startActivity(intent);
}
新尝试的代码
Button favbtn = (Button) findViewById(R.id.singleitemviewButton);
favbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
List <CodeList> codelist = null;
Bundle extras = getIntent().getExtras();
CodeList codes = extras.getParcelable("selected item");
if(checkArchivedItem(codes)){
archvPrefrnces.removeArchive(SingleItemView.this, codes);
}else{
archvPrefrnces.addArchive(SingleItemView.this, codes);
Toast.makeText(SingleItemView.this, getString(R.string.archive_added), Toast.LENGTH_SHORT).show();
}
}
private boolean checkArchivedItem(CodeList checkCodes) {
boolean check = false;
List<CodeList> archives = archvPrefrnces.getArchives(getApplicationContext());
if (archives != null) {
for (CodeList codes : archives) {
if (codes.equals(checkCodes)) {
check = true;
break;
}
}
}
return check;
}
});
答案 0 :(得分:1)
使您要发送的模型Parcelable。然后在你的活动A中选择一个项目写:
Intent intent = new Intent(this, B.class);
intent.putExtra("SELECTED_ITEM", selectedItemObject);
startActivity(intent);
然后在您的活动B中,按钮点击监听器的代码应该是:
Button btn = (Button) findViewById(R.id.singleitemviewButton);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Bundle extras = getIntent().getExtras();
Item item extras.getParcelable("SELECTED_ITEM");
}
});
答案 1 :(得分:0)
当您在项目上启动新活动时,您可以在传递给活动的意图中发送您想要在吐司中显示的位置或其他详细信息,如下所示
intent.putExtra("details 1", "position or some thing");
intent.putExtra("details 2", "something");
intent.putExtra("details", "something");
startActivity(intent);
现在你可以通过这样做活动
String s1 = getIntent().getStringExtra("details 1");
Stirng s2 = getIntent().getStringExtra("details 2");