2013-10-11 69 views
0

我有這個列表視圖從SQLite瀏覽器檢索項目(類別),現在我希望用戶點擊其中一個類別例如,如果用戶點擊餐廳類別,我想要取得該ID並將所有餐館從數據庫中檢索到新意向活動中的另一個列表中。顯示ListView項目從數據庫

這裏的問題是,類別中的每個項目都顯示相同的結果。

類別活動

public class WhereToGo extends ListActivity { 

public final static String ID_EXTRA="com.example.buttontest._id"; 
    DataBaseHelper db_con; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_where_to_go); 

} 

@Override 
    public void onStart(){ 
    super.onStart(); 
    db_con = new DataBaseHelper(this); 
     listNotes(); 
    } 
@SuppressWarnings("deprecation") 
private void listNotes(){ 
    SQLiteDatabase db = db_con.getReadableDatabase(); 
    try { 
     Cursor c = db.rawQuery("SELECT name as '_id' " + 
    "FROM category ",null); 
     final ListAdapter noteAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, c,new String[] {"_id"},new int[] {android.R.id.text1}); 
     this.setListAdapter(noteAdapter); 
    } finally { 
    db.close(); 
    } 
    } 
@Override 
    protected void onListItemClick(ListView l, View v, int position, long id){ 
    super.onListItemClick(l, v, position, id); 
    // Intent: 
    Intent i = new Intent(this, ShowWhereToGo.class); 
    i.putExtra(ID_EXTRA, String.valueOf(id)); 
    startActivity(i); 

    } 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.where_to_go, menu); 
    return true; 
} 

} 

ShowWhereToGo

public class ShowWhereToGo extends Activity { 

    DataBaseHelper dbhelper; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_show_where_to_go); 

     String[] from = new String[] { "name" }; 
      int[] to = new int[] { R.id.TextView1 }; 

      dbhelper = new DataBaseHelper(this); 
      try { 
      dbhelper.createDataBase(); 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } 

      Cursor c = dbhelper.getFacilityData(); 

      @SuppressWarnings("deprecation") 
     SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.activity_tab, c, from, to); 

      ListView list = (ListView) findViewById(R.id.listView1); 

      list.setAdapter(adapter); 
      list.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view,int position, long id) {  
        Intent myIntent = new Intent(view.getContext(), CheckinActivity.class); 
        myIntent.putExtra("id", position); 
        startActivityForResult(myIntent, 0); // display SubView.class    } 
      } 

       @SuppressWarnings("unused") 
       public void onItemClick1(AdapterView<?> arg0, View arg1, 
         int arg2, long arg3) { 
        // TODO Auto-generated method stub 

       }}); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.show_where_to_go, menu); 
     return true; 
    } 

} 

回答

0

試圖改變這一行:

i.putExtra(ID_EXTRA, String.valueOf(id)); 

到:

i.putExtra(ID_EXTRA, position); 
0

1-在listNotes方法中,取noteAdapter並使其成爲全局變量。 2-在onListItemClick方法 更改此i.putExtra(ID_EXTRA, String.valueOf(id));i.putExtra(ID_EXTRA, noteAdapter.getItem(position));

**你必須通過的值* * _id但這裏要傳遞的是陣列中的字符串對象中的哪一個完全無關的你正在尋找的ID。