正确获取ListView中行的id

时间:2012-01-06 12:50:48

标签: android sqlite listview simplecursoradapter simpleadapter

我有一个ListView,它显示了SQLite表中的一组值。首先,我使用SimpleCursorAdapter根据SQL查询中的Cursor填充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);
}

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

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

1 个答案:

答案 0 :(得分:0)

使用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);
      }
    }

只需在您的活动中将适配器设置为listview即可。

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