程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> listview-在列表中添加值後,卻檢索不出來

listview-在列表中添加值後,卻檢索不出來

編輯:編程綜合問答
在列表中添加值後,卻檢索不出來

在一個列表中我使用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中的每一項都要引用同樣的對象,那樣的話總是顯示你最後一次輸入的數據。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved