问题
How to check the CheckBox of ListView when item clicked?
I have a ListView with CheckBox, TextView, Button.
Here i want to select multiple rows of ListView and so used CheckBox. If i click on a row, i want to make its corresponding CheckBox to be checked and get the RowID of the clicked item of ListView.
If i click on Button, i want to open a pop-up or another Screen(Activity) but CheckBox must not be checked.
How to do this? Please help.
Thanks in advance.
Edit:
This is how my layout looks.
Code is simple using Adapter to the ListView.
public class Adapter extends BaseAdapter{
private Context context;
Button btnView;
CheckBox box;
ArrayList<String> altextView1;
ArrayList<String> altextView2;
public Adapter(Context c, Button view, CheckBox checkBox, ArrayList<String> textView1, ArrayList<String> textView2) {
this.context=c;
this.btnView=view;
this.box=checkBox;
this.altextView1=textView1;
this.altextView2=textView2;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return altextView1.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View view=arg1;
view=LayoutInflater.from(context).inflate(R.layout.list7, null);
TextView tv1=(TextView)view.findViewById(R.id.tV1);
TextView tv2=(TextView)view.findViewById(R.id.tV2);
btnView=(Button)findViewById(R.id.btnView);
box=(CheckBox)findViewById(R.id.cB);
tv1.setText(altextView1.get(arg0));
tv2.setText(altextView2.get(arg0));
return view;
}
}
回答1:
Set checkbox object as tag to your row view that might be your 'convertView' in getView() method of your adapter.
Write on click listener on your row view.
Inside that click-listener to row view getTag from view that's parameter in onClick method and cast it to checkbox and then setChecked to true for that checkbox object.
code might look like this,
convertView.setTag(yourCheckBoxObject);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v.getTag();
cb.setChecked(true);
}
});
You need to follow this thread to learn it.. just read whole thread and try to understand and then implement .. this is the best help you can get. Getting an issue while checking the dynamically generated checkbox through list view
回答2:
In your custom adaptor do like this
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
Button textView = (Button) layoutInflater.inflate(R.layout.btn, null);
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//start activity or open pop-up
}
});
........
}
And in onListItemClick callback :
protected void onListItemClick(ListView l, android.view.View v, int position, long id) {
// using view v get check box and make it checked
CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkbx);
};
Hope this will work.Sorry I have not tested. Please let me know the result.
回答3:
First, you will need to call setChoiceMode() on the ListView in Java code to set the choice mode, supplying either CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE as the value. You can get your ListView from a ListActivity via getListView(). You can also declare this via the android:choiceMode attribute in your layout XML.
Then, rather than use android.R.layout.simple_list_item_1 as the layout for the list rows in your ArrayAdapter constructor, you will need to use either
android.R.layout.simple_list_item_single_choice
or
android.R.layout.simple_list_item_multiple_choice
for single-choice or multiple-choice lists, respectively.
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:choiceMode="multipleChoice"
/>
and java code could be
package com.commonsware.android.checklist;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ChecklistDemo extends ListActivity {
private static final String[] items={"lorem", "ipsum", "dolor",
"sit", "amet","consectetuer", "adipiscing", "elit", "morbi", "vel",
"ligula", "vitae", "arcu", "aliquet", "mollis",
"etiam", "vel", "erat", "placerat", "ante",
"porttitor", "sodales", "pellentesque", "augue", "purus"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,items));
}
}
来源:https://stackoverflow.com/questions/24157026/how-to-check-the-checkbox-of-listview-when-item-clicked