2016-02-01 60 views
1

首頁,它有listView(從MySQL檢索)和popup Menu,其中popup Menu視圖頁。還有listView如何在同一個tabAdapter中傳遞兩個不同的ID?

首頁 enter image description here

視圖

enter image description here

首頁視圖點擊列表,它會去片段其中有4個標籤,並允許用戶做編輯b在身份證上。

首頁列表點擊

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; 

     } 
} 

EditInformation

Bundle bundle = this.getArguments(); 
     if (getArguments() != null) { 
      ID = bundle.getInt("ID"); 
     } 
     Toast.makeText(getActivity(),"SS"+ID,Toast.LENGTH_LONG).show(); 

當我點擊主頁上的列表中,它顯示在主頁和EditInformation正確的ID。但是,當我在視圖中單擊列表時,它將顯示正確的ID,,但在EditInformation中沒有ID顯示。

bundle.putInt("ID", HomePage.ID);之後添加bundle.putInt("ID", View.ID);,它適用於所有,但不適用於首頁作品! HomePage列表顯示其ID,但在EditInformation中顯示0 ID。

如何解決這個??? 我們如何區分列表是單擊全部還是視圖?謝謝

+0

只是爲了確保我明白了這個問題:通過單擊HomePage中的列表項目,而不是通過單擊View中的列表項目,雖然您在偵聽器中使用相同的代碼,但ID已正確傳輸到同一目標(ActivityB) ? – 0X0nosugar

+0

@ 0X0nosugar –

+0

沒有像最小,完整等等的東西....例如它很難確定,但我對「**公共靜態** int ID」有不好的感覺。這樣,ID值以某種方式泄漏,而不是僅從其源(View *或* Homepage)傳輸。如果你想讓我更深入地研究一下,那麼我真的需要足夠的代碼來重現錯誤。 – 0X0nosugar

回答

1

再放一個Intent表示用戶點擊的位置。例如在的ListView首頁OnItemClickListener

intent.putExtra("FROM_HOME", true); 

而且在其他ListView

intent.putExtra("FROM_HOME", false); 

OnItemClickListener總而言之,你Intent有三個額外您可以通過getIntent()得到在活動B

讓我們來介紹一下thr這項活動的EE變量

private int _id; 
private String _name; 
private boolean _fromHome; 

然後在onCreate()你可以得到額外的意圖是這樣的:

Intent intent = getIntent(); 
_id = intent.getIntExtra("ID", -1); 
_name = intent.getStringExtra("name"); 
_fromHome = intent.getBooleanExtra("FROM_HOME", false); 

現在,你的活動擁有所有必要的信息,來決定什麼應該做的事。

+0

我應該在tabAdapter中更改哪些內容? –

+0

@John Joe - 在選項卡適配器中,您可以使用來自ActivityB的值。你需要這個ID,所以你寫了「bundle.putInt(」ID「,_id);」我認爲你甚至可以改變tab適配器的構造函數來傳遞_id的值( - >「Encapsulation Is Good」;)) – 0X0nosugar

相關問題