【问题标题】:How to get position of checkbox in listview如何在列表视图中获取复选框的位置
【发布时间】:2014-09-03 23:12:14
【问题描述】:

如何获取列表视图中复选框的 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;
}

和我的带有列表视图项目的 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>

【问题讨论】:

  • 你会在网上收到很多与此相关的帖子,上网浏览一下
  • 您能否也发布您的视图的 xml 布局来定义您的 ListView 的每个项目?
  • 看来您可能必须定义一个自定义适配器并在 getView() 方法中操作复选框事件。根据您提供的信息,您的复选框事件可能与附加到包含该复选框的每个列表项的单击事件“冲突”。在自定义适配器的 getView() 方法中处理此类事件可解决此问题。
  • @Richa ,不幸的是,我没有找到,但我已经看够了......
  • @user1841702 ,已编辑。

标签: android checkbox android-listview


【解决方案1】:

您可以将标签设置为复选框,当您获得事件的回调时,您可以从回调中提供的视图中提取标签。设置标签中的位置(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();
    }
};

【讨论】:

  • 您介意我编辑您的格式答案吗?我输入了相同的答案,但你先发布了,我很乐意复制/粘贴我的格式化代码。
  • 它不工作,我试过.. 因为我解析的位置没有移动。它总是一样的,因为我没有点击列表的项目。
  • @Yurets 你到底是什么意思?这是一种非常常见的方法。您是否检查过positiononCheckedChange() 中是否正确?如果是这样,那么其他地方有问题。
  • @MikeM。是的,当然我检查了它......“位置”保持不变,无论我选择什么复选框......也许是某种方式允许我在选中时使用值“1”更新数据库,在未选中时使用“0”更新数据库?我的意思是再次调用这部分代码: int cbxID = 1; // 1 if cheched cv.put(DB.CBX, cbxID); mDB.updateCbx(cv, id);
  • @MikeM。天哪,我比较松散……你说得对,在 getView 中解决了……非常感谢你!!!
【解决方案2】:

您正在使用 listview 适配器的 onItemClick 方法,您传入的参数位置是用户点击的位置,根据您的需要使用该变量的值。

【讨论】:

  • 不幸的是位置不同...这里需要在 onItemClick 方法内的起始代码上重定向一些东西,如果无法获取 cbx 的位置...它是 listview 的子项,可能在这样 smb 就知道了……
【解决方案3】:

您是否尝试过以下方法?

@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

}

【讨论】:

  • 它不起作用,因为方法 onClick 无法到达位置,但好主意,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 2013-02-24
相关资源
最近更新 更多