2014-10-01 21 views
0

我有一個android片段,它有一個listview。對於那個listview,我實現了一個內部OnItemClickListener類。 發生點擊時,我將選擇保存在名爲SelectedIndex的全局變量中。內部OnItemClickListener中全局變量的問題

如果再次點擊該列表,我可以正確看到前面的選擇,所以它將正確地保存在全局變量上。

問題是當我嘗試從另一個內部類訪問同一個全局變量時,例如,一個類用於監聽點擊按鈕。總是顯示我用於初始化變量(-1)的值。

片段的代碼:

/** 
* A placeholder fragment containing the view for the recentCalls list 
*/ 
public class RecentCallsFragment extends Fragment { 

    private Cursor cursorAllRows; 
    private RecentCallsTable rcTable; 
    private ListView list; 
    private RecentCallsAdapter adapter; 
    Button btnDelete, btnCreditRequest, btnCreditBlock, btnSendTo; 
    int selectedIndex; //this is the global variable that I am using. 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     rcTable = new RecentCallsTable(getActivity()); 
     cursorAllRows = rcTable.getRecentCallsCursor(); 
     adapter = new RecentCallsAdapter(getActivity(), cursorAllRows); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_main, container, false); 
     list = (ListView) view.findViewById(R.id.listViewMain); 

     btnDelete = (Button) getActivity().findViewById(R.id.buttonDelete); 
     btnCreditRequest = (Button) getActivity().findViewById(R.id.buttonCr); 
     btnCreditBlock = (Button) getActivity().findViewById(R.id.buttonCRD); 

     list.setAdapter(adapter); 
     list.setOnItemClickListener(new ItemClickHandler()); //Add the inner ItemClickLister 

     btnSendTo = (Button) getActivity().findViewById(R.id.buttonSendTo); 
     btnSendTo.setOnClickListener(new DebugOnClick());//here I add the inner clicklister 

     return view; 
    } 

    /** 
    * Class that handles the one click action on the list 
    */ 
    public class ItemClickHandler implements AdapterView.OnItemClickListener{ 

     //when there's one fast click, keep the selection on the item or remove it if already has it 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
      int prevSelection = adapter.getSelectedIndex(); 
      Toast.makeText(getActivity(), Integer.toString(selectedIndex), Toast.LENGTH_SHORT).show(); 
      int newSelection = position; 
      if(prevSelection == position){ 
       newSelection = -1; 
      } 
      selectedIndex = newSelection; //here I change the value of the global variable 
      adapter.setSelectedIndex(newSelection); 
      adapter.notifyDataSetChanged(); 
     } 
    } 

    public class DebugOnClick implements View.OnClickListener{ 

     public DebugOnClick(){ 
     } 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(getActivity(), Integer.toString(selectedIndex), Toast.LENGTH_SHORT).show(); //here I show the value of the global variable and is always -1 
     } 
    } 
} 

這可能是什麼問題?

回答

0

我剛發現這個問題。按鈕在主要活動中,所以我只是將全局變量移動到主Activity,並開始從這樣的片段中操作它:

MainActivity ma = (MainActivity) getActivity(); 
ma.rcSelected = newSelection; 
0

有一種可能性進入我的腦海。當你有一個內部類實例化時,它隱式地綁定到一個宿主類的實例(就像它是宿主類的引用一樣)。因此,我假設您使用的內部類都與託管類的不同實例鏈接,因此使用不同的selectedIndex。你的全局變量並不是真正的全局變量,它是一個實例變量。

+0

那麼可以採取什麼解決方案?將內部類移到獨立類中?我在想這個,但我不知道如何與聽衆實現這一點。 – tenhsor 2014-10-01 21:04:38

+0

我沒有看到任何外部化這些內部類的問題,一個監聽器不必是匿名的或定義爲內部類。在創建監聽器時,您應該在構造函數中提供正確的RecentCallsFragment的鏈接,它們應該共享。 – Juru 2014-10-01 21:08:10