简体   繁体   中英

How to use LazyAdapter to load JSON into ListView

I am trying to use a LazyAdapter to load my JSON data into a ListView . How do I modify this LazyAdapter to load the data and how should I call it from my Fragment ? I am using JSON data that is grabbed using Volley . This is the main JSON returned:

{
    "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"
        }
    ]
}

I used this line to extract the data only:

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

This is the LazyAdapter that I have currently:

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;
    }
}

This is my row_listview_item.xml file:

<?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>

}

I figured it out! I will post my answer for other people to benefit from it.

Okay, I made some change to the LazyAdapter. This is the new LazyAdapter file:

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;
    }

}

And this is how I called it in my Fragment:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM