0

我在这里成功地解析了这个网站的数据。编写了一些代码后,我得到了一个字符串,我想在其中显示到 ListView 中。基本上,我想将整个数组从网站显示到 ListView 中。

DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://ec2-54-213-155-95.us-west-2.compute.amazonaws.com/notices.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");

    InputStream inputStream = null;
    String result = null;
    try {
        HttpResponse response = httpclient.execute(httppost);           
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
    } catch (Exception e) { 
        // Oops
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }

    try {
        JSONObject jObject = new JSONObject(result);
        JSONArray jsonArray = jObject.getJSONArray("notices");        
        for(int i = 0; i < jsonArray.length(); i++) {
            String arrayString = jsonArray.getString(i);
            Log.d("notices", arrayString); 
            ListView listView1 = (ListView) findViewById(R.id.listView1);


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我正在考虑使用arrayAdapter,但我不确定如何使用它!

4

5 回答 5

4

在将数据显示在 ListView 中之前,首先决定是要将数据存储在 ArrayList 还是 Database 中。对于 ArrayList 到 ListView,您将需要 ArrayAdapter,从数据库到 ListView,您将需要 CursorAdapter。在 ArrayList 中,如果您只有 TextView,那么您可以使用 Simple ArrayAdapter,否则如果有多个 TextView 或更多组件,则使用自定义 ArrayAdapter。

于 2013-09-04T03:52:49.827 回答
0

您可以按如下方式使用 SimpleArrayAdapter:-

ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < jsonArray.length(); i++) {
            String arrayString = jsonArray.getString(i);
            Log.d("notices", arrayString); 
          list .add(arrayString);


    }


// and after filling this array list you can set this adapter to you list as 
ListView listView1 = (ListView) findViewById(R.id.listView1);
 final StableArrayAdapter adapter = new StableArrayAdapter(this,
        android.R.layout.simple_list_item_1, list);
    listView1 .setAdapter(adapter)
于 2013-09-04T04:24:23.577 回答
0

如果您想在列表视图中显示您的数据,那么您可以使用以下代码:

    private ArrayAdapter<String> mArrayAdapter;
     ListView listView1 ;

    // Initialize array adapter.

   mArrayAdapter = new ArrayAdapter<String>(this, R.layout.array_layout);
    listView1 = (ListView) findViewById(R.id.listView1);

    // Rest of your code...
    //
    //  

 try {
            JSONObject jObject = new JSONObject(result);
            JSONArray jsonArray = jObject.getJSONArray("notices");        
            for(int i = 0; i < jsonArray.length(); i++) {
                String arrayString = jsonArray.getString(i);
                   mArrayAdapter.add(arrayString) ;

                Log.d("notices", arrayString); 
                }
               listView1.setAdapter(mArrayAdapter);


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

array_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:padding="5dp"
/>
于 2013-09-04T04:27:25.570 回答
0

Try to add the data using ArrayList<String> and StableArrayAdapter as below:

    ListView listView1 = (ListView) findViewById(R.id.listView1); 
     JSONObject jObject = new JSONObject(result);
    JSONArray jsonArray = jObject.getJSONArray("notices");   
     final ArrayList<String> list = new ArrayList<String>();
     for (int i = 0; i < jsonArray.length; ++i) 
       {
           String arrayString = jsonArray.getString(i);
            Log.d("notices", arrayString); 
           list.add(arrayString);
     }
    final StableArrayAdapter adapter = new StableArrayAdapter(this,
    android.R.layout.simple_list_item_1, list);
    listview1.setAdapter(adapter);

StableArrayAdapter class

   private class StableArrayAdapter extends ArrayAdapter<String> {
         HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
   public StableArrayAdapter(Context context, int textViewResourceId,
           List<String> objects) {
         super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.size(); ++i) {
          mIdMap.put(objects.get(i), i);
     }
  }
@Override
public long getItemId(int position) {
  String item = getItem(position);
  return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
  return true;
}

}

于 2013-09-04T04:29:58.067 回答
0

到目前为止,您可能已经得到了答案,但它仍然可以帮助其他人。我是这样做的,效果很好。

我制作了一个包含 4 个文本视图的 xml recco_list 文件。

声明这些。

ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
ListView list;

进入数组

      for(int i = 0; i < RECCO_ARRAY.length(); i++) {
        try {

            JSONObject recco = RECCO_ARRAY.getJSONObject(i);
            JSONObject program = recco.getJSONObject("program");
            JSONObject outlet = recco.getJSONObject("outlet");
            String normalized_weight = recco.getString("normalized_weight");
            String distance = recco.getString("distance");
            HashMap<String, String> map = new HashMap<String, String>();
            String program_name = program.getString("name");
            String outlet_basics = outlet.getString("basics");
            String outlet_name = new JSONObject(outlet_basics).getString("name");
            map.put("program_name", program_name);
            map.put("outlet_name", outlet_name);
            map.put("normalized_weight", normalized_weight);
            map.put("distance", distance);

            oslist.add(map);
            list= (ListView) rootView.findViewById(R.id.recco_list);
            ListAdapter adapter = new SimpleAdapter(getActivity(), oslist, 
                    R.layout.recco_list_view, new String[] {"program_name","outlet_name", "normalized_weight", "distance"}, 
                    new int[]{R.id.program_name,R.id.outlet_name, R.id.normalized_weight, R.id.distance});
            list.setAdapter(adapter);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
于 2014-07-17T18:07:54.303 回答