我正在使用从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;
}
}
答案 0 :(得分:0)
尝试更改此行:
i.putExtra(ID_EXTRA, String.valueOf(id));
为:
i.putExtra(ID_EXTRA, position);
答案 1 :(得分:0)
1-在listNotes
方法中,取noteAdapter
并将其设为全局变量。
2-在onListItemClick
方法中
将此i.putExtra(ID_EXTRA, String.valueOf(id));
更改为i.putExtra(ID_EXTRA, noteAdapter.getItem(position));
**你必须传递* _id *的值,但是你在这里传递数组中String对象的is,这与你要搜索的id完全无关。