gpt4 book ai didi

java - 自定义 ArrayList 搜索问题?

转载 作者:行者123 更新时间:2023-11-29 05:58:05 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于java - 自定义 ArrayList 搜索问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11261472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com