如何在listview中更改CheckBox的文本?

时间:2013-06-18 12:24:54

标签: android listview

我知道这是一个愚蠢的问题,但我无法完成它。我想在点击时更改ChekcBox的文本。它会根据需要进行更改,但奇怪的是当我滚动列表视图时,该文本被分配给列表视图中的其他复选框。像Listview这样的内容每次滚动时都会重新加载。以下是我到目前为止所尝试的内容。

public View getView(int position, View convertView, ViewGroup parent) {  
              ViewHolder holder;  
              if (convertView == null) {  
                   holder = new ViewHolder();  
                   convertView = LayoutInflater.from(mContext).inflate(  
                             R.layout.row_photo, null);  
                   holder.textview = (TextView) convertView  
                             .findViewById(R.id.thumbImage);  
                   holder.checkbox = (CheckBox) convertView  
                             .findViewById(R.id.itemCheckBox);  
                   convertView.setTag(holder);  
              } else {  
                   holder = (ViewHolder) convertView.getTag();  
              }  
              holder.checkbox.setId(position);  
              holder.textview.setId(position);  
              holder.checkbox.setOnClickListener(new OnClickListener() {  
                   public void onClick(View v) {  
                        // TODO Auto-generated method stub  
                         cb = (CheckBox) v;  
                        id = cb.getId();  
                       cb.setText("Hiii");
                   }  

              });  

              holder.textview.setText(items.get(position));  

              holder.id = position;  
              return convertView;  
         }  

请指导我。

3 个答案:

答案 0 :(得分:1)

您的自定义适配器必须实现CompoundButton.OnCheckedChangeListener

然后

     cb.setChecked(mCheckStates.get(position, false)); 
     cb.setOnCheckedChangeListener(this);

然后使用选中状态将文本设置为复选框

      public boolean isChecked(int position) {
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);

    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));
    }
@Override
public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
    // TODO Auto-generated method stub
    if(isChecked)
    {
    buttonView.setText("Hello");
    }
    else
    {
        buttonView.setText("");
    }
     mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

}

实施例

public class MainActivity extends Activity implements
AdapterView.OnItemClickListener {
    int count;
private CheckBoxAdapter mCheckBoxAdapter;

String[] GENRES = new String[] {
    "Action", "Adventure", "Animation", "Children", "Comedy",
"Documentary", "Drama",
    "Foreign", "History", "Independent", "Romance", "Sci-Fi",
"Television", "Thriller"
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ListView listView = (ListView) findViewById(R.id.lv);

    listView.setItemsCanFocus(false);
    listView.setTextFilterEnabled(true);
    listView.setOnItemClickListener(this);
    mCheckBoxAdapter = new CheckBoxAdapter(this, GENRES);
           listView.setAdapter(mCheckBoxAdapter);
    Button b= (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            StringBuilder result = new StringBuilder();
            for(int i=0;i<GENRES.length;i++)
            {
                if(mCheckBoxAdapter.mCheckStates.get(i)==true)
                {
                    result.append(GENRES[i]);
                    result.append("\n");
                }

            }
            Toast.makeText(MainActivity.this, result, 1000).show();
        }

    });




   }

public void onItemClick(AdapterView parent, View view, int
position, long id) {
    mCheckBoxAdapter.toggle(position);
}

class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener
{  private SparseBooleanArray mCheckStates;
   LayoutInflater mInflater;
    TextView tv1,tv;
    CheckBox cb;
    String[] gen;
    CheckBoxAdapter(MainActivity context, String[] genres)
    {
        super(context,0,genres);
        mCheckStates = new SparseBooleanArray(genres.length);
        mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        gen= genres;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return gen.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub

        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi=convertView;
        if(convertView==null)
         vi = mInflater.inflate(R.layout.checkbox, null); 
         tv= (TextView) vi.findViewById(R.id.textView1);

         cb = (CheckBox) vi.findViewById(R.id.checkBox1);
         tv.setText("Name :"+ gen [position]);
         cb.setTag(position);
         cb.setChecked(mCheckStates.get(position, false));
        cb.setOnCheckedChangeListener(this);
        return vi;
    }
     public boolean isChecked(int position) {
            return mCheckStates.get(position, false);
        }

        public void setChecked(int position, boolean isChecked) {
            mCheckStates.put(position, isChecked);

        }

        public void toggle(int position) {
            setChecked(position, !isChecked(position));
        }
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        // TODO Auto-generated method stub
        if(isChecked)
        {
        buttonView.setText("Hello");
        }
        else
        {
            buttonView.setText("");
        }
         mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

    }

}

}

快照。当您选中复选框时,取消选中该文本时将文本设置为“”

enter image description here

答案 1 :(得分:0)

我看到你已经实现了listView,因为它应该被实现 - viewholder + convert view。 现在你所要做的就是打电话

holder.checkbox.setText("Hello World!");

答案 2 :(得分:0)

为了解决您的问题,您所要做的就是根据某些条件更改每个复选框的文本。因此,如果选中 - 设置文本“+”否则为“ - ”。根据需要更改它,但您必须更改每个复选框文本。