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

今、対応する_idフィールドのArrayListの 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;
}
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 に答える