Android 正确获取ListVew中行的_id

Android 正确获取ListVew中行的_id,android,sqlite,listview,simplecursoradapter,simpleadapter,Android,Sqlite,Listview,Simplecursoradapter,Simpleadapter,我有一个列表视图,它显示了一组来自SQLite表的值。首先,我使用了一个SimpleCursorAdapter来根据SQL查询中的游标填充ListView。我转而使用simpledapter,因为在将数据发送到列表视图之前,我必须操作/添加列表中的数据 使用SimpleCorsorAdapter点击一行后从ListView返回的id是数据库表中的正确id,但使用SimpleAdapter时,id看起来像是由ListView生成的,因为它与位置相同 我的桌子看起来像这样: public Curso

我有一个
列表视图
,它显示了一组来自SQLite表的值。首先,我使用了一个
SimpleCursorAdapter
来根据SQL查询中的游标填充
ListView
。我转而使用
simpledapter
,因为在将数据发送到
列表视图之前,我必须操作/添加列表中的数据

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

我的桌子看起来像这样:

public Cursor fetchDataAsCursor()
{
  return db.query("table_name", new String[] { "_id", "col1", "col2"}, null, null, null, null, null);
}
  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);
  }
\u id|col1 | col2 | col3

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

public Cursor fetchDataAsCursor()
{
  return db.query("table_name", new String[] { "_id", "col1", "col2"}, null, null, null, null, null);
}
  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);
  }
使用
SimpleCursorAdapter
填充
列表视图的方法如下所示:

public Cursor fetchDataAsCursor()
{
  return db.query("table_name", new String[] { "_id", "col1", "col2"}, null, null, null, null, null);
}
  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是ok的:

  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);
  }
现在切换到
simpledapter

生成
列表的代码

  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
无法拾取列表中的
\u id
值。我猜它会像使用
SimpleCursorAdapter

是否有方法操纵
列表视图中的行id
,以确保它与数据库表中的
\u id
键具有相同的值

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

编辑:

我想出来了。我必须创建自己的
simpledapter
子类,它覆盖
public long-getItemId(int-position)

公共类MyListAdapter扩展了SimpleAdapter
{
私有最终字符串ID=“_ID”;
公共PunchListAdapter(上下文,列表>数据,int资源,字符串[]从,int[]到)
{
超级(上下文、数据、资源、从、到);
}
@凌驾
公共长getItemId(int位置)
{
对象o=获取项目(位置);
长id=位置;
if(o映射的实例)
{
Map m=(Map)o;
如果(m.containsKey(ID))
{
o=m.get(ID);
if(o长实例)
id=(长)o;
}
} 
返回id;
}
}

使用SimpleAdapter处理游标是一种糟糕的方法。您应该实现CursorAdapter

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

而不仅仅是在活动中将适配器设置为listview

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

这是使用SimpleAdapter处理游标的糟糕方法。您应该实现CursorAdapter

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

而不仅仅是在活动中将适配器设置为listview

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