我的主要活动将一个字符串传递给另一个活动(我们称之为子活动)。在这个子活动中,我需要在游标原始查询中使用该字符串来从数据库中选择与该字符串匹配的数据。
public class RestaurantsInfo extends ListActivity {
static final String restaurantListTable = "RestaurantList";
static final String colRestaurantID = "RestaurantID";
static final String colRestaurantName = "RestaurantName";
static final String colRestaurantStore = "StoreNo";
static final String colRestaurantAddress = "StoreAddress";
public String strRestaurantName;
RestaurantDB db = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
db=new RestaurantDB(RestaurantsInfo.this);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String strRestaurantName = extras.getString("RestaurantName");
setTitle(strRestaurantName);
getData();
}
private Cursor doQuery(String strRestaurantName) {
return(db.getReadableDatabase().rawQuery("SELECT "+colRestaurantID+" AS _id, "+colRestaurantName+", "+colRestaurantStore+", "+colRestaurantAddress+
" FROM "+restaurantListTable+" WHERE "+colRestaurantName+" = strRestaurantName ORDER BY "+colRestaurantStore+"", null));
}
public void getData() {
SimpleCursorAdapter adapter;
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
adapter=new SimpleCursorAdapter(this, R.layout.activity_address,
doQuery(strRestaurantName), new String[] {
RestaurantDB.colRestaurantName,
RestaurantDB.colRestaurantStore,
RestaurantDB.colRestaurantAddress },
new int[] { R.id.textView1, R.id.textView2, R.id.textView3 },
0);
}
else {
adapter=new SimpleCursorAdapter(this, R.layout.activity_address,
doQuery(strRestaurantName), new String[] {
RestaurantDB.colRestaurantName,
RestaurantDB.colRestaurantStore,
RestaurantDB.colRestaurantAddress },
new int[] { R.id.textView1, R.id.textView2, R.id.textView3 });
}
setListAdapter(adapter);
}
}
从代码中可以看出,我尝试传递字符串strRestaurantName
并在查询中使用它WHERE
,但它不起作用!日志文件说“没有这样的列strRestaurantName
”,但我并不是说它是一个列。我试过doQuery()
代替doQuery(strRestaurantName)
,但它也给出了同样的错误。我该怎么办?