0

我使用购物车处理存储在 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();

        }
    }
4

1 回答 1

0

我发现只是放错了名称值对并清理了一些大括号希望答案能帮助一些人

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sendorder);

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);
    // 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));


    // Creating HTTP client
    HttpClient httpClient = new DefaultHttpClient();
    // Creating HTTP Post
    HttpPost httpPost = new HttpPost( "http://example.com/droid/order.php");


    // 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();
      }
    }
}
于 2013-03-25T10:08:08.110 回答