Yur*_*ets 3 checkbox android android-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);
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
以及我的带有 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>
Run Code Online (Sandbox Code Playgroud)
您可以为复选框设置标签,当您收到事件的回调时,您可以从回调中提供的视图中提取标签。设置标签中的位置(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();
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17487 次 |
| 最近记录: |