1

I apologize in advance, I am from Indonesia, so my English may not be good .. I just translate me through google translate

I'm making a android app associated with sqlite. the problem I have, I want to display data from the database according to the selected listview. application is an application that I made a tour guide.

while running the application, will be performing some of the category of existing attractions. if one of the selected categories, such as category: Nature, it will show a list of names that exist in nature in the form of a database listview.

I managed to get it up here.

after that, if I choose one of the name of nature, such as: Losari, it will show a description of Losari according to the database.

I am confused how to display as I described above .. please help me

this file alam.java for displaying listview whose data retrieved from the database

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) {


    }

}

This file deskAlam.java will display a description of the selected option in 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();
}   

}

please fix it if something goes wrong or less of the source code I mentioned above

4

1 回答 1

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);
于 2014-01-03T10:28:00.733 回答