【问题标题】:Items selected in a list are not shown as selected列表中选择的项目未显示为已选择
【发布时间】:2012-10-15 10:58:33
【问题描述】:

现在这可能是一个难以理解的问题。

我真正想要的:要从通话记录中获取不同呼叫者的填充列表,让用户选择他想要的数量,然后通过点击按钮将他们列入黑名单。

因此我得出结论,我想要的是跟随

结束我想要的: 有一个带有标题按钮和项目列表的活动。从项目列表中,用户可以选择任意数量的项目(通过使用复选框来实现,稍后将显示我是如何做到的),然后通过选择标题按钮执行操作。

我做了什么:活动布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
    <LinearLayout android:id="@+id/add_btn_layout" android:layout_width="match_parent" android:layout_height="50dp" android:padding="2dp" >
        <Button android:id="@+id/btn_blacklist_sender" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="21dp" >
        </Button>
    </LinearLayout>
    <ListView android:id="@+id/list_chose_to_blacklist" android:layout_width="wrap_content" android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

行布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="2dp" >
<CheckBox android:id="@+id/isDelete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="43dp" android:paddingRight="3dp" />
</LinearLayout>

在活动中:

public class CallerChooseToBlacklistActivity extends Activity {

Button btnBlacklistCaller;
ListView listCallers;
HashSet<String> toBlacklistNumbers = new HashSet<String>();
String caller = "Sender";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chose_to_blacklist);

    btnBlacklistCaller = (Button) findViewById(R.id.btn_blacklist_sender);
    btnBlacklistCaller.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            BlacklistNumberHelper helper = new BlacklistNumberHelper(
                    getApplicationContext());
            for (String number : toBlacklistNumbers) {
                helper.insert(number, DbHelper.TABLE_CALL_BLACKLIST,
                        DbHelper.C_CALL_BLACKLIST_NO);
            }
            helper.close();
            onResume();
        }
    });
    btnBlacklistCaller.setText(getString(R.string.btn_blacklist_caller));

    listCallers = (ListView) findViewById(R.id.list_chose_to_blacklist);
}

@Override
protected void onResume() {
    super.onResume();

    String[] projection = new String[] { CallLog.Calls._ID,
            CallLog.Calls.NUMBER };
    Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
            projection, null, null, null);

    for (int idx = 0; idx < cursor.getColumnCount(); idx++) {
        Log.d(MainActivity.TAG, idx + ":" + cursor.getColumnName(idx));
    }

    HashSet<String> distinctCallers = new HashSet<String>();
    ArrayList<String> allreadyBlacklisted = (new BlacklistNumberHelper(
            getApplicationContext())).getAllNumbers(
            DbHelper.TABLE_CALL_BLACKLIST, DbHelper.C_CALL_BLACKLIST_NO);
    if (cursor.moveToFirst()) {
        for (int i = 0; i < cursor.getCount(); i++) {
            try {
                String address = cursor.getString(
                        cursor.getColumnIndexOrThrow("number")).toString();
                boolean isPresent = false;
                for (String no : allreadyBlacklisted)
                    if (no.equalsIgnoreCase(address)) {
                        isPresent = true;
                        break;
                    }
                if (!isPresent)
                    distinctCallers.add(address);
                cursor.moveToNext();
            } catch (IllegalArgumentException e) {
            }
        }
    }

    ArrayList<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

    for (String address : distinctCallers) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(caller, address);
        fillMaps.add(map);
    }

    String[] from = { caller };
    int[] to = { R.id.isDelete };
    SimpleAdapter cursorAdapter = new SimpleAdapter(this, fillMaps,
            R.layout.row_blacklist, from, to);
    cursorAdapter.setViewBinder(VIEW_BINDER);
    listCallers.setAdapter(cursorAdapter);
}

// custom binder to bind columns customally
final ViewBinder VIEW_BINDER = new ViewBinder() {
    public boolean setViewValue(View view, Object arg1, String arg2) {
        if (view.getId() == R.id.isDelete) {
            CheckBox cb = (CheckBox) view;
            cb.setText(arg2);
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    String text = buttonView.getText().toString();
                    if (isChecked)
                        toBlacklistNumbers.add(text);
                    else if (toBlacklistNumbers.contains(text))
                        toBlacklistNumbers.remove(text);
                }
            });
            return true;
        } else {
            return false;
        }
    }

};
}

问题是什么:当我选择一个项目并向下滚动并通过向上滚动所选项目返回时,所选项目上的检查发生了变化。虽然当我查看哈希集toBlacklistNumbers 时,我最初选择的数字只是在那里。这意味着当我点击标题按钮时,它会将其列入黑名单。要从哈希集中删除它,我必须先选择它,然后取消选择它。但这不是我想要的。

我不希望在我向下滚动列表时取消选择该项目。这显然不是 android 版本的问题,因为我在 Android 2.2 和 Android 4.1 上检查过它

更新:选择的项目发生变化,我的意思是我选择的项目不再被选择,而是选择它上面或下面的任何项目。另外,当我向下滚动时,下面列表中的许多项目也会被自动选中

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

    你没有处理回收权,你没有根据toBlacklistNumbers中的数据设置CheckBox的状态:

    @Override
    public boolean setViewValue(View view, Object arg1, String arg2) {
                if (view.getId() == R.id.isDelete) {
                    CheckBox cb = (CheckBox) view;
                    cb.setText(arg2);
                    String data = (String) arg1;
                    if (toBlacklistNumbers.contains(data)) {
                        cb.setChecked(true);
                    } else {
                        cb.setChecked(false);
                    }               
                    cb.setOnCheckedChangeListener(mListener);
                    return true;
                } else {
                    return false;
                }
            }
    

    其中mListener 是单个OnCheckedChangeListener,因此您不会在用户滚动时每次都创建一个:

    private OnCheckedChangeListener mListener = new OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                String text = buttonView.getText().toString();
                if (isChecked)
                    toBlacklistNumbers.add(text);
                else if (toBlacklistNumbers.contains(text))
                    toBlacklistNumbers.remove(text);
            }
        };
    

    另外,不要自己调用onResume方法,这是一个只有系统才能调用的生命周期方法。

    【讨论】:

    • 那么,我应该做的是:删除 onResume 调用,添加 setViewValue 函数覆盖并制作一个 OnCheckedChangeLIsener 对吗?我会试试看。可能需要一些时间,谢谢回答
    • 我应该删除自定义的 VIEW_BINDER 还是保持原样
    • @harshit 您无法从代码中删除活页夹。只需将setViewValue 的内容替换为我回答中的代码即可。
    • Activity 类没有这样的方法setViewValue 可以覆盖。我该怎么办
    • @harshit 我指的是ViewBindersetViewValue方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多