【问题标题】:Getting the _id of the rows in ListVew right正确获取 ListView 中行的 _id
【发布时间】:2012-02-04 04:41:33
【问题描述】:

我有一个ListView,它显示了 SQLite 表中的一组值。首先,我使用 SimpleCursorAdapter 根据 SQL 查询中的游标填充 ListView。我改用SimpleAdapter,因为我必须在列表中操作/添加数据,然后再将其发送到ListView

使用SimpleCursorAdapter,在点击一行后从ListView返回的id是数据库表中的正确ID,但使用SimpleAdapter,id看起来就像是由ListView生成的,因为它是与职位相同。

我的桌子是这样的:

_id | col1 | col2 | col3

SimpleCursorAdapter 生成光标的方法如下所示:

public Cursor fetchDataAsCursor()
{
  return db.query("table_name", new String[] { "_id", "col1", "col2"}, null, null, null, null, null);
}

使用SimpleCursorAdapter填充ListView的方法如下:

  private void simpleFillData()
  {
    Cursor cursor = dbAdapter.fetchDataAsCursor();
    startManagingCursor(cursor);

    String[] from = new String[] {"col1", "col2"};
    int[] to = new int[] {R.id.col1, R.id.col2};

    SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
        R.layout.list_row, cursor, from, to);
    setListAdapter(notes);
  }

这很好用,因为在以下方法中返回的 id 是可以的:

  protected void onListItemClick(ListView l, View v, int position, long id)
  {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, DetailActivity.class);
    i.putExtra("_id", id);
    startActivityForResult(i, ACTIVITY_EDIT);
  }

现在切换到SimpleAdapter

生成List的代码:

  public ArrayList<HashMap<String, Object>> getList()
  {
    ArrayList <HashMap<String, Object>> list = new ArrayList();

    c = fetchDataAsCursor();
    c.moveToFirst();
    for(int i = 0; i < c.getCount(); i++)
    {    
      HashMap<String, Object> h = new HashMap<String, Object>();
      h.put("_id", c.getLong(0));
      h.put("col1", c.getString(1));
      h.put("col2", c.getString(2));

      //This is the extra column
      h.put("extra", calculateSomeStuff(c.getString(1), c.getString(2));
      list.add(h);
      c.moveToNext();
    }

    return list;
  }

然后对于填充ListView的方法:

private void fillData()
  {
    ArrayList<HashMap<String, Object>> list = dbAdapter.getList();
    String[] from = new String[] {"col1", "col2", "extra"};
    int[] to = new int[] {R.id.col1, R.id.col2, R.id.extra};
    SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.list_row, from, to);
    setListAdapter(notes);
  }

在最后一种方法中,ListView 无法获取列表中的 _id 值。我猜想它会像使用SimpleCursorAdapter 时那样自动执行此操作

有没有办法操作ListView 中行的id 以确保它与数据库表中的_id 键具有相同的值?

(所有代码示例都大大简化了)

编辑:

我想通了。我必须创建自己的 SimpleAdapter 子类,它会覆盖 public long getItemId(int position)

public class MyListAdapter extends SimpleAdapter
{
  private final String ID = "_id";
  public PunchListAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
  {
    super(context, data, resource, from, to);
  }

  @Override
  public long getItemId(int position)
  {
    Object o = getItem(position);
    long id = position;
    if(o instanceof Map)
    {
      Map m = (Map)o;
      if(m.containsKey(ID))
      {
        o = m.get(ID);
        if(o instanceof Long)
          id = (Long)o;
      }
    } 
    return id;
  }
}

【问题讨论】:

    标签: android sqlite listview simplecursoradapter simpleadapter


    【解决方案1】:

    使用 SimpleAdapter 处理光标是一种不好的方式。你应该实现 CursorAdapter。

    public class MyCursorAdapter extends CursorAdapter
    {
          LayoutInflater inflater;
          public MyCursorAdapter(Context context, Cursor c) {
            super(context, c);
            inflater = LayoutInflater.from(context);
          }
    

     @Override
              public void bindView(View view, Context context, Cursor cursor) {
                //cursor is already setted to requared position, just get your column
                TextView tv1 = (TextView)view.findViewById(R.id.textView1);
                TextView tv2 = (TextView)view.findViewById(R.id.textView2);
                tv1.setText(cursor.getString(1));
                tv2.setText(cursor.getString(2));
                viev.addOnClickListener(new OnClickListener{
                     public void onClick(View v){
                        ...
                        cursor.getLong(0);
                        ...
                     }
                });       
              }
    

    @Override
          public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return inflater.inflate(R.layout.my_raw_view, parent, false);
          }
        }
    

    不仅仅是在您的活动中将适配器设置为列表视图。

    Cursor cursor = fetchDataAsCursor();
        ListView myListView = (ListView)findViewById(R.id.my_list_view);
        myListView.setAdapter(new MyCursorAdapter(this,cursot));
    

    【讨论】:

      猜你喜欢
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多