自定义ArrayList搜索问题?

时间:2012-06-29 12:06:45

标签: java android list 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;
    }
}

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

loglist = new ArrayList<LogList>();

现在我正在添加项目

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

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

我用这个作为索引返回 -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;
}

2 个答案:

答案 0 :(得分:2)

  

indexOf ArrayList的metod不能用于自定义对象,除非   直到他们覆盖

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

答案 1 :(得分:1)

我想你忘记了mCursor.moveToFirst();

LogList ll = new LogList();
mCursor.moveToFirst(); <<<add here...
ll._id = Integer.parseInt(mCursor.getString(mCursor.getColumnIndex("_id")));