Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Displaying data from json to cardview in RecyclerView

I want to display data from webservice to cardview in recycleviewer. It had already success to display data. But it only display one data, although it has a lot of data to display. Can you help me to solve this problem? Thank you

Seller.java

public class Seller {
    String penjual,image,alamat,telp;

    public String getPenjual() {
        return penjual;
    }

    public void setPenjual(String penjual) {
        this.penjual = penjual;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getAlamat() {
        return alamat;
    }

    public void setAlamat(String alamat) {
        this.alamat = alamat;
    }

    public String getTelp() {
        return telp;
    }

    public void setTelp(String telp) {
        this.telp = telp;
    }
}

SellerAdapter.Java

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.amobi.lomapodfix.R;
import com.amobi.lomapodfix.model.Seller;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Nicky-PC on 5/4/2016.
 */
public class SellerAdapter extends RecyclerView.Adapter<SellerAdapter.PersonViewHolder> {

    List<Seller> sellers;
    Context context;
    public SellerAdapter(Context context,List<Seller> sellers)
    {
        this.sellers=sellers;
        this.context=context;
    }

    public void setGridData(List<Seller> mGridData)
    {
        this.sellers=mGridData;
        notifyDataSetChanged();
    }
    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_toko, parent, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder holder, int i) {
        holder.Seller.setText(sellers.get(i).getPenjual());
        holder.Address.setText(sellers.get(i).getAlamat());
        holder.Telp.setText(sellers.get(i).getTelp());
        Picasso.with(context).load(sellers.get(i).getImage()).fit().into(holder.image);
    }

    @Override
    public int getItemCount() {
        return sellers.size();
    }

    public static class PersonViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView Seller;
        TextView Address;
        TextView Telp;
        ImageView image;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            Seller = (TextView)itemView.findViewById(R.id.lvSeller);
            Address = (TextView)itemView.findViewById(R.id.lvAlamat);
            Telp = (TextView)itemView.findViewById(R.id.lvAlamat);
            image= (ImageView)itemView.findViewById(R.id.person_photo);
        }
    }
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }
}

DogFragment.java

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.amobi.lomapodfix.JSONParser;
import com.amobi.lomapodfix.MainActivity;
import com.amobi.lomapodfix.R;
import com.amobi.lomapodfix.adapter.SellerAdapter;
import com.amobi.lomapodfix.model.Seller;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DogFragment extends Fragment {

    SellerAdapter mGridadapter;
    JSONParser jParser= new JSONParser();
    ArrayList<HashMap<String, String>> nameList;
    JSONArray names=null;
    RecyclerView data;
    List<Seller> mgridData;
    ProgressDialog pDialog;
    private static final String URL_TEST_ANJING = "http://lomapod.azurewebsites.net/readSellerAnjing.php";
    private static final String TAG_PESAN = "message";
    private static final String TAG_HASIL = "result";
    private static final String TAG_ID = "id_penjual";
    private static final String TAG_SELLER= "nama_toko";
    private static final String TAG_ALAMAT= "alamat_toko";
    private static final String TAG_TELP= "no_telp";
    private static final String TAG_IMAGE= "image_name";
    private RecyclerView.LayoutManager layoutManager;


    public DogFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView=inflater.inflate(R.layout.fragment_dog, container, false);
        data=(RecyclerView) rootView.findViewById(R.id.rv);
        layoutManager = new LinearLayoutManager(getActivity());
        data.setLayoutManager(layoutManager);
        mgridData=new ArrayList<>();
        mGridadapter=new SellerAdapter(getActivity(),mgridData);

        new AmbilDataJsonAnjing().execute();
        return rootView;
    }

    public class AmbilDataJsonAnjing extends AsyncTask<String,String,String> {

        int sukses=0;

        public AmbilDataJsonAnjing() {
            pDialog = new ProgressDialog(getActivity());
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog.setMessage("Mengambil Data. Silahkan Tunggu...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {

            List<NameValuePair> params = new ArrayList<NameValuePair>();

            try
            {
                JSONObject json = jParser.makeHttpRequest(URL_TEST_ANJING, "GET", params);
                Seller sellers=null;
                if(json != null)
                {
                    sukses = json.getInt(TAG_PESAN);
                    if(sukses == 1)
                    {
                        nameList = new ArrayList<HashMap<String,String>>();
                        Log.d("Semua Nama: ", json.toString());
                        names = json.getJSONArray(TAG_HASIL);

                        for(int i = 0; i < names.length();i++)
                        {
                            JSONObject c = names.getJSONObject(i);
                            String id = c.getString(TAG_ID);
                            String seller = c.getString(TAG_SELLER);
                            String alamat=c.getString(TAG_ALAMAT);
                            String telp=c.getString(TAG_TELP);
                            String image=c.getString(TAG_IMAGE);

                            HashMap<String,String> map = new HashMap<String,String>();

                            sellers=new Seller();
                            sellers.setPenjual(seller);
                            sellers.setAlamat(alamat);
                            sellers.setTelp(telp);
                            sellers.setImage(image);

                            map.put(TAG_ID, id);
                            map.put(TAG_SELLER,seller);
                            map.put(TAG_ALAMAT,alamat);
                            map.put(TAG_TELP,telp);
                            nameList.add(map);
                            mgridData.add(sellers);
                        }
                    }
                }
            }catch(JSONException e)
            {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            pDialog.dismiss();
            RecyclerView.Adapter adapter=new SellerAdapter(getActivity(),mgridData);
            if(sukses == 1 )
            {
                mGridadapter.setGridData(mgridData);
                data.setAdapter(adapter);
            }
            else
            {
                Toast.makeText(getActivity(), "Error - No Data Available", Toast.LENGTH_SHORT).show();
            }
        }
    }

}

And I get this in android monitor

05-04 23:35:55.874 5605-5668/com.amobi.lomapodfix D/url: http://lomapod.azurewebsites.net/readSellerAnjing.php?
05-04 23:35:55.878 5605-5605/com.amobi.lomapodfix W/FragmentManager: moveToState: Fragment state for CatFragment{a9c44708 #1 id=0x7f0d007e android:switcher:2131558526:1} not updated inline; expected state 3 found 2
05-04 23:35:55.938 5605-5605/com.amobi.lomapodfix W/EGL_emulation: eglSurfaceAttrib not implemented
05-04 23:35:55.938 5605-5605/com.amobi.lomapodfix E/RecyclerView: No adapter attached; skipping layout
05-04 23:35:55.946 5605-5605/com.amobi.lomapodfix I/dalvikvm: Could not find method android.support.v7.widget.LinearLayoutCompat.drawableHotspotChanged, referenced from method android.support.design.internal.ForegroundLinearLayout.drawableHotspotChanged
05-04 23:35:55.946 5605-5605/com.amobi.lomapodfix W/dalvikvm: VFY: unable to resolve virtual method 15705: Landroid/support/v7/widget/LinearLayoutCompat;.drawableHotspotChanged (FF)V
05-04 23:35:55.946 5605-5605/com.amobi.lomapodfix D/dalvikvm: VFY: replacing opcode 0x6f at 0x0000
05-04 23:35:55.958 5605-5608/com.amobi.lomapodfix D/dalvikvm: GC_CONCURRENT freed 374K, 8% free 55730K/60500K, paused 2ms+1ms, total 9ms
05-04 23:35:56.098 5605-5668/com.amobi.lomapodfix I/info: org.apache.http.client.methods.HttpGet@a9c7a668
05-04 23:35:56.098 5605-5668/com.amobi.lomapodfix D/Semua Nama:: {"message":1,"result":[{"no_telp":"08345678","nama_toko":"lomapod","image_name":null,"id_image":"1","no_rek":"12345","email":"[email protected]","id_penjual":"1","alamat_toko":"jl.bbbbb","kota":"Yogyakarta","nama_bank":"BBB"},{"no_telp":"0829417","nama_toko":"meow","image_name":null,"id_image":"1","no_rek":"12345","email":"[email protected]","id_penjual":"2","alamat_toko":"jl.ccccc","kota":"Yogyakarta","nama_bank":"BNI"}]}
05-04 23:35:56.682 5605-5605/com.amobi.lomapodfix W/EGL_emulation: eglSurfaceAttrib not implemented
05-04 23:35:56.702 5605-5605/com.amobi.lomapodfix I/Choreographer: Skipped 42 frames!  The application may be doing too much work on its main thread.
05-04 23:35:56.706 5605-5605/com.amobi.lomapodfix E/RecyclerView: No adapter attached; skipping layout
05-04 23:35:56.918 5605-5605/com.amobi.lomapodfix W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
like image 742
Nicky Apriliani Avatar asked May 04 '16 16:05

Nicky Apriliani


People also ask

How to show JSON data in RecyclerView in android?

Android JSON Data Parsing tutorial – Download JSOn data from online, Parse it, and display in RecyclerView. Open New activity when RecyclerView is clicked. Android JSON tutorial. Download JSON data from online using HTTPURLConnection class, parse that data and bind it to RecyclerView.

What is CardView in Android with example?

CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design.


1 Answers

I've encountered this issue that the recycler view only displays the first item. It was because I was importing different versions of recyclerview, cardview, design and appcompat. The issue is fixed after I made the versions consistent in the app gradle, The version doesn't have to be 23.2.1 but they have to be the same for all of these.

compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:recyclerview-v7:23.2.1'
compile 'com.android.support:cardview-v7:23.2.1'

Another cause could be you are using match_parent for the height for the items in the recyclerview and the parent container of you recyclerview. This makes the first item to occupy the entire screen. Should use wrap_content or give a specific height instead.

like image 126
s-hunter Avatar answered Sep 27 '22 16:09

s-hunter