【问题标题】:Set SelectedValue in ListView CheckBox in Android在 Android 的 ListView CheckBox 中设置 SelectedValue
【发布时间】:2014-06-04 01:24:15
【问题描述】:

我在 Android 的 CheckBox 中使用 ListView。它工作正常。

现在,我想根据参数在CheckBox 中设置选定值。所以在加载列表时,如果 Parameter User 是 Yes 然后检查 Checkbox else 不检查。在这里,在下面基于 country.getUser() 的代码中,我想检查CheckBox。来源:ListView CheckBox in Android

我的代码:

private class MyCustomAdapter extends ArrayAdapter<country> {

  private ArrayList<country> countryList;

  public MyCustomAdapter(Context context, int textViewResourceId,
    ArrayList<country> countryList) {
   super(context, textViewResourceId, countryList);
   this.countryList = new ArrayList<country>();
   this.countryList.addAll(countryList);
  }

  private class ViewHolder {
   TextView code;
   CheckBox name;
  }

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

   ViewHolder holder = null;
   Log.v("ConvertView", String.valueOf(position));

   if (convertView == null) {
   LayoutInflater vi = (LayoutInflater)getSystemService(
     Context.LAYOUT_INFLATER_SERVICE);
   convertView = vi.inflate(R.layout.country_info, null);

   holder = new ViewHolder();
   holder.code = (TextView) convertView.findViewById(R.id.code);
   holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
   convertView.setTag(holder);

    holder.name.setOnClickListener( new View.OnClickListener() {
     public void onClick(View v) {
      CheckBox cb = (CheckBox) v ;
      Country country = (Country) cb.getTag();
      Toast.makeText(getApplicationContext(),
       "Clicked on Checkbox: " + cb.getText() +
       " is " + cb.isChecked(),
       Toast.LENGTH_LONG).show();
      country.setSelected(cb.isChecked());
     }
    });
   }
   else {
    holder = (ViewHolder) convertView.getTag();
   }

   Country country = countryList.get(position);
   holder.code.setText(" (" +  country.getCode() + ")");
   holder.name.setText(country.getName());
   holder.name.setChecked(country.isSelected());
   holder.name.setTag(country);

   return convertView;

  }
 }

请帮我解决这个问题。

【问题讨论】:

    标签: android listview checkbox android-listview


    【解决方案1】:

    您可以使用setChecked 来检查复选框取决于这样的值

    if(country.getUser().equalsIgnoreCase("Yes")){
            holder.checkBox.setChecked(true);
        }
        else {
            holder.checkBox.setChecked(false);
        }
    

    【讨论】:

    • 我试过了,但它没有存储选定的值。
    • 你检查源了吗?
    • 哦。使用复选框视图 holder.checkBox.setTag(true)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    相关资源
    最近更新 更多