我正在制作与sqlite相关联的Android应用。 我有问题,我想根据所选的列表视图显示数据库中的数据。 申请是我制作导游的应用程序。
在运行应用程序时,将会执行某些类别的现有景点。如果其中一个选定的类别,例如category:Nature,它将以数据库列表视图的形式显示自然界中存在的名称列表。我设法把它弄到了这里。
之后,如果我选择其中一个自然名称,例如:Losari,它将根据数据库显示Losari的描述。
我很困惑如上所述如何显示.. 请帮帮我
此文件alam.java用于显示从数据库中检索数据的listview
dbadapter db;
protected Cursor cursor;
protected ListAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alam);
final ListView dftAlam = (ListView)findViewById(R.id.lv);
final dbadapter db = new dbadapter(this);
ArrayList<DFT_ALAM> dft_alam = db.getAllAlam();
dftAlam.setAdapter(new MyAdapter(this, dft_alam));
dftAlam.setOnItemClickListener(new OnItemClickListener() {
private String nama;
private String alamat;
private String desk;
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String selectedItem = (String) dftAlam.getItemAtPosition(arg2);
String query = "SELECT nama, alamat, desk FROM wisata WHERE nama = '" + selectedItem + "'";
SQLiteDatabase dbs = db.getReadableDatabase();
Cursor result = dbs.rawQuery(query, null);
result.moveToFirst();
nama = result.getString(result.getColumnIndex("nama"));
alamat = result.getString(result.getColumnIndex("alamat"));
desk = result.getString(result.getColumnIndex("desk"));
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, null, null, deskAlam.class);
startActivity(intent);
}
});
}
public void onClick(View v) {
}
}
此文件deskAlam.java将在listview
中显示所选选项的说明dbadapter db;
TextView nama = null;
TextView alamat = null;
TextView desk = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.desk);
TextView nama = (TextView)findViewById(R.id.nm);
TextView alamat = (TextView)findViewById(R.id.alm);
TextView desk = (TextView)findViewById(R.id.desk);
dbadapter db = new dbadapter (this);
db.openDataBase();
Cursor c = (Cursor) db.getAllAlam();
if (c.moveToFirst()){
do{
tampil(c);
}while (c.moveToNext());
}
}
public void tampil(Cursor c) {
// TODO Auto-generated method stub
Toast.makeText(this, c.getString(4) + "\n" +
c.getString(5) + "\n" +
c.getString(6), Toast.LENGTH_LONG).show();
}
}
如果出现问题或者我上面提到的源代码较少,请修复它
答案 0 :(得分:0)
您需要在onItemClick中执行以下操作:
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(ThisActivity.this, NextActivity.class);
intent.putExtra("chosen", position);
startActivity(intent);
}
});
以及如何在下一个意图中获取数据:
int chosenPosition;
YourObject chosenObject;
Bundle extras = getIntent().getExtras();
if (extras != null) {
//Here you get the id from the item
chosenPosition= (int)extras.getInt("chosen");
}
//Here you use the id to get the object from your database or whatever
chosenObject = Database.getYourObject(chosenPosition+ 1);