2013-12-16 27 views
0

当我点击一个项目它不运行我的onListItemClick代码,我不知道为什么!onListClickItem不响应任何东西

我有一个片段扩展listFragment与此代码。这是onCreateView()

{ 
adapter = new CustomArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, names); 
     setListAdapter(adapter); 
} 


@Override 
public void onListItemClick(ListView l, View v, int position, long id){ 

    CheckedTextView check = (CheckedTextView) v; 

    if (check.isChecked()){ 
     selections.remove((Integer) position); 
    } 
    else{ 
     selections.add((Integer) position); 
    } 

    //DOESNT RUN THIS 

} 

这是我的自定义适配器

package project.android.bellringing; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckedTextView; 

public class CustomArrayAdapter<T> extends ArrayAdapter<String> { 

private ArrayList<Boolean> bitset = new ArrayList<Boolean>(); 
private ArrayList<String> names; 

public CustomArrayAdapter(Context context, int textViewResourceId){ 
    super(context, textViewResourceId); 
} 


public CustomArrayAdapter(Activity a, int textViewResourceId, ArrayList<String> entries) {  
    super(a, textViewResourceId, entries); 

    for (int i = 0; i < entries.size(); i++){ 
     bitset.add(i, false); 
    } 

    names = new ArrayList<String>(entries); 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    CheckedTextView c; 

    if (convertView == null) { 

     LayoutInflater vi = LayoutInflater.from(getContext()); 
     c = (CheckedTextView) vi.inflate(android.R.layout.simple_list_item_multiple_choice, null); 

    } 
    else{ 
     c = (CheckedTextView) convertView; 
    } 

    final CheckedTextView ctv = c; 
    ctv.setText(names.get(position)); 

    if (bitset.get(position) == false) 
     ctv.setChecked(false); 
    else 
     ctv.setChecked(true); 

    ctv.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      ctv.setChecked(!ctv.isChecked()); 

      if (bitset.get(position) == false) 
       bitset.set(position, true); 
      else 
       bitset.set(position, false); 
     } 

    }); 


    return ctv; 
} 

}

+0

我的样本是否有用?还是您需要其他信息? –

回答

0

得到的东西,你CheckedTextView被窃取输入的触摸,所以在您的xml把它定义为Not focusable

+0

你可以在那里看到它。所以不行 –

0

问题是CheckedTextViewListView的可聚焦性。您可以在CheckedTextView的布局中设置android:focusable="false"

这会导致将调用OnItemClick,但CheckedTextView上的OnClickListener未被调用,您必须自己处理已检查和未检查的状态。我对它感兴趣,所以我做了一个样本。它的解释是可能比代码,所以这里更长,更复杂的是:

核心部分是处理CheckedTextView状态onItemClick,也处理国家在getView年底,国家在BooleanSparseArray举行时更新从onItemClick

public class TestListActivity extends ListActivity 
{ 
    private TestAdapter mTestAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.list_activity); 
     final List<String> names = Arrays.asList(new String[]{"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}); 

     mTestAdapter = new TestAdapter(this, names); 

     setListAdapter(mTestAdapter); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) 
    { 
     CheckedTextView checkedTextView = (CheckedTextView) v; 

     boolean toggleChecked = !checkedTextView.isChecked(); //toggle checked state 
     checkedTextView.setChecked(toggleChecked); 
     checkedTextView.setCheckMarkDrawable(toggleChecked ? android.R.drawable.checkbox_on_background : 0); //setting correct check mark 

     //setting the flag to properly refresh when scrolling 
     mTestAdapter.setChecked(position, toggleChecked); 
    } 

    private static class TestAdapter extends ArrayAdapter<String> 
    { 
     private SparseBooleanArray bitset = new SparseBooleanArray(); 

     private TestAdapter(Context context, List<String> objects) 
     { 
      super(context, 0, objects); 
     } 

     public void setChecked(int position, boolean checked) 
     { 
      bitset.append(position, checked); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
     { 
      CheckedTextView checkedTextView; 
      if (convertView == null) 
      { 
       checkedTextView= (CheckedTextView) LayoutInflater.from(getContext()).inflate(R.layout.checked, null); 
      } 
      else 
      { 
       checkedTextView = (CheckedTextView) convertView; 
      } 

      boolean isChecked = bitset.get(position); 
      checkedTextView.setChecked(isChecked); //setting checked to correct state 
      checkedTextView.setCheckMarkDrawable(isChecked ? android.R.drawable.checkbox_on_background : 0); // setting correct check mark 
      checkedTextView.setText(getItem(position)); 

      return checkedTextView; 
     } 
    } 
}