ListView帮助JSON(ListView Help JSON)

编程入门 行业动态 更新时间:2024-10-11 17:22:06
ListView帮助JSON(ListView Help JSON)

我在实现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);

更多推荐

本文发布于:2023-07-15 07:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111438.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ListView   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!