1

我创建了一个如下所示的数据结构

public class LogList {
    public int _id;
    public boolean Checked;
    public LogList(int name, boolean status) {
        this._id = name;
        this.Checked = status;
    }
    public LogList(int name) {
        this._id = name;
    }
    public LogList() {
    }
    public int get_id() {
        return _id;
    }
    public void set_id(int _id) {
        this._id = _id;
    }
    public boolean isChecked() {
        return Checked;
    }
    public void setChecked(boolean checked) {
        Checked = checked;
    }
}

现在我已经使用这个数据结构创建了一个 ArrayList

loglist = new ArrayList<LogList>();

现在我正在向它添加项目

l = new 
LogList(Integer.parseInt(mCursor.getString(mCursor.getColumnIndex("_id"))),false);                          
loglist.add(l);

现在我想在ArrayList中的 Checked 字段的当前值中搜索相应的_id字段我怎么能找到它

我使用它返回-1作为索引。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    mCursor.moveToPosition(position);
    View Lv = null;
    try {
    if (convertView == null) {
    LayoutInflater vi = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = vi.inflate(R.layout.loglist, parent, false);
    Lv = convertView;
    } else {
    Lv = (TableRow) convertView;
    _id = (TextView) Lv.findViewById(R.id.txt_id);
    _title = (TextView) Lv.findViewById(R.id.txt_title);
    _sym = (TextView) Lv.findViewById(R.id.txt_sym);
    _amt = (TextView) Lv.findViewById(R.id.txt_amt);
        _date = (TextView) Lv.findViewById(R.id.txt_date);
    _time = (TextView) Lv.findViewById(R.id.txt_time);
    _remarks = (TextView) Lv.findViewById(R.id.txt_remark);
    _sym.setText("Rs.");
         chk = (CheckBox) Lv.findViewById(R.id.chk);
    _id.setText(mCursor.getString(mCursor.getColumnIndex("_id")));
    _title.setText(mCursor.getString(mCursor
        .getColumnIndex("Title")));
    _amt.setText(mCursor.getString(mCursor.getColumnIndex("Amt")));
    _date.setText(mCursor.getString(mCursor.getColumnIndex("Date")));
    _time.setText(mCursor.getString(mCursor.getColumnIndex("Time")));
    _remarks.setText(mCursor.getString(mCursor
            .getColumnIndex("remark")));
    boolean status = true;

/// this snippet is used to search item in list

    LogList ll = new LogList();
    ll._id = Integer.parseInt(mCursor.getString(mCursor
        .getColumnIndex("_id")));
    int index = myList.indexOf(ll);
    LogList myLogList = new LogList();
    myLogList = myList.get(index);
    if (myLogList.isChecked()) {
        status = true;
    } else {
            status = false;
    }
                chk.setChecked(status);
    }
    } catch (Exception ex) {
            ex.printStackTrace();
    }
return Lv;
}
4

2 回答 2

2

ArrayList 的 indexOf 方法不适用于自定义对象,除非它们覆盖

public boolean equals(Object obj)

您必须在 LogList 中覆盖 equals ,然后只有 ArrayList 会返回正确的索引。

因此,将您的代码更改为以下。

public class LogList {
    public int _id;
    public boolean Checked;

    public LogList(int name, boolean status) {
        this._id = name;
        this.Checked = status;
    }

    public LogList(int name) {
        this._id = name;
    }

    public LogList() {
    }

    public int get_id() {
        return _id;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public boolean isChecked() {
        return Checked;
    }

    public void setChecked(boolean checked) {
        Checked = checked;
    }

    @Override
    public boolean equals(Object obj) {
        if (this._id == ((LogList) obj)._id)
            return true;
        return false;
    }
}
于 2012-06-29T12:24:45.497 回答
1

我想你忘了mCursor.moveToFirst();

LogList ll = new LogList();
mCursor.moveToFirst(); <<<add here...
ll._id = Integer.parseInt(mCursor.getString(mCursor.getColumnIndex("_id")));
于 2012-06-29T12:10:02.203 回答