Search code examples
androidjsonpojo

how to extract element from arraylist of json pojo class


i want to extract array element of arraylist using imageTypeViewHolder.categories.setText(object.categories); that i have get through json pojo class

Json Pojo Class for Data fetch

public class WPPost {
    @Expose
    private List<Object> meta = null;
    @SerializedName("categories")

    public List<Integer> getCategories() {
            return categories;
        }
    }

In short when i have fetch it's result show me [10,51,13]

but i need to print this type of result [US,India,UK] with switch case or other logics. so please let me know how can do this

imageTypeViewHolder.categories.setText(object.categories);
switch(Integer.parseInt(object.categories)){

                case 10:
                    imageTypeViewHolder.categories.setText("US");
                    break;

                case 51:
                    imageTypeViewHolder.categories.setText("India");
                    break;
                case 13:
                    imageTypeViewHolder.categories.setText("UK");
                    break;
                default:
                    imageTypeViewHolder.categories.setText("Not Found"); }

File Name: RecyclerViewAdapter.java (Here i have set value for print)

package com.ejobbox.ejobbox;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
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.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.messaging.FirebaseMessagingService;

import java.util.ArrayList;


public class RecyclerViewAdapter extends RecyclerView.Adapter{
    private final int VIEW_TYPE_ITEM = 0;
    private final int VIEW_TYPE_LOADING = 1;
    private OnLoadMoreListener onLoadMoreListener;
    private boolean isLoading;
    private Activity activity;
    private ArrayList<Model> dataset;

    private Context mContext;
    private int visibleThreshold = 5;
    private int lastVisibleItem, totalItemCount;

    SharedPreferences sharedPreferences;

    String getdata;
    String colorName="#4242f4";
    int myColor = Color.parseColor(colorName);

    String colorName2="#3bc600";
    int myColor2 = Color.parseColor(colorName2);


    public RecyclerViewAdapter(RecyclerView recyclerView, ArrayList<Model> mlist, Context context) {


        this.dataset=mlist;
        this.mContext=context;
        this.activity=activity;


        final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                totalItemCount = linearLayoutManager.getItemCount();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                    if (onLoadMoreListener != null) {
                        onLoadMoreListener.onLoadMore();
                    }
                    isLoading = true;
                }
            }
        });

    }

    public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
        this.onLoadMoreListener = mOnLoadMoreListener;
    }

    @Override
    public int getItemViewType(int position) {
        return dataset.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.postdetails,parent,false);
            return new ImageTypeViewHolder(view);
        } else if (viewType == VIEW_TYPE_LOADING) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemload,parent,false);
            return new LoadingViewHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ImageTypeViewHolder) {
            final Model object=dataset.get(position);
            final ImageTypeViewHolder imageTypeViewHolder=(ImageTypeViewHolder) holder;
            imageTypeViewHolder.title.setText(object.title);
            imageTypeViewHolder.subtitle.setText(object.subtitle);
            imageTypeViewHolder.link.setText(object.link);
            imageTypeViewHolder.date.setText(object.date);
            imageTypeViewHolder.id.setText(object.id);
            imageTypeViewHolder.categories.setText(object.categories);

            switch(Integer.parseInt(object.categories)){

                case 10:
                    imageTypeViewHolder.categories.setText("US");
                    break;

                case 51:
                    imageTypeViewHolder.categories.setText("India");
                    break;
                case 13:
                    imageTypeViewHolder.categories.setText("UK");
                    break;
                default:
                    imageTypeViewHolder.categories.setText("Not Found");
            }

            String myid=object.id;
            // Set prefrences
            sharedPreferences=mContext.getSharedPreferences("datakey",mContext.MODE_PRIVATE);

            String getdata=sharedPreferences.getString("postkey", String.valueOf(myColor2));
            if(getdata!=null) {
                //Toast.makeText(mContext,"Data Value Already Set: "+myid+" Data"+getdata,Toast.LENGTH_SHORT).show();

                if (getdata.equals(myid)) {
                  //  Toast.makeText(mContext, "Data Value Match: " + getdata, Toast.LENGTH_SHORT).show();
                    imageTypeViewHolder.title.setTextColor(Color.BLUE);
                    //Toast.makeText(mContext, "Data Value Already Setting: " + getdata, Toast.LENGTH_SHORT).show();
                }

            }


           // Toast.makeText(mContext,"GET TOTAL DATA SIZE: "+dataset.size(),Toast.LENGTH_SHORT).show();
            // Set On Click Listner
            imageTypeViewHolder.title.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    //imageTypeViewHolder.title.setTextColor(myColor2);
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    editor.putString("postkey",object.id);
                    editor.apply();


                  String getdata=sharedPreferences.getString("postkey","No Data");

                  //TODO: call Webview

                  // CALL WEBVIEW FILE
                    Intent intent=new Intent(mContext, WPPostDetails.class);
                    intent.putExtra("itemPosition",position);
                    intent.putExtra("link",object.link);

                    mContext.startActivity(intent);

                }
            });


        } else if (holder instanceof LoadingViewHolder) {
            LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
            loadingViewHolder.progressBar.setIndeterminate(true);
        }
    }

    public static class ImageTypeViewHolder extends RecyclerView.ViewHolder{
        TextView title,subtitle,link,date,id,categories;
        ImageView imageView;

        public ImageTypeViewHolder(View itemView){
            super(itemView);
            this.title=(TextView)itemView.findViewById(R.id.title);
            this.link=(TextView)itemView.findViewById(R.id.link);
            this.subtitle=(TextView) itemView.findViewById(R.id.subtitle);
            this.imageView=(ImageView) itemView.findViewById(R.id.icon);
            this.date=(TextView)itemView.findViewById(R.id.date);
            this.id=(TextView)itemView.findViewById(R.id.id);
            this.categories=(TextView)itemView.findViewById(R.id.education);
        }
    }

    @Override
    public int getItemCount() {
        return dataset == null ? 0 : dataset.size();
    }

    public void setLoaded() {
        isLoading = false;
    }

    private class LoadingViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;

        public LoadingViewHolder(View view) {
            super(view);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
        }
    }

    public void setFilter(ArrayList<Model> newList){
        dataset=new ArrayList<>();
        dataset.addAll(newList);
        notifyDataSetChanged();
    }
}

File Name: Model.java

package com.ejobbox.ejobbox;



import java.util.ArrayList;
import java.util.Date;

public class Model {
    public static final int IMAGE_TYPE=1;
    public String title;
    public String subtitle;
    public String Image;
    public String link;
    public String id;
    public String categories;
    public String date;
    public int type;

    public Model(int mtype, String mtitle, String msubtitle,String mdate, String mlink,String mid,String mcategories){
        this.title=mtitle;
        this.subtitle=msubtitle;
        this.type=mtype;
        //this.Image=image;
        this.link=mlink;
        this.date=mdate;
        this.id=mid;
        this.categories=mcategories;
    }

}

Solution

  • Simply iterate through list:

    List<Integer> categoryList = object.getCategories();
        for(Integer catId:categoryList)
        {
            switch(catId)
            {
                case 10:
                    imageTypeViewHolder.categories.setText("US");
                    break;
    
                case 51:
                    imageTypeViewHolder.categories.setText("India");
                    break;
                case 13:
                    imageTypeViewHolder.categories.setText("UK");
                    break;
                default:
                    imageTypeViewHolder.categories.setText("Not Found");
            }
        }