Android SQLite 数据库
我有使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的 onItemClick 中
in your onItemClick