2015-02-08 130 views
0


我在解析來自URL的JSON的代碼時遇到問題,應該更新一個listview。
這是我的代碼:
如何將項目從JSON添加到列表視圖?

private String sometext; 

public class AsyncTaskParseJson extends AsyncTask<String, String, String> { 

     final String TAG = "AsyncTaskParseJson.java"; 

     // contacts JSONArray 
     JSONArray dataJsonArr = null; 

     @Override 
     protected void onPreExecute() {} 

     @Override 
     protected String doInBackground(String... arg0) { 

      try { 

       // instantiate our json parser 
       JSONParser jParser = new JSONParser(); 

       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
       String userEmailVariable = settings.getString("variable1", ""); 

       // get json string from url 
       JSONObject json = jParser.getJSONFromUrl("path/to/jsonpage.php", userEmailVariable.toString()); 

       // get the array of users 
       dataJsonArr = json.getJSONArray("Files"); 

       // loop through all users 
       for (int i = 0; i < dataJsonArr.length(); i++) { 

        JSONObject c = dataJsonArr.getJSONObject(i); 
        sometext = ""; 
        // Storing each json item in variable 
        String id = c.getString("id"); 
        String type = c.getString("type"); 
        String name = c.getString("name"); 
        String icon = c.getString("icon"); 
        sometext = name.toString(); 

        if(icon.toString().length() == 0){ 
         icon = "default"; 
        } 

        // show the values in our logcat 
        Log.i(TAG, "type: " + type 
          + ", name: " + name 
          + ", icon: " + icon); 

        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          populatelist(sometext); 
         } 
        }); 
       } 

      } catch (JSONException e) { 
       Log.i(TAG, "Errore LS: Errore del JSON in Files"); 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(String strFromDoInBg) {} 
    } 
    public void populatelist(String name) { 
     final String TAG = "Popolazione lista"; 
     ListView myListView = (ListView)findViewById(R.id.userContentList); 
     ArrayList<String> myStringArray1 = new ArrayList<String>(); 
     myStringArray1.add(name); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, R.id.file_list, myStringArray1); 
     myListView.setAdapter(adapter); 
     Log.i(TAG, name); 
    } 

的問題是,這個代碼僅增加了JSON數組的最後一項,即使在logcat中正確顯示的每個條目。
似乎populatelist每次獲得相同的名稱值。
這段代碼有什麼問題?我是Android和Java開發人員的新手。

回答

1

看來你打電話populatelist(sometext)多次,最後,列表視圖顯示最後value.So重寫代碼爲

public class AsyncTaskParseJson extends AsyncTask < String, String, String > { 

    final String TAG = "AsyncTaskParseJson.java"; 
    ArrayList <String> myStringArray1 = new ArrayList <String>(); 
    // contacts JSONArray 
    JSONArray dataJsonArr = null; 

    @Override 
    protected void onPreExecute() {} 

    @Override 
    protected String doInBackground(String...arg0) { 

     try { 

      // instantiate our json parser 
      JSONParser jParser = new JSONParser(); 

      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
      String userEmailVariable = settings.getString("variable1", ""); 

      // get json string from url 
      JSONObject json = jParser.getJSONFromUrl("path/to/jsonpage.php", userEmailVariable.toString()); 

      // get the array of users 
      dataJsonArr = json.getJSONArray("Files"); 

      // loop through all users 
      for (int i = 0; i < dataJsonArr.length(); i++) { 

       JSONObject c = dataJsonArr.getJSONObject(i); 
       sometext = ""; 
       // Storing each json item in variable 
       String id = c.getString("id"); 
       String type = c.getString("type"); 
       String name = c.getString("name"); 
       String icon = c.getString("icon"); 
       sometext = name.toString(); 

       if (icon.toString().length() == 0) { 
        icon = "default"; 
       } 

       // show the values in our logcat 
       Log.i(TAG, "type: " + type + ", name: " + name + ", icon: " + icon); 

       myStringArray1.add(sometext); 
      } 

     } catch (JSONException e) { 
      Log.i(TAG, "Errore LS: Errore del JSON in Files"); 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(String strFromDoInBg) { 
     populatelist(myStringArray1); 
    } 
} 
public void populatelist(ArrayList <String> myStringArray1) { 
    final String TAG = "Popolazione lista"; 
    ListView myListView = (ListView) findViewById(R.id.userContentList); 

    ArrayAdapter <String> adapter = new ArrayAdapter <String> (getApplicationContext(), R.layout.list_item, R.id.file_list, myStringArray1); 
    myListView.setAdapter(adapter); 
    Log.i(TAG, name); 
} 

在這個初始化myStringArray1爲全局變量和調用populatelist方法onPostExecute方法,而不是調用的它在後臺(刪除runOnUiThread)。

+0

嗨,謝謝你幫助我。無論如何,在populatelist()在行ArrayAdapter < String > adapter = new ArrayAdapter < String >(getApplicationContext(),R.layout.list_item,R.id.file_list,myStringArray1);它說'無法解析符號myStringArray1'。爲什麼? 更新:非常感謝!現在它工作了! :D – 2015-02-08 17:41:54

+0

@FrancescoSorge在'populatelist'方法中傳遞'myStringArray1'作爲參數(見編輯) – 2015-02-08 17:45:38

+0

非常感謝!現在它完美的工作! :D – 2015-02-08 17:46:09

相關問題