Android SQLite 数据库

发布于 2024-09-26 01:36:59 字数 2027 浏览 2 评论 0原文

我有使用 SQLite DB(包含两行的表)的应用程序,我需要接收所选列表视图项的 _ID 行,但不知道如何执行。所以,主要活动

public class Main extends ListActivity {
private RecipesData recipes;
private static int[] TO={0,R.id.row_text_id};
private static String[] FROM={ _ID, CATEGORY_NAME, };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    recipes=new RecipesData(this);
    try{
        Cursor cursor=getCategories();
        showCategories(cursor);
    }finally{
        recipes.close();
    }
    final ListView lv=getListView();
    lv.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> a, View v, int position, long id){
             AlertDialog.Builder adb=new AlertDialog.Builder(Main.this);
                  adb.setTitle("LVSelectedItemExample");
                 adb.setMessage("Selected Item is = "+((TextView)v).getText());
                  adb.setPositiveButton("Ok", null);
                  adb.show();
        }
    });
}
@Override
protected void onPause(){
    recipes.close();
    super.onPause();
}
@Override
protected void onStop(){
    recipes.close();
    super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case R.id.add_category_item:
         return true;
    }
    return false;
}

private void showCategories(Cursor cursor){
    SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.item,cursor,FROM,TO);
    setListAdapter(adapter);
}
private Cursor getCategories(){
    SQLiteDatabase db=recipes.getReadableDatabase();
    Cursor cursor=db.query(CATEGORY_TABLE, FROM, null, null, null, null, null);
    startManagingCursor(cursor);
    return cursor;
}}

I have application working with SQLite DB(table with two rows) I need to recieve _ID row of the selected listview Item, but don't know how to do it. So, main activity

public class Main extends ListActivity {
private RecipesData recipes;
private static int[] TO={0,R.id.row_text_id};
private static String[] FROM={ _ID, CATEGORY_NAME, };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    recipes=new RecipesData(this);
    try{
        Cursor cursor=getCategories();
        showCategories(cursor);
    }finally{
        recipes.close();
    }
    final ListView lv=getListView();
    lv.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> a, View v, int position, long id){
             AlertDialog.Builder adb=new AlertDialog.Builder(Main.this);
                  adb.setTitle("LVSelectedItemExample");
                 adb.setMessage("Selected Item is = "+((TextView)v).getText());
                  adb.setPositiveButton("Ok", null);
                  adb.show();
        }
    });
}
@Override
protected void onPause(){
    recipes.close();
    super.onPause();
}
@Override
protected void onStop(){
    recipes.close();
    super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case R.id.add_category_item:
         return true;
    }
    return false;
}

private void showCategories(Cursor cursor){
    SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.item,cursor,FROM,TO);
    setListAdapter(adapter);
}
private Cursor getCategories(){
    SQLiteDatabase db=recipes.getReadableDatabase();
    Cursor cursor=db.query(CATEGORY_TABLE, FROM, null, null, null, null, null);
    startManagingCursor(cursor);
    return cursor;
}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

jJeQQOZ5 2024-10-03 01:36:59

在你的 onItemClick 中

Cursor cursor = ((CursorAdapter) a.getAdapter()).getCursor();
String currentId = cursor.getString(0);

in your onItemClick

Cursor cursor = ((CursorAdapter) a.getAdapter()).getCursor();
String currentId = cursor.getString(0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文