如何在ListView中更改CheckBox的文本?
我知道这是一个愚蠢的问题,但我无法完成。 我想改变一个ChekcBox的文本,当clicked.Its根据需要改变,但奇怪的是,当我滚动列表视图,该文本被分配给列表视图中的一些其他checkbox。像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; }
请指导我通过这个。
您的自定义适配器必须实现CompoundButton.OnCheckedChangeListener
然后
cb.setChecked(mCheckStates.get(position, false)); cb.setOnCheckedChangeListener(this);
然后使用选中状态将文本设置为checkbox
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); } } }
快照。 当你勾选checkbox时,当你取消选中文字设置为“”
我看到你已经实现了listView,因为它应该实现 – viewholder + convert视图。 现在你所要做的就是打电话
holder.checkbox.setText("Hello World!");
为了解决您的问题,您所要做的只是在某些条件下更改每个checkbox的文本。 所以如果被检查设置文本“+”其他“ – ”。 改变它,只要你喜欢,但你必须改变每个checkbox文本。
- RecyclerView.ViewHolder – getLayoutPosition vs getAdapterPosition
- 可绘制到byte
- 在Android操作栏中设置SearchView的样式
- 如何实施棒棒糖的材料devise高程
- 如何在Android应用程序中使用现有的数据库
- Android上最好的REST客户端框架/实用工具
- ListView:setItemChecked只适用于标准ArrayAdapter – 在使用自定义ArrayAdapter时不起作用?
- 如何创build包含客户端证书链的BKS(BouncyCastle)格式的Java Keystore
- 阻止Android活动对话框在外部closures时closures