【问题标题】:Custom ArrayList searching issue?自定义 ArrayList 搜索问题?
【发布时间】:2012-06-30 23:11:55
【问题描述】:

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

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

【问题讨论】:

    标签: java android list arraylist


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我想你忘了mCursor.moveToFirst();

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

      【讨论】:

      • 他正在处理自定义对象,因此他必须覆盖 equals。
      猜你喜欢
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多