从列表中的数据库获取ID onclick [英] Get Id from database in a listview onclick

查看:99
本文介绍了从列表中的数据库获取ID onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个此类,该类从数据库获取数据并显示在列表视图中.

I have this class, that gets the data from the database and shows in a listview.

是否有一种方法可以从数据库中获取ID并进行烘烤?

Is there a way to get the Id from the database and toast it?

现在,烤面包正在返回: android.database.sqlite.SQLiteCursor@4180c088

Now the toast is returning: android.database.sqlite.SQLiteCursor@4180c088

 public class AndroidSQLite extends Activity { 

 private SQLiteAdapter
 mySQLiteAdapter;

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     ListView listContent = (ListView)findViewById(R.id.contentlist);

     mySQLiteAdapter = new SQLiteAdapter(this);
     mySQLiteAdapter.openToRead();

     Cursor cursor = mySQLiteAdapter.queueAll();
     startManagingCursor(cursor);

     String[] from = new String[]{SQLiteAdapter.KEY_NOME,SQLiteAdapter.KEY_ID};
     int[] to = new int[]{R.id.text,R.id.id};

     SimpleCursorAdapter cursorAdapter =
      new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

     listContent.setAdapter(cursorAdapter);

     listContent.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(AdapterView<?> parent, View view, int position,
                 long id) {
                //GET ID FROM DATABASE
                Toast.makeText(getBaseContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
                //THIS TOAST IS RETURNING "android.database.sqlite.SQLiteCursor@4180c088"
         }
     });

     mySQLiteAdapter.close();
 } }

推荐答案

只需使用 onItemClick() id 参数:

Toast.makeText(getBaseContext(), id + "", Toast.LENGTH_LONG).show();


为了将来参考,您当前正在尝试将整个Cursor用作字符串,您想要选择一个像这样的列:


For future reference you are currently trying to use an entire Cursor as a String, you want to select a column like this:

Toast.makeText(getBaseContext(), parent.getItemAtPosition(position).getLong(0), Toast.LENGTH_LONG).show();

(对于SimpleCursorAdapter,第一列始终是 _id 列,因此是 getLong(0).)

(For a SimpleCursorAdapter the first column is always the _id column, hence getLong(0).)

这篇关于从列表中的数据库获取ID onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆