myc_100 2013-03-27 02:22 采纳率: 10%
浏览 2264
已采纳

在列表中添加值后,却检索不出来

在一个列表中我使用add(int,Object)方法添加了一个值,但是当我使用get(int)方法检索值的时候,却还是获得上次添加的值,并不是这次新添加的值。
谁能给出适当的建议呢?
我使用的下面的代码:

public static List getCompanyName(String user_id) {

    List<CustomerList> fetchDatefromID = new ArrayList<CustomerList>();
    CustomerList tempProgram = new CustomerList();
    String result = "";
    InputStream is = null;

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("q", "" + user_id));

    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "https://erestaurantonline.co.uk/kernow_mobile/customer_search.php?");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }
    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    // parse json data
    try {
        JSONArray jArray = new JSONArray(result);
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);

            if (json_data.getString("company") != null) {
                tempProgram.setCompanyName(json_data.getString("company"));
                tempProgram.setID(json_data.getString("id"));
                fetchDatefromID.add(i, tempProgram);
            }

        }

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    for (int i = 0; i < fetchDatefromID.size(); i++) {
        Log.v("log_tag","DataMy  :  "+fetchDatefromID.get(i).getCompanyName().toString());
    }
    return fetchDatefromID;
}
  • 写回答

2条回答 默认 最新

  • xiaoyan_12 2013-03-28 02:16
    关注

    把下面的代码:

    if (json_data.getString("company") != null) {
                    tempProgram.setCompanyName(json_data.getString("company"));
                    tempProgram.setID(json_data.getString("id"));
                    fetchDatefromID.add(i, tempProgram);
                }
    

    换成:

    if (json_data.getString("company") != null) {
                    tempProgram = new CustomerList();
                    tempProgram.setCompanyName(json_data.getString("company"));
                    tempProgram.setID(json_data.getString("id"));
                    fetchDatefromID.add(i, tempProgram);
                }
    

    否则,list中的每一项都要引用同样的对象,那样的话总是显示你最后一次输入的数据。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿