我已经使用ArrayAdapter从数据库中填充了listview,当我单击该项目时,我想获取该列表项目的数据库行ID。我已经搜索了许多小时,但是由于我是初学者,所以无法获得答案。
这是我的main.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=findViewById(R.id.quote_list);
registerForContextMenu(listView);
DatabaseAccess datac= DatabaseAccess.getInstance(getApplicationContext());
datac.open();
final ArrayList<String> thelist = new ArrayList<>();
Cursor data=datac.getquotes();
while(data.moveToNext()){
thelist.add(data.getString(2));
ListAdapter listAdapter = new ArrayAdapter<>(this,R.layout.textcenter,R.id.textitem,thelist);
listView.setAdapter(listAdapter);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater menuInflater=getMenuInflater();
menuInflater.inflate(R.menu.my_contextual_menu,menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
return super.onContextItemSelected(item);
}
}
答案 0 :(得分:0)
以这种方式尝试,
1.单击列表视图的项目时,将获得ID。
2.从该索引中获取字符串值,然后调用一个方法,在该方法中,将收集的字符串作为参数发送,并在该方法中触发查询,例如
const image = this.props.details.image? this.props.details.image : '';