gpt4 book ai didi

java - 如何不按位置而是按sqlite表中的_id切换到新的Activity

转载 作者:行者123 更新时间:2023-12-01 23:33:16 27 4
gpt4 key购买 nike

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

最佳答案

通过单击项目的位置(这是您能够直接获取的唯一内容),您可以使用以下方法从列表“quotes”中获取报价:

final String desiredQuote = quotes.get(position);

然后,向您的数据库发出请求,您将能够获得所需的 ID。像这样的东西:

final Cursor cursor = database.rawQuery("SELECT _id FROM regiones where izbrannoe = 1 AND quote=?",new String [] {desiredQuote});

这就是你要找的吗?

关于java - 如何不按位置而是按sqlite表中的_id切换到新的Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58283844/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com