gpt4 book ai didi

android - 在 ListView Android 中设置 CheckBox

转载 作者:行者123 更新时间:2023-11-29 20:51:33 25 4
gpt4 key购买 nike

我有两个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com