Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

android - How to change the text of a CheckBox in listview?

I know its a silly question but I am not able to get it done. I want to change the text of a ChekcBox when clicked.Its changing as required but the weird thing is when I scroll the listview, that text is assigned to some other checkbox present in the listview.Seems like Listview is reloading each time we scroll.Below is what I have tried so far.

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;  
         }  

Please guide me through this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your Custom Adapter must implement CompoundButton.OnCheckedChangeListener

Then

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

Then use the checked state to set text to check box

      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);    

}

Example

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("
");
                }

            }
            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);    

    }

}

}

Snap Shot. When you check the checkbox it set's the text when you uncheck set the text to ""

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...