1

我正在尝试使用两个 TextViews 和一个 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);
            }

        }); 

这是行不通的。请告诉我哪里出错了。

4

1 回答 1

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

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

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

你可以在这里阅读更多。

于 2012-10-26T14:53:06.270 回答