0

I'm having this listview with items (categories) retrieved from the SQLite browser, and now i want the user to click on one of the categories for example, if the user click of restaurant category, i want to take the id and retrieve all the restaurants from the db into another list in new intent activity.

The problem here, is every item in the category display the same result.

The categories Activity

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;
    }

}
4

2 回答 2

0

尝试更改此行:

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

至:

i.putExtra(ID_EXTRA, position);
于 2013-10-11T10:31:16.963 回答
0

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

** 你必须传递 *_id* 的值,但在这里你传递的是数组中 String 对象的 is,它与你正在搜索的 id 完全无关。

于 2013-10-11T13:20:31.953 回答