2015-10-04 79 views
2

嗨請回答我的問題 我有這個代碼在eclipse中的Android開發。 我正在使用mysql和php的數據庫和JSON數據。但我不知道如何在ListView中使用JSONparse數據。請編輯我的代碼。如何從parseJson獲取列表視圖數據

public class ViewAllPersons extends Activity { 

String url = "http://192.168.1.206/androhp/view_all_persons.php"; 
ArrayList<String> result; 
ListView list; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view_all_person); 
    result = new ArrayList<String>(); 
    LoadAllPersons lap = new LoadAllPersons(); 
    lap.execute(url); 
} 

class LoadAllPersons extends AsyncTask<String, String, String> { 

    protected String doInBackground(String... args) { 
     InputStream jsonStream = getStreamFromURL(args[0], "GET"); 
     String jsonString = streamToString(jsonStream); 
     parseJSON(jsonString); 
     return null; 
    } 

    void parseJSON(String JSONString) { 
     try { 

      JSONObject jo = new JSONObject(JSONString); 

      JSONArray allpersons = jo.getJSONArray("allpersons"); 
      for (int i = 0; i < allpersons.length(); i++) { 
       JSONObject object = allpersons.getJSONObject(i); 
       String objString = ""; 
       objString = object.getString("name") + " , " 
         + object.getString("name2") + " : " 
         + object.getInt("iconlink"); 
       result.add(objString); 
      } 

     } catch (JSONException e) { 

     } 
    } 

    protected void onPostExecute(String file_url) { 

     list = (ListView) findViewById(R.id.list); 
      String[] web = { 
        "Google Plus", 
         "Twitter", 
         "Windows" 
       } ; 

       String[] imageUrl = { 
         "http://www.varzesh3.com/football3_Images/varzesh3-logo.png", 
         "http://www.varzesh3.com/football3_Images/varzesh3-logo.png", 
         "http://www.varzesh3.com/football3_Images/varzesh3-logo.png" 

       }; 
     CustomList adapter = new 
       CustomList(ViewAllPersons.this, web, imageUrl); 
     list.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 
    } 

} 

如何使用parseJSON數據,而不是網頁的ListView:

list = (ListView) findViewById(R.id.list); 
      String[] web = { 
        "Google Plus", 
         "Twitter", 
         "Windows" 
       } ; 

       String[] imageUrl = { 
         "http://www.varzesh3.com/football3_Images/varzesh3-logo.png", 
         "http://www.varzesh3.com/football3_Images/varzesh3-logo.png", 
         "http://www.varzesh3.com/football3_Images/varzesh3-logo.png" 

       }; 
+0

您是否獲得自定義適配器的任何錯誤,你已經創建了?如果是,請添加該CustomList代碼? –

+0

不,我沒有在custam適配器中的任何錯誤,只是我不知道如何可以使用parseJSON數據,而不是網絡列表視圖。 –

回答

1
You have got both data as well as list, now you need to combine them. 
first you have to convert your json result into list, where values are your json values. 
    final ArrayList<String> listdata = new ArrayList<String>(); 
    for (int i = 0; i < values.length; ++i) { 
     listdata .add(values[i]); 
    } 

then you have to assign adapter to your list. You can use following code. 
    final StableArrayAdapter adapter = new StableArrayAdapter(this, 
     android.R.layout.yourlistlayout, listdata); 
    list.setAdapter(adapter); 

更多細節 http://www.vogella.com/tutorials/AndroidListView/article.html

相關問題