android - 在 ListView Android 中设置 CheckBox

标签 android android-listview android-checkbox

我有两个 ListView ,行有简单的复选框和 TextView,当我单击 list1 中的复选框时,我将项目移动到 List2 并希望显示其复选框已选中。即使我在 List2 适配器中对 checkbox.setSelected(true) 进行硬编码,但复选框似乎没有显示复选标记。这是代码,有什么想法吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="7dp"
>
<CheckBox
    android:id="@+id/checkbox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<TextView
    android:id="@+id/item_title"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="14dp"
    android:textStyle="normal"
    android:layout_marginRight="25dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"/>
 </LinearLayout>

   public class AlphabetListDemo extends Activity {

//String of alphabets //
List<ListItem> alphabets = new ArrayList<ListItem>();
List<ListItem> prods = new ArrayList<ListItem>();

ListView L1, L2;
myAdapter myadp;
myAdapter2 myadp2;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alphabet_list_demo);

    L1 = (ListView)findViewById(R.id.list1);
    L2 = (ListView)findViewById(R.id.list2);

    myadp = new myAdapter(this,alphabets);
    myadp2 = new myAdapter2(this,prods);
    L1.setAdapter(myadp);
    L2.setAdapter(myadp2);

    L1.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            //add to Other List & update
            prods.add(alphabets.get(arg2));
            L2.setAdapter(myadp2);

            //remove from current List & update
            alphabets.remove(arg2);
            L1.setAdapter(myadp);
        }
    });

    L2.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            //add to Other List & update
            alphabets.add(prods.get(arg2));
            L1.setAdapter(myadp);

            //remove from current List & update
            prods.remove(arg2);
            L2.setAdapter(myadp2);
        }
    });

    final EditText textField = (EditText) findViewById(R.id.editText);
    Button addBtn = (Button) findViewById(R.id.addBtn);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(textField.getText().length()>0){
                alphabets.add(new ListItem(textField.getText().toString(),false));
                L1.setAdapter(myadp);
            }
        }
    });

}


class myAdapter extends ArrayAdapter<ListItem>
{
    TextView label;
    CheckBox checkBox;
    View row;
    public myAdapter(Context context,List<ListItem>list)
    {
        super(context, android.R.layout.simple_list_item_1, list);
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        try{
            LayoutInflater inflater=getLayoutInflater();
            row = inflater.inflate(R.layout.simple_list_item_1, parent, false);
            label = (TextView)row.findViewById(R.id.item_title);
            checkBox= (CheckBox)row.findViewById(R.id.checkbox1);
            label.setText(alphabets.get(position).description);
            label.setTextColor(Color.BLACK);
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    prods.add(alphabets.get(position));
                    L2.setAdapter(myadp2);

                    //remove from current List & update
                    alphabets.remove(position);
                    L1.setAdapter(myadp);
                }
            });

        }catch(Exception e){

        }
        return row;
    }
}
// adapter for second list.....
class myAdapter2 extends ArrayAdapter<ListItem>
{
    TextView label;
    CheckBox checkBox;
    View row;
    public myAdapter2(Context context,List<ListItem>list)
    {
        super(context, android.R.layout.simple_list_item_1, list);
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        try{
            LayoutInflater inflater=getLayoutInflater();
            row = inflater.inflate(R.layout.simple_list_item_1, parent, false);
            label = (TextView)row.findViewById(R.id.item_title);
            checkBox= (CheckBox)row.findViewById(R.id.checkbox1);
            label.setText(prods.get(position).description);
            label.setTextColor(Color.BLUE);
            checkBox.setSelected(true);

            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //add to Other List & update
                    alphabets.add(prods.get(position));
                    L1.setAdapter(myadp);

                    //remove from current List & update
                    prods.remove(position);
                    L2.setAdapter(myadp2);
                }
            });
        }catch(Exception e){

        }
        return row;
    }
}

class ListItem{
    public boolean isSelected;
    public String description;

    ListItem(String description, boolean isSelected){
        this.description = description;
        this.isSelected=isSelected;
    }
}

最佳答案

可以编程方式完成

checkBox.setChecked(true);

获取更多信息

How to handle check and uncheck dynamically created checkbox in android

关于android - 在 ListView Android 中设置 CheckBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29046202/

相关文章:

java - 安卓 : Inform adapter that data-set has changed from a static method

安卓 : set visibility of individual buttons in a custom ListView

xamarin.android - 更改 Xamarin Android 复选框的大小

android - 如何创建圆形的复选框?

java - 在 Firebase 数据库中读取和写入数据

java - 如何禁用标签栏中的滚动?

android - 如何让ListView Adapter中的OnClick调用Activity函数

android - listview下的textview

android - 如果像 flipkart 价格范围一样在 RecyclerView 中检查过,如何将复选框设置为 true

java - JSONObject.toString() 返回 OutOfMemoryError