harshit
harshit

Reputation: 3856

Items selected in a list are not shown as selected

Now this might be a difficult to understand problem.

What I actually wanted : To get a populated list of distinct callers from call log, let user select as manny he wants and then blacklist them by tapping on a button.

Thus I concluded that what I wanted is following

Concluded What I wanted : To have an activity with a header button and a list of items. From the list of items, user can select any number of the items(implemented this by using checkboxes, will show how I did it later) and then perform an action by selecting header button.

What I did : The activity layout:

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

The row layout :

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

In the activity:

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

};
}

What the problem is : When I select an item and scroll down and come back by scrolling up the selected item check on the selected item gets changed. Although when I look at the hashset toBlacklistNumbers the number I originally selected is there only. That means when I tap on the header button it does blacklist it. To remove it from the hashset I have to select it first, then unselect it. But this is not what I want.

I dont want the item to get unselected when I scroll down the list. This is obviously not the problem with the android version as I have checked it on Android 2.2 and Android 4.1

Update : By selected item getting changed, I meant that the item I selected is not selected any more, instead any item above it or below it is selected. Also, When I scroll down, many items in the list below are also selected automatically

Upvotes: 0

Views: 262

Answers (1)

user
user

Reputation: 87074

You aren't handling the recycling right, you aren't setting the status of the CheckBox based on the data in 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;
            }
        }

where mListener is a single OnCheckedChangeListener so you don't create each time one when the user scrolls:

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

Also, don't call the onResume method yourself, that is a lifecycle method to be called only by the system.

Upvotes: 1

Related Questions