I would create some listview from my api json response, but I stuck with this LinkedTreeMap error in my code. Could anyone help me to solve this?

public class KategoriListAdapter extends BaseAdapter {

    Context context;
    ArrayList<Barang> barang;

    public KategoriListAdapter(Context context, ArrayList<Barang> barang) {
        this.context = context;
        this.barang = barang;
    }

    @Override
    public int getCount() {
        return barang.size();
    }

    @Override
    public Barang getItem(int i) {
        return this.barang.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.custom_list_view_kategori, viewGroup, false);
        }

        TextView tvNama = (TextView) view.findViewById(R.id.tv_nama);
        TextView tvHarga = (TextView) view.findViewById(R.id.tv_harga);
        TextView tvUsername = (TextView) view.findViewById(R.id.tv_username);


        Object getrow = this.barang.get(i);
        LinkedTreeMap<Object, Object> rowmap = (LinkedTreeMap) getrow;
        String nama = rowmap.get("nama").toString();
        String harga = rowmap.get("harga").toString();
        String username = rowmap.get("username").toString();

        tvNama.setText(nama);
        tvHarga.setText(harga);
        tvUsername.setText(username);

        return view;
    }

}

public class Barang {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("username")
    @Expose
    private String username;
    @SerializedName("nama")
    @Expose
    private String nama;
    @SerializedName("harga")
    @Expose
    private String harga;
    @SerializedName("gambar")
    @Expose
    private String gambar;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }

    public String getHarga() {
        return harga;
    }

    public void setHarga(String harga) {
        this.harga = harga;
    }

    public String getGambar() {
        return gambar;
    }

    public void setGambar(String gambar) {
        this.gambar = gambar;
    }

}

The log result while I run the activity is

java.lang.ClassCastException: com.example.barangkoz.model.Barang cannot be 
cast to com.google.gson.internal.LinkedTreeMap
    at     com.example.barangkoz.activities.KategoriListAdapter.getView(KategoriListAdapter.java:57)`

Related posts

Recent Viewed