【发布时间】:2016-02-01 05:49:01
【问题描述】:
在 HomePage 中,它有 listView(从 MySQL 中检索)和一个 popup Menu ,其中 popup Menu 有一个 view 页面。 查看还有listView。
查看
当HomePage或View列表被点击时,它会转到有4个标签的fragment,并允许用户根据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。
这个怎么解决??? 我们如何区分列表是单击全部还是查看?谢谢
【问题讨论】:
-
只是为了确保我理解问题:通过单击主页中的列表项而不是单击视图中的列表项,ID 被正确传输到同一目标 (ActivityB),尽管您使用的是侦听器中的相同代码?
-
如果没有最小的、完整的等......示例很难确定,但我对“public static int ID”有一种不好的感觉。这样,ID 值就可以通过某种方式泄漏,而不是仅从其源(查看 或 主页)传输。如果您希望我对此进行更多研究,那么我真的需要足够的代码来重现该错误。
-
@0X0nosugar stackoverflow.com/questions/35124362/…
-
谢谢,在阅读了另一个问题的 cmets 之后,我更加确定您不应该对 ID 使用公共变量。但我也看到你有你需要的所有帮助:)
-
@0X0nosugar 感谢您的关注
标签: android listview android-fragments tabs bundle