如何使用LazyAdapter将JSON加载到ListView

时间:2016-05-30 20:31:03

标签: android json listview

我正在尝试使用 LazyAdapter 将我的JSON数据加载到 ListView 中。如何修改此 LazyAdapter 以加载数据以及如何从 Fragment 中调用它?我使用的是使用 Volley 抓取的JSON数据。这是返回的主要JSON:

{
    "success": "1",
    "message": "Users refreshed",
    "data":
    [
        {
            "id": "3",
            "main_user": "1",
            "name": "Name 02",
            "photo_link": "http://example.com/folder/photos/1.jpg"
        },
        {
            "id": "4",
            "main_user": "1",
            "name": "Name 03",
            "photo_link": "http://example.com/folder/photos/2.png"
        },
        {
            "id": "5",
            "main_user": "1",
            "name": "Name 04",
            "photo_link": "http://example.com/folder/photos/3.png"
        },
        {
            "id": "6",
            "main_user": "1",
            "name": "Name 05",
            "photo_link": "http://example.com/folder/photos/4.jpeg"
        }
    ]
}

我使用此行仅提取数据:

JSONObject jsonObject = new JSONObject(response);
JSONArray data = jsonObject.getJSONArray("data");

这是我目前的LazyAdapter:

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;

    public LazyAdapter(Activity a, String[] d) {
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            view = inflater.inflate(R.layout.row_listview_item, null);
        }
        TextView text = (TextView) view.findViewById(R.id.text);
        ImageView image = (ImageView) view.findViewById(R.id.image);
        text.setText(position);
        imageLoader.DisplayImage(data[position], image);
        return view;
    }
}

这是我的row_listview_item.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:id="@+id/listItemLayout"
    android:descendantFocusability="blocksDescendants">

    <ImageView
        android:id="@+id/image"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="Loading..."
        android:typeface="sans"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/image"
        android:layout_toEndOf="@id/image"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:maxLines="1"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:visibility="invisible"
        android:layout_toRightOf="@id/image"
        android:layout_toEndOf="@id/image"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

}

1 个答案:

答案 0 :(得分:0)

我明白了!我将发布我的答案,让其他人从中受益。

好的,我对LazyAdapter进行了一些更改。这是新的LazyAdapter文件:

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private JSONArray data;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;

    public LazyAdapter(Activity activity, JSONArray data) {
        this.activity = activity;
        this.data = data;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.length();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            view = inflater.inflate(R.layout.list_row, parent, false);
        }
        TextView id = (TextView) view.findViewById(R.id.employeeId);
        TextView name = (TextView) view.findViewById(R.id.employeeName);
        ImageView image = (ImageView) view.findViewById(R.id.employeeImage);
        try {
            id.setText(data.getJSONObject(position).getString("id"));
            name.setText(data.getJSONObject(position).getString("name"));
            imageLoader.DisplayImage(data.getJSONObject(position).getString("photo_link"), image);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return view;
    }

}

这就是我在片段中调用它的方式:

adapter = new LazyAdapter(getActivity(), data);
//If calling from activity, just use 'this' instead of 'getActivity()'. 'data' is just a JSONArray as you see in the question.
list.setAdapter(adapter);