【问题标题】:Default checkbox with checked not working选中的默认复选框不起作用
【发布时间】:2015-10-21 07:45:31
【问题描述】:

CheckBox 默认值不起作用。我已经通过setcheck(true) 给出了CheckBox 的值,并且在布局中也给出了,我需要将检查的值传递给arraylist。

如何使用arraylist在两个类之间发送值如何在android中将数据保存在资产db中。

  public class CustomListAdapter extends ArrayAdapter<String> 
  {

   RetrieveFromDB sqlConn;
    private final Context context;
    private ArrayList<String> resArray;
    public static ArrayList<String> selitemsList=new ArrayList<String>();

    public CustomListAdapter(Context context, ArrayList<String> resArray)
    {
    super(context, R.layout.categorylist, resArray);
    // TODO Auto-generated constructor stub
    this.context = context;
    this.resArray = resArray;
     }
     public View getView(final int position, View convertView, ViewGroup       parent) {

    try{
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_item_2, parent, false);
        CheckBox chk=(CheckBox)rowView.findViewById(R.id.checkBox1);
        TextView  list= (TextView) rowView.findViewById(R.id.text2);
        list.setText(resArray.get(position).toString());
        sqlConn=new RetrieveFromDB(context);
          selitemsList=sqlConn.displaySelectedItems(EditSelectedItems.sel_category);
        sqlConn.close();

        if(selitemsList.contains(list.getText().toString()))
            chk.setChecked(true);
        else
            chk.setChecked(false);
        chk.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                             CategoryListView.st.add(resArray.get(position).toString());
                    selitemsList.add(resArray.get(position).toString());

                };

【问题讨论】:

    标签: android android-checkbox


    【解决方案1】:

    您关于 .setChecked 的代码是正确的。尝试记录一些关于 selitemsList 的内容,你可以查看 logcat。例如

    Log.d("my debug", "selitemsList content:");
    for (String s: selitemsList){
        Log.d("my debug", "\t- " + s);
    }
    if(selitemsList.contains(list.getText().toString())){
        Log.d("my debug", "selitemsList contains " + list.getText().toString());
        chk.setChecked(true);
    }else{
        Log.d("my debug", "selitemsList doesn't contain " + list.getText().toString());
        chk.setChecked(false);
    }
    

    在这种模式下,您应该能够在 logcat 中看到问题出在哪里。可能 selitemsList 没有正确填写。

    【讨论】:

      【解决方案2】:

      使用SparseBooleanArray 获取所选项目的列表。

      int len = listView.getCount();
      ArrayList<String> selectedItems = new ArrayList<String>();
      SparseBooleanArray checked = listView.getCheckedItemPositions();
      for (int i = 0; i < len; i++) {
          if (checked.get(i)) {
          String item = cont_list.get(i);
          selectedItems.add(item);
      }
      

      【讨论】:

        猜你喜欢
        • 2023-02-08
        • 2019-05-06
        • 1970-01-01
        • 1970-01-01
        • 2018-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多