在 ListView 中设置复选框 Android
Set CheckBox in ListView Android
我有两个列表视图,行有简单的复选框和 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
你应该让你的 CheckBox
成为:
android:focusable="false"
android:focusableInTouchMode="false"
否则 CheckBox
点击和 ListView
项目点击有冲突。
这里有一个很好的细节示例Android ListView Checkbox Example - OnItemClickListener() and OnClickListener()
我有两个列表视图,行有简单的复选框和 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
你应该让你的 CheckBox
成为:
android:focusable="false"
android:focusableInTouchMode="false"
否则 CheckBox
点击和 ListView
项目点击有冲突。
这里有一个很好的细节示例Android ListView Checkbox Example - OnItemClickListener() and OnClickListener()