我使用购物车处理存储在 data/data/.../cartlist.db 中的一堆选择来构建 android 4.0 应用程序,并且需要知道如何将光标结果与 List NameValuePair 相关联,这是我的活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sendorder);
postData();
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
String[] columns = new String[] { "_id","name",
"qty", "price" };
Cursor messagesCursor= getContentResolver().query(
MyContentProvider.CONTENT_URI, columns, null, null,
null);
int theTotal = messagesCursor.getCount();
Toast.makeText(this, "Total6: " + theTotal, Toast.LENGTH_LONG).show();
int idCol = messagesCursor.getColumnIndex("_id");
int nameCol = messagesCursor.getColumnIndex("name");
int qtyCol = messagesCursor.getColumnIndex("qty");
int priceCol = messagesCursor.getColumnIndex("price");
while (messagesCursor.moveToNext()) {
columns[0] = Integer.toString((messagesCursor.getInt(idCol)));
quantity = messagesCursor.getString(nameCol);
product = messagesCursor.getString(qtyCol);
cost = messagesCursor.getString(priceCol);
}
}
public void postData() {
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost( "http://example.com/droid/order.php");
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("qty", quantity));
nameValuePair.add(new BasicNameValuePair("product",product));
nameValuePair.add(new BasicNameValuePair("price",cost));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
}