在 HomePage 中,它有 listView
(从 MySQL 检索)和一个 popup Menu
,其中 popup Menu
有一个查看页面。 View 还有 listView
。
查看
当点击HomePage 或View 列表时,它会转到fragment,它有4 个选项卡并允许用户根据ID。
点击主页列表时
public static int ID;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
ID = search.get(position).getID();
Toast.makeText(getApplicationContext(), "Edit" + ID, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), ActivityB.class);
intent.putExtra("ID", ID); // pass to tabAdapter
intent.putExtra("name", name); // pass to tabAdapter
startActivity(intent);
}
});
点击查看时
public static int ID;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
ID = search.get(position).getID();
Toast.makeText(getApplicationContext(), "Edit" + ID, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), ActivityB.class);
intent.putExtra("ID", ID); // pass to tabAdapter
intent.putExtra("name", name); // pass to tabAdpter
startActivity(intent);
}
});
TabAdapter
public Fragment getItem(int index) {
// TODO Auto-generated method stub
if(index == 0) {
Fragment fragment=new EditInformation();
Bundle bundle = new Bundle();
bundle.putInt("ID", HomePage.ID);
fragment.setArguments(bundle);
return fragment;
}
}
编辑信息
Bundle bundle = this.getArguments();
if (getArguments() != null) {
ID = bundle.getInt("ID");
}
Toast.makeText(getActivity(),"SS"+ID,Toast.LENGTH_LONG).show();
当我单击主页中的列表时,它会在主页和 EditInformation 中显示正确的 ID。但是当我在 View 中单击列表时,它显示正确的 ID,但在 EditInformation 中没有 ID 显示。
我在 bundle.putInt("ID", HomePage.ID);
之后添加 bundle.putInt("ID", View.ID);
,它适用于All ,但不适用于 HomePage ! HomePage 列表显示其 ID,但在 EditInformation 中显示 0 id。
如何解决??? 我们如何区分列表是从 All 还是 View 点击???谢谢
最佳答案
放置另一个 Intent
extra 来指示用户点击的位置。例如在 HomePage 中 ListView
的 OnItemClickListener
中:
intent.putExtra("FROM_HOME", true);
而在 OnItemClickListener
中的另一个 ListView
intent.putExtra("FROM_HOME", false);
总而言之,您的Intent
有三个额外的功能,您可以通过ActivityB 中的getIntent()
获得:
让我们为这个 Activity 引入三个变量
private int _id;
private String _name;
private boolean _fromHome;
然后在 onCreate()
中,您可以获得这样的 intent extras:
Intent intent = getIntent();
_id = intent.getIntExtra("ID", -1);
_name = intent.getStringExtra("name");
_fromHome = intent.getBooleanExtra("FROM_HOME", false);
现在您的 Activity 拥有决定应该做什么的所有必要信息。
关于android - 如何在同一个 tabAdapter 中传递两个不同的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35123745/