Android的 - 获取有关复选框相关的ListView行被更改 [英] Android - Getting the relevant ListView row on CheckBox changed

查看:213
本文介绍了Android的 - 获取有关复选框相关的ListView行被更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有我的Andr​​oid应用程序一个ListView有一个侧面的复选框。

I currently have a ListView in my Android app that has a CheckBox on the side.

在该复选框被改变我想从列表视图行的一些信息,按钮的,然后一直存在的,但是,我不能工作了如何从onClickChanged听者获得行数据。

When the checkbox is changed I want to get some info from the listview row that the button is in and then persist it, however, I can not work out how to get the row data from the onClickChanged listener.

我的列表视图中使用自定义的JSONObject listadapter填充,我希望它是类似的工作出了什么行什么检测onLongTouch当选择 - 例如下面是code我用它来获取相关的JSON对象在longTouch选择的行:

My listview is populated using a custom JSONObject listadapter, I am hoping it is something similar to working out what row is selected when detecting the onLongTouch - for example below is the code I use to get the relevant JSON object for the row selected on a longTouch:

public boolean onContextItemSelected(MenuItem item) { 
        //get row selected information
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        JSONObject selected = (JSONObject) getListAdapter().getItem(menuInfo.position);

上面显然是我的ListActivity内,并且我onCheckChanged听者不会在相同的活性(因为它是该行中所定义,而不是表)

The above is obviously within my ListActivity, and my onCheckChanged listener would not be in the same activity (as it is defined in the row rather than the list)

任何帮助将是AP preciated!

Any help would be appreciated!

推荐答案

要解决这个问题的一种可能的方式是结合发生时,一些信息附加到复选框。

One possible way to solve this is to attach some info to the checkbox when binding occurs.

在您的适配器,添加(或修改)bindView覆盖这样添加一个标签thatcontains您需要的上下文信息:

In your Adapter, add (or modify) the bindView override like this to add a tag thatcontains the context information you need:

    @Override
    public void bindView( View view, Context context, Cursor cursor){
        CheckBox checker = (CheckBox)view.findViewById(R.id.checkbox);

        checker.setOnCheckedChangeListener(MyActivity.this);
        checker.setTag(Long.valueOf(cursor.getLong(cursor.getColumnIndex(BaseColumns._ID))));
        // call super class for default binding
        super.bindView(view,context,cursor);
    }

(或附加任何其他值的复选框,可以帮助您识别的项目,例如属性格式名称或索引)

(or attach any other value to the checkbox that helps you identify the item, e.g. the propery name or index)

然后,在onCheckedChanged,你可以参考此信息识别已更改的项目:

Then, in the onCheckedChanged, you can refer to this information to identify the item that was changed:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    Long id = (Long) buttonView.getTag();
    if ( id!=null)
        Log.d(TAG, "onCheckedChanged id="+id);

}

这篇关于Android的 - 获取有关复选框相关的ListView行被更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆