如何在listview中获取复选框的ID或位置? 当isChecked = true或false时我需要更新数据库,但我需要位置...... 也许我可以做与OnItemClick方法相同的事情?
@Override
public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id){
CheckBox cbx = (CheckBox)itemClicked.findViewById(R.id.cbxList);
cbx.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
if(cbx.getVisibility() == View.VISIBLE){
ContentValues cv = new ContentValues();
if(cbx.isChecked()){
cbx.setChecked(false);
int cbxID = 0;
cv.put(DB.CBX, cbxID);
mDB.updateCbx(cv, id);
Log.d(LOG, " updated with id = " + id + " and cbxID is " + cbxID);
}else{
cbx.setChecked(true);
int cbxID = 1;
cv.put(DB.CBX, cbxID);
mDB.updateCbx(cv, id);
Log.d(LOG, " updated with id = " + id + " and cbxID is " + cbxID);
}
}else{
Intent intentContactView = new Intent(this, ContactView.class);
intentContactView.putExtra("id", id);
startActivity(intentContactView);
}
}
使用SimpleCursorAdapter的类中的方法getView()...同样的问题,如何获取所选复选框的位置或ID?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.item, parent, false);
view = super.getView(position, convertView, parent);
CheckBox cbx = (CheckBox)convertView.findViewById(R.id.cbxList);
}
return view;
}
和我的带有listview项目的Xml文件。这里所说的复选框是不可见的,但是当我打电话给它们时它们会出现(只是为了理解。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:layout_weight="0.5"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivPhoto"
android:layout_width="@dimen/imageWidth"
android:layout_height="@dimen/imageHeight"
android:layout_gravity="center_vertical"
android:contentDescription="@string/photoDescription"
android:src="@drawable/default_contact" />
</LinearLayout>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/margin"
android:text=""
android:textSize="@dimen/textSize" />
<TextView
android:id="@+id/tvSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/margin"
android:text=""
android:textSize="@dimen/textSize" />
</LinearLayout>
<CheckBox
android:id="@+id/cbxList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/margin"
android:layout_weight="0.2"
android:focusable="false"
android:visibility="invisible" />
</LinearLayout>
答案 0 :(得分:4)
您可以将标记设置为复选框,当您获得该事件的回调时,您可以从回调中提供的视图中提取标记。设置标签中的位置(setTag()是设置标签的方法)。 所以你的东西应该像下面这样:
public View getView(int position, View convertView, ViewGroup parent)
{
//...
CheckBox cbx;
//...
cbx.setTag(position);
cbx.setOnCheckedChangeListener(checkListener);
//...
}
OnCheckedChangeListener checkListener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
int position = buttonView.getTag();
}
};
答案 1 :(得分:0)
您正在使用listview适配器的onItemClick方法,您传递的参数位置是用户点击的位置根据您的需要使用该变量的值。
答案 2 :(得分:0)
您尝试过以下方法吗?
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convertView init and other stuff
mCheckBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean state = ((CheckBox) v).isChecked();
// mCheckBoxStates is an ArrayList<Boolean> that contains current states of checkboxes
// (if you need such information somewhere in your app)
mCheckBoxStates.get(position) = state;
if (state) {
// Update your database according to mCheckBoxStates values
}
});
// other stuff
}