Hello. There is a table from which data is displayed in ListView by position. Everything works great. But if there are spaces in the table, then this becomes a problem. How can I switch to the desired activity by _id in the table, and not by position? I ask for help, I will be very grateful. Thank you all very much! Below is my code:
This is my code MainActivity.java
this.listView = findViewById(R.id.listView);
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
List<String> quotes = databaseAccess.getQuotes();
databaseAccess.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, quotes);
this.listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(id==0){
Intent myintent = new
Intent(view.getContext(),MosObl.class);
startActivityForResult(myintent,0);
}
if(id==1){
Intent myintent = new
Intent(view.getContext(),Arhangelsk.class);
startActivityForResult(myintent,1);
}
if(id==2){
Intent myintent = new
Intent(view.getContext(),Astrahan.class);
startActivityForResult(myintent,2);
}
This is my code DatabasesAccess.java
public List<String> getQuotes() {
List<String> list = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT * FROM regiones where izbrannoe = 1", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
return list;
}
With the position of the clicked item (which is the only thing you will be able to get directly), you can get the quote from your list "quotes", using :
final String desiredQuote = quotes.get(position);
Then, making a request to your database you'll be able to get the desired id. Something like:
final Cursor cursor = database.rawQuery("SELECT _id FROM regiones where izbrannoe = 1 AND quote=?",new String [] {desiredQuote});
Is that what you're looking for?