【问题标题】:How can I pass two different ID in same tabAdapter?如何在同一个 tabAdapter 中传递两个不同的 ID?
【发布时间】:2016-02-01 05:49:01
【问题描述】:

HomePage 中,它有 listView(从 MySQL 中检索)和一个 popup Menu ,其中 popup Menu 有一个 view 页面。 查看还有listView

主页

查看

HomePageView列表被点击时,它会转到有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 值就可以通过某种方式泄漏,而不是仅从其源(查看 主页)传输。如果您希望我对此进行更多研究,那么我真的需要足够的代码来重现该错误。
  • 谢谢,在阅读了另一个问题的 cmets 之后,我更加确定您不应该对 ID 使用公共变量。但我也看到你有你需要的所有帮助:)
  • @0X0nosugar 感谢您的关注

标签: android listview android-fragments tabs bundle


【解决方案1】:

额外添加一个Intent 以指示用户点击的位置。例如在 HomePage 中的ListViewOnItemClickListener 中:

intent.putExtra("FROM_HOME", true);

OnItemClickListener 中另一个ListView

intent.putExtra("FROM_HOME", false);

总而言之,您的Intent 有三个额外功能,您可以通过 ActivityB 中的getIntent() 获得:

让我们为这个活动引入三个变量

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);

现在您的活动拥有所有必要的信息来决定应该做什么。

【讨论】:

  • 我应该在 tabAdapter 中改变什么?
  • @John Joe - 在选项卡适配器中,您可以使用来自 ActivityB 的值。你需要 ID,所以你写 "bundle.putInt("ID", _id);"我认为您甚至可以更好地更改选项卡适配器的构造函数以传递 _id 的值 (--> "Encapsulation Is Good" ;))
猜你喜欢
  • 2020-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
  • 1970-01-01
  • 2017-03-25
相关资源
最近更新 更多