1

我已经实现了带有复选框和文本视图的自定义列表视图。当列表视图不在滚动时它工作正常,但是如果行增加,那么当我单击复选框时,列表视图处于滚动视图中,选中标记不会保留。这是我的代码,如果该代码有任何问题,请建议我

public class ArchivedAdapter extends ArrayAdapter<MessageBean> {
final ArrayList<MessageBean> updatedList = new ArrayList<MessageBean>();
private List<MessageBean> list;
private TextView officenameView, sentOnView, messageView;
public final static String MY_ACTION = "com.android.threeptalk.broadcast";
private boolean isDeleted = false;
int count = 0;
HashMap<Integer, CheckBox> checkList = new HashMap<Integer, CheckBox>();

public ArchivedAdapter(Context context, int resourseID,
        List<MessageBean> list) {
    super(context, resourseID, list);
    this.list = list;

}

@Override
public int getCount() {
    return list.size();
}

@Override
public MessageBean getItem(int arg0) {
    return list.get(arg0);
}

@Override
public long getItemId(int arg0) {
    return arg0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View row = convertView;
    LayoutInflater inflater = (LayoutInflater) this.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.archievedmsgrow, parent, false);
    row.setId(position);
    Object obj = getItem(position);
    if (obj instanceof MessageBean) {
        final MessageBean message = (MessageBean) obj;
        CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox);
        officenameView = (TextView) row.findViewById(R.id.officename);
        sentOnView = (TextView) row.findViewById(R.id.time);
        messageView = (TextView) row.findViewById(R.id.message);
        officenameView.setText(message.getOfficeName());
        checkList.put(position, checkBox);
        checkBox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                CheckBox c = (CheckBox) v;
                if (c.isChecked()) {
                    c.setChecked(true);
                    count++;
                    updatedList.add(message);


                } else {
                    c.setChecked(false);
                    count--;
                    updatedList.remove(message);
                    updatedList.size());
                }
                checkedCount(count, updatedList);
            }
        });

        row.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rowclicked(message, position);

            }
        });
        sentOnView.setText(Common.getAppropriateTimeUsingTimeZone(message
                .getmDate()));
        if (message.getMessage() != null) {

            messageView.setText((message.getMessage()));

        } else {
            if (message.getMessageType().equals("OPTION")) {
                if (message.getOptions() != null
                        && message.getOptions().size() > 0) {
                    String data = message.getOptions().get(0);
                    if (data != null && !data.equals("")) {

                        messageView.setText(data);
                    }
                }
            }
        }

    }
    return row;
}

protected void messageSelected() {

}


protected void rowclicked(MessageBean message, int position) {
    // TODO Auto-generated method stub

}

protected void checkedCount(int count2, ArrayList<MessageBean> updatedList) {

}
}

`

4

3 回答 3

4

试试这个带有复选框滚动问题的示例ListView

于 2013-02-13T05:09:02.810 回答
3

似乎您正在检查其onClickListener()中复选框的检查状态。

首先定义一个HashmapSparseBooleanArray来保存您的选择。

private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();

然后将以下代码添加到您的getView()方法中。

 CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox);
    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            mSelection.put(position, isChecked);
        }
    });

    if (mSelection.get(position) != null) {
        if (mSelection.get(position))
            checkBox.setChecked(true);
        else
            checkBox.setChecked(false);
    }
于 2013-02-13T05:22:00.793 回答
1

试试这个,假设您为 ListView 使用自定义适配器,您将能够为适配器的 getView() 中的每个项目设置标签,

    public View getView(int position, View convertView, ViewGroup parent) {
    EvenViewHolder holdereven;
        if (convertView == null) {
            convertView = mInfaltor.inflate(R.layout.schedule_adapter_view,
                    null);
            holdereven = new EvenViewHolder();
            holdereven.time = (TextView) convertView
                    .findViewById(R.id.time);
            holdereven.title = (TextView) convertView
                    .findViewById(R.id.title);
            holdereven.des = (TextView) convertView.findViewById(R.id.des);
            convertView.setTag(holdereven);

        } else {
            holdereven = (EvenViewHolder) convertView.getTag();
        }

 static class EvenViewHolder {
    TextView time, title, des;

}
  }
于 2013-02-13T05:01:10.930 回答