How to show json in data adapter?

Viewed 38

This is my adaptermodel public int cid = -1;public String category_name;public String category_image;Three data should be displayed in my adapter and how to transfer json to adapter.I'm a new devloper and I don't know how to handle this. If you know, I need to fix this

import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.kannada.newspaper.india.R;
    import com.kannada.newspaper.india.models.Users;
    
    import java.util.ArrayList;
    
    public class GalleryAdapter extends BaseAdapter{
        private ArrayList<Users> users;
        private Context context;
    
        public GalleryAdapter(ArrayList<Users> users, Context context) {
            this.users = users;
            this.context = context;
        }
    
        @Override
        public int getCount() {
            return users.size();
        }
    
        @Override
        public Object getItem(int i) {
            return null;
        }
    
        @Override
        public long getItemId(int i) {
            return 0;
        }
    
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=layoutInflater.inflate(R.layout.custom_gallery_layout,null);
            ImageView photo,option;
            if(view==null){
                photo=new ImageView(context);
            }
            photo=(ImageView)view.findViewById(R.id.photo);
    
            TextView name=(TextView)view.findViewById(R.id.name);
    
            Users user=users.get(i);
            photo.setImageResource(user.getPhoto());
            name.setText(user.getName());
    
    
            return view;
        }
     

Category This is the model

 import java.io.Serializable;
    
    public class Category implements Serializable {
    
        public int cid = -1;
        public String category_name;
        public String category_image;

layout this

    <?xml version="1.0" encoding="utf-8"?>
<com.kannada.newspaper.india.utils.SquareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_main"
android:padding="@dimen/nav_header_vertical_spacing"
android:orientation="vertical">

<ImageView
    android:background="@drawable/bg_google"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:alpha="1"
        android:background="@drawable/bg_roundiconimg"
        android:orientation="vertical">
        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:id="@+id/photo"/>
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorWhite"
        android:textSize="@dimen/primeryText"
        android:fontFamily="@font/sfprodisplayregular"
        android:layout_marginTop="@dimen/margin10"
        android:id="@+id/name"
        android:text="Facebook"/>
</LinearLayout>

</com.kannada.newspaper.india.utils.SquareFrameLayout>
1 Answers

Converting JSON to Pojo (i.e class that contains data in code) this mechanism is called serialization. In the android easiest way to do this is using an external library such as GSON or Jackson or Manual serilization.

Gson is one of the libraries majorly used by developers.

If you want to convert JSON to POJO follow this thread JSON TO POJO

Related