你好。有一个表,该表按位置在ListView中显示数据。一切正常。但是,如果表中有空格,那么这将成为一个问题。如何通过表中的_id而不是按位置切换到所需的活动?我寻求帮助,我将非常感谢。非常感谢大家!下面是我的代码:
这是我的代码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);
}
这是我的代码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;
}
具有被单击项的位置(这是您唯一可以直接获得的位置,您可以使用:]从列表“报价”中获取报价。
final String desiredQuote = quotes.get(position);
然后,向您的数据库发出请求,您将可以获取所需的ID。类似于:
final Cursor cursor = database.rawQuery("SELECT _id FROM regiones where izbrannoe = 1 AND quote=?",new String [] {desiredQuote});
这是您要找的吗?