1

I've created a data structure like below

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

Now i;ve created an ArrayList using this data structure

loglist = new ArrayList<LogList>();

Now i am adding items to it

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

Now I want to search the current value of Checked field in ArrayList for the corresponding _id field How i can find it

I used this one it returns -1 as index.

@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 Answers 2

2

indexOf metod of ArrayList will not work with custom Objects unless and until they override

public boolean equals(Object obj)

You must override equals in LogList then only ArrayList will return you correct index.

So change your code to following.

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

i think you forgot mCursor.moveToFirst();

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

1 Comment

He is dealing with Custom Objects so he must override equals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.