列表中选择的项目不会显示为已选中

时间:2012-10-25 09:28:58

标签: android listview android-listview

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

我真正想要的是什么:要从通话记录中获取不同来电者的填充列表,请让用户选择他想要的manny,然后点击按钮将其列入黑名单。

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

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

我做了什么:活动布局:

<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;
        }
    }

};
}

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

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

更新:根据所选项目的更改,我的意思是我选择的项目不再被选中,而是选中它上面或下面的任何项目。此外,当我向下滚动时,下面列表中的许多项目也会自动选择

1 个答案:

答案 0 :(得分:1)

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

@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,因此当用户滚动时,您不会每次创建一个{/ p>

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方法,这是一种只能由系统调用的生命周期方法。