CheckBox未使用自定义适配器在自定义Listview中切换

时间:2012-10-26 14:39:09

标签: android android-listview onitemclicklistener android-checkbox

我正在尝试使用两个TextView和一个CheckBox创建自定义ListView。

//define your custom adapter

public class Customadaptor extends ArrayAdapter<HashMap<String, Object>>
{
    public  ArrayList<HashMap<String, Object>> players;
   // boolean array for storing
   //the state of each CheckBox 
   boolean[] checkBoxState;
   static int i =0; 


   ViewHolder viewHolder;

   public Customadaptor(Context context, int textViewResourceId,ArrayList<HashMap<String, Object>> player) {

    //let android do the initializing :)
    super(context, textViewResourceId, player); 
    players = player;
  //create the boolean array with 
   //initial state as false
  checkBoxState=new boolean[player.size()];
  }

   String[] arr = {" "};
    //class for caching the views in a row  
 public class ViewHolder
 {
   TextView name,name2;
   CheckBox checkBox;
 } 

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

   if(convertView==null)
    {
       LayoutInflater inflater =LayoutInflater.from(getContext());
   convertView=inflater.inflate(R.layout.customlistview, null);
   viewHolder=new ViewHolder();

   viewHolder.name=(TextView) convertView.findViewById(R.id.name);
   viewHolder.team=(TextView) convertView.findViewById(R.id.name2);
   viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkBox);

    //link the cached views to the convertview
   convertView.setTag( viewHolder);

    }
   else
       viewHolder=(ViewHolder) convertView.getTag();


    //cache the views
    viewHolder.name=(TextView) convertView.findViewById(R.id.name);
    viewHolder.team=(TextView) convertView.findViewById(R.id.name2);
    viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkBox);

     //link the cached views to the convertview
    convertView.setTag( viewHolder);          
  viewHolder.name.setText(players.get(position).get("name").toString());
  viewHolder.team.setText(players.get(position).get("team").toString());

   //VITAL PART!!! Set the state of the 
   //CheckBox using the boolean array
        viewHolder.checkBox.setChecked(checkBoxState[position]);   
        viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {

               public void onClick(View v) {
                if(((CheckBox)v).isChecked())
                 checkBoxState[position]=true;
                else
                 checkBoxState[position]=false;

                }
               });


   //return the view to be displayed
   return convertView;
  } 
 }

我尝试使用setOnItemClickListener()来切换复选框。代码工作正常。唯一的区别是复选框没有被切换。

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View item,
                    int position, long id) {    
                Log.v("Position",""+position);

                CheckBox checkBox = (CheckBox)item.findViewById(R.id.checkBox); 
                checkBox.toggle();
                Log.v("Position",""+position);
            }

        }); 

这不起作用。 请告诉我哪里出错了。

1 个答案:

答案 0 :(得分:0)

android:focusable="false"
android:focusableInTouchMode="false"

在list_item布局中为复选框组件添加上述代码。

希望它有所帮助!,为我工作。

您可以阅读更多here

相关问题