2014-10-01 18 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