我在实现HTTPGET请求时收到了这个JSON数组。 然后在textView中显示如下:
if(inputStream != null) { JSONArray reps = new JSONArray(convertInputStreamToString(inputStream)); for (int i = 0; i < reps.length(); i++) { JSONObject item = (JSONObject) reps.get(i); result.append("\n(" + (i + 1) + ") " + item.get("name") + ":\n" + item.get("url") + "\n" + "Stars:" + item.get("stargazers_count") + "\n" + item.get("forks_count") + "\n"); }我想在列表视图中查看这个,但是尝试了同样的尝试失败了。 我无法为它创建一个可行的适配器,所以对此的帮助真的非常适合。
I have this JSON Array that I receive when I implement an HTTPGET request. Then this is displayed in a textView as follows:
if(inputStream != null) { JSONArray reps = new JSONArray(convertInputStreamToString(inputStream)); for (int i = 0; i < reps.length(); i++) { JSONObject item = (JSONObject) reps.get(i); result.append("\n(" + (i + 1) + ") " + item.get("name") + ":\n" + item.get("url") + "\n" + "Stars:" + item.get("stargazers_count") + "\n" + item.get("forks_count") + "\n"); }I want to view this in a list view but have failed so many attempts to do the same. I am unable to create a viable adapter for it, so help on this is really appriciated.
最满意答案
创建一个非常基本的列表适配器
public class ListAdapter extends BaseAdapter { private JSONArray array; private LayoutInflater inflater; private ViewHolder holder; public ListAdapter(Context context, JSONArray array) { this.array = array; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } static class ViewHolder { private TextView itemTV; } public int getCount() { return array.length(); } public JSONObject getItem(int position) { try { return array.getJSONObject(position); } catch (JSONException e) { e.printStackTrace(); return new JSONObject(); } } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { holder = new ViewHolder(); View view = convertView; if (view == null) { view = inflater.inflate(R.layout.list_item, null); holder.itemTV = (TextView) view.findViewById(R.id.itemTextView); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.itemTV.setText(getItem(position).get("name").toString()); return view; }在你的布局xml中,添加一个ListView,例如:
<ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" />创建列表项布局文件。 对于此示例,我们将只使用一个textview创建一个简单的布局,并将其另存为“item_layout.xml”...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/itemTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>在您的活动中,声明您的listview和适配器,一旦获得JSON响应,请设置适配器...
private ListAdapter listAdapter; private ListView listView; ... listView = (ListView) view.findViewById(R.id.listView); ... // do whatever you need to get your JSON response, then pass it to your adapter // JSONArray reps = .... listAdapter = new ListAdapter(context, reps); listView.setAdapter(listAdapter);Create a very basic list adapter:
public class ListAdapter extends BaseAdapter { private JSONArray array; private LayoutInflater inflater; private ViewHolder holder; public ListAdapter(Context context, JSONArray array) { this.array = array; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } static class ViewHolder { private TextView itemTV; } public int getCount() { return array.length(); } public JSONObject getItem(int position) { try { return array.getJSONObject(position); } catch (JSONException e) { e.printStackTrace(); return new JSONObject(); } } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { holder = new ViewHolder(); View view = convertView; if (view == null) { view = inflater.inflate(R.layout.list_item, null); holder.itemTV = (TextView) view.findViewById(R.id.itemTextView); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.itemTV.setText(getItem(position).get("name").toString()); return view; }In your layout xml, add a ListView, such as:
<ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" />Create a list item layout file. For this example, we'll just create a simple layout with one textview and save it as "item_layout.xml"...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/itemTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>In your activity, declare your listview and adapter, and once you get your JSON response, set the adapter...
private ListAdapter listAdapter; private ListView listView; ... listView = (ListView) view.findViewById(R.id.listView); ... // do whatever you need to get your JSON response, then pass it to your adapter // JSONArray reps = .... listAdapter = new ListAdapter(context, reps); listView.setAdapter(listAdapter);更多推荐
发布评论