Question

I have prepared the listview which contains one checkbox and one textbox in its each row by the help of Romain Guy's answer and Raghunandan's answer.

Its working fine if I check each checkboxes from top to bottom order( i.e showing each row data) but if I check the last one and its previous one it only show spaces.

My two classes:

ShowCurrentStatus.java

private AlertDialog leaveDialog(View view, int icon, String title,
        final String[] data) {
    final FilterAdapter mAdapter = new FilterAdapter(ShowCurrentStatus.this,R.layout.customleavetypefilter, data);
    AlertDialog dialog;
    AlertDialog.Builder builder = new AlertDialog.Builder(
            ShowCurrentStatus.this);
    builder.setIcon(icon);
    builder.setTitle(title);
    builder.setView(view);
    ListView mListView = (ListView) view
            .findViewById(R.id.listViewleavetypefilter);
    mListView.setCacheColorHint(Color.TRANSPARENT);
    mListView.setAdapter(mAdapter);
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < mAdapter.mCheckStates.size(); i++) {
                if (mAdapter.mCheckStates.get(i) == true) {

                    result.append(data[i]);
                    result.append("\n");
                } else {
                    result.append("hi");
                }

            }
            Toast.makeText(ShowCurrentStatus.this, result, Toast.LENGTH_SHORT).show();
        }
    });
    builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

    dialog = builder.create();
    return dialog;
}

FilterAdapter.java

public class FilterAdapter extends ArrayAdapter<String> implements  CompoundButton.OnCheckedChangeListener {

SparseBooleanArray mCheckStates;
private String[] mData;
private Context mContext;
int layoutResourceId;

public FilterAdapter(Context mContext,int layoutResourceId, String[] mData) {
    super(mContext, layoutResourceId, mData);
    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    this.mData = mData;
    mCheckStates = new SparseBooleanArray(mData.length);

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    MyViewHolder mMyViewHolder;

    if (view == null) {
        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
        view = inflater.inflate(layoutResourceId, parent, false);
        mMyViewHolder = new MyViewHolder(view);
        view.setTag(mMyViewHolder);
    } else {

        mMyViewHolder = (MyViewHolder) view.getTag();
    }
    mMyViewHolder.name.setText(mData[position]);
    mMyViewHolder.name.setTextColor(Color.CYAN);

    mMyViewHolder.check.setTag(position);
    mMyViewHolder.check.setChecked(mCheckStates.get(position, false));

    mMyViewHolder.check.setOnCheckedChangeListener(this);
    return view;
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}

private class MyViewHolder {
    TextView name;
    CheckBox check;

    MyViewHolder(View mView) {
        name = (TextView) mView
                .findViewById(R.id.textViewAddDataSelectMultiple);
        check = (CheckBox) mView.findViewById(R.id.checkBoxSelectMultiple);
    }
}

public boolean isChecked(int position) {
    return mCheckStates.get(position, false);
}

public void setChecked(int position, boolean isChecked) {
    mCheckStates.put(position, isChecked);
    notifyDataSetChanged();

}

public void toggle(int position) {
    setChecked(position, !isChecked(position));

}

The else part in mAdapter.mCheckStates.get(i) == true (i.e "hi") always executed if I checked from bottom to top order.

Can someone tell me where I did wrong here.

Thanks

Was it helpful?

Solution

Change your

for (int i = 0; i < mAdapter.mCheckStates.size(); i++) {

to

for (int i = 0; i < data.length; i++) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top