Android 带有编辑文本和复选框的自定义列表视图
我是编程新手,在使用复选框的Android 带有编辑文本和复选框的自定义列表视图,android,checkbox,android-listview,Android,Checkbox,Android Listview,我是编程新手,在使用复选框的getCheckedItemPositions()以及从自定义列表视图中的编辑文本获取编辑值时遇到问题。谁能帮我举一个例子来创建具有上述功能的自定义列表视图。谢谢 *这是自定义listview xml代码 <CheckBox android:id="@+id/checkBox" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_al
getCheckedItemPositions()
以及从自定义列表视图中的编辑文本获取编辑值时遇到问题。谁能帮我举一个例子来创建具有上述功能的自定义列表视图。谢谢
*这是自定义listview xml代码
<CheckBox android:id="@+id/checkBox" android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:text="CheckBox"
android:onClick="clickHandler"
></CheckBox>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="TextView"
android:id="@+id/textView1"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true" android:layout_marginBottom="14dp"
*这就是我检查时试图做的
公共void onClick(视图v){
int len=lv1.getCount();
SparseBooleanArray checked=lv1.getCheckedItemPositions();
对于(int i=0;i
但这不起作用。我还想以这种方式使用编辑文本来获取值。当我选中并单击按钮时,应用程序终止。
以下代码将解决您的问题
public class SimpleCursorAdapter extends ArrayAdapter {
private Context mcontext;
private View rowview;
LayoutInflater inflater;
public static ArrayList<Boolean > itemchecked=new ArrayList<Boolean>();
public SimpleCursorAdapter(Context context,ArrayList<String> mylist)
{
super(context,your layout id);
mcontext=context;
//this is the important step
for (int i = 0; i < this.getCount(); i++)
{
itemchecked.add(i,false); // initializes all items value with false
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
rowview=convertView;
if(convertView==null)
{
rowview = inflater.inflate(R.yourlayout, parent, false);
}
TextView textView_heading = (TextView) rowview.findViewById(R.id.textView1);
CheckBox checkbox_detail=(CheckBox) rowview.findViewById(R.id.checkBox1);
checkbox_detail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);
if (cb.isChecked()) {
itemchecked.set(position, true);
// do some operations here
} else if (!cb.isChecked()) {
itemchecked.set(position, false);
// do some operations here
}
}
});
checkbox_detail.setChecked(itemchecked.get(position));
textView_heading.setText(userheading_list.get(position));
return rowview;
}
}
//now the custom list part finish
公共类SimpleCursorAdapter扩展了ArrayAdapter{
私有上下文;
私有视图行视图;
充气机;
public static ArrayList itemchecked=new ArrayList();
公共SimpleCursorAdapter(上下文上下文,ArrayList mylist)
{
super(上下文,布局id);
mcontext=上下文;
//这是重要的一步
for(int i=0;i
现在,要从列表中获取所有信息并查看选中了哪个复选框:
for(int i=0;i<yourlistadapterobject.getCount();i++)
{
View content_view=msg_adapter.getView(i,null , user_detail_list);
System.out.println("the list count"+user_detail_list.getCount());
if(MyContactAdapter.itemchecked.get(i))
{
System.out.println("is checked true");
TextView tv_heading= (TextView) content_view.findViewById(R.id.textView1);
String text=tv_heading.getText();
}
}
for(int i=0;i告诉我们您尝试了什么,这样我们就可以告诉您哪里出错了……我们不会为您编码。@Barak我尝试的代码在上面。。thanks@AlexLockwood我试过的代码在上面..谢谢,谢谢你的代码,现在请定义“不工作”。你期望(或不期望)发生了什么(或没有发生)请把你的航海日志也贴出来,这样我们就能知道去哪里找了。
public class SimpleCursorAdapter extends ArrayAdapter {
private Context mcontext;
private View rowview;
LayoutInflater inflater;
public static ArrayList<Boolean > itemchecked=new ArrayList<Boolean>();
public SimpleCursorAdapter(Context context,ArrayList<String> mylist)
{
super(context,your layout id);
mcontext=context;
//this is the important step
for (int i = 0; i < this.getCount(); i++)
{
itemchecked.add(i,false); // initializes all items value with false
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
rowview=convertView;
if(convertView==null)
{
rowview = inflater.inflate(R.yourlayout, parent, false);
}
TextView textView_heading = (TextView) rowview.findViewById(R.id.textView1);
CheckBox checkbox_detail=(CheckBox) rowview.findViewById(R.id.checkBox1);
checkbox_detail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);
if (cb.isChecked()) {
itemchecked.set(position, true);
// do some operations here
} else if (!cb.isChecked()) {
itemchecked.set(position, false);
// do some operations here
}
}
});
checkbox_detail.setChecked(itemchecked.get(position));
textView_heading.setText(userheading_list.get(position));
return rowview;
}
}
//now the custom list part finish
for(int i=0;i<yourlistadapterobject.getCount();i++)
{
View content_view=msg_adapter.getView(i,null , user_detail_list);
System.out.println("the list count"+user_detail_list.getCount());
if(MyContactAdapter.itemchecked.get(i))
{
System.out.println("is checked true");
TextView tv_heading= (TextView) content_view.findViewById(R.id.textView1);
String text=tv_heading.getText();
}
}