0

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

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

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

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

我做了什么:活动布局:

<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 上检查过它

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

4

1 回答 1

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

wheremListener是一个单一的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自己调用该方法,这是一个只能由系统调用的生命周期方法。

于 2012-10-25T11:37:25.840 回答