如何获取它的ID ... SQLite SimplecursorAdapter?

时间:2014-10-19 14:19:48

标签: android sqlite simplecursoradapter

我试过但却无法得到它...... 我尝试像这样添加

new String[] {"name", "_id"}, // I want the value of _id
new int[] {R.id.textView1, id});

但仍然在努力工作

public class Veg extends SherlockActivity implements OnItemClickListener {

private Cursor data;
private DBManager db;
ListView lv;
int id;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nonveg);
    lv = (ListView) findViewById(R.id.list1);
    db = new DBManager(this, null, null, 0);
    data = db.getVeg(); // you would not typically call this on the main thread
    @SuppressWarnings("deprecation")
    ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.veg_item,
            data,
            new String[] {"name", "_id"},
            new int[] {R.id.textView1});
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(this);
}
@Override
protected void onDestroy() {
    super.onDestroy();
    data.close();
    db.close();
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int i, long l) {
    // TODO Auto-generated method stub
    //Toast.makeText(this, id, Toast.LENGTH_LONG).show();
    //Intent in = new Intent(Veg.this, Veg_View.class);
    //in.putExtra("id", String.valueOf(i)); /// ID need to passed here
    //startActivity(in);
}

我需要将此ID传递给新活动。 在新的活动中,我需要检索相应的数据

2 个答案:

答案 0 :(得分:0)

将数据发送到另一个活动

是正确的
Intent in = new Intent(Veg.this, Veg_View.class);
in.putExtra("id", String.valueOf(i)); /// ID need to passed here
startActivity(in);

在您的新活动中,只需使用此

即可
Intent in = getIntent();
Bundle bu = in.getExtras();
int id = in.getInt("id");

答案 1 :(得分:0)

public void onItemClick(AdapterView<?> arg0, View view, int i, long l)

最后一个参数是你想要的_id OnItemClick方法签名是     public void onItemClick(AdapterView parent,View view,int position,long id)

parent是适配器

view是点击项目的根视图

positionAdapter

上的位置

id CursorAdapters中与_id

匹配的项的适配器的ID