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;
}}