我提前道歉,我来自印度尼西亚,所以我的英语可能不好。。我只是通过谷歌翻译
我正在制作一个与sqlite相关的android应用程序。我遇到的问题是,我想根据所选的列表视图显示数据库中的数据。申请书是我做导游的申请书。
在运行该应用程序时,将执行一些现有景点类别。如果选择了其中一个类别,例如category:Nature,它将以数据库列表视图的形式显示自然界中存在的名称列表。
我设法把它弄上来了。
之后,如果我选择一个自然的名字,比如:Losari,它会根据数据库显示对Losari的描述。
我对如何如上所述进行显示感到困惑。。请帮我
这个文件是alam.java,用于显示从数据库检索到其数据的列表视图
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将在列表视图中显示所选选项的描述
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();
}
}
如果我上面提到的源代码出现问题或更少,请修复它
以下是您需要在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);