【发布时间】:2013-10-11 09:25:27
【问题描述】:
我有这个列表视图,其中包含从 SQLite 浏览器检索的项目(类别),现在我希望用户单击其中一个类别,例如,如果用户单击餐厅类别,我想获取 id并将数据库中的所有餐厅检索到新意图活动中的另一个列表中。
这里的问题是,类别中的每个项目都显示相同的结果。
类别活动
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;
}
}
【问题讨论】: