zoukankan      html  css  js  c++  java
  • 在列表中添加值后,无法检索出来

    原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/1879

    问题描述:

    在一个列表中我使用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;
    }

    解决方案:

    把下面的代码:

    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中的每一项都要引用同样的对象,那样的话总是显示你最后一次输入的数据。

  • 相关阅读:
    hdu 1301 prime算法
    hdu 4763 kmp算法
    linux上安装程序出现的问题汇总
    linux之下载工具wget
    python之os模块
    管道和xargs的区别
    linux下查找文件或目录(which,whereis,locate,find)
    blast+学习之search tools
    linux的文件,目录操作命令(mv,rm,cp)
    PHPCMS V9 简单的二次开发
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2987574.html
Copyright © 2011-2022 走看看