2015-10-04 31 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

相关问题