我有两个列表视图,行有简单的复选框和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;
}
}
答案 0 :(得分:2)
可以通过编程方式完成
checkBox.setChecked(true);
了解更多信息
How to handle check and uncheck dynamically created checkbox in android
答案 1 :(得分:0)
您应该将CheckBox
设为:
android:focusable="false"
android:focusableInTouchMode="false"
否则CheckBox
点击和ListView
项目点击之间存在冲突。
以下是执行Android ListView Checkbox Example - OnItemClickListener() and OnClickListener()
详细信息的好示例