如何从json pojo类的arraylist中提取元素

时间:2018-02-22 07:02:40

标签: android json pojo

我想使用@if(request()->path()=='/path/another_path/*') @endif 提取arraylist的数组元素,我已通过json pojo类

数据提取的Json Pojo类

imageTypeViewHolder.categories.setText(object.categories);

总之,当我拿到它时,结果显示[10,51,13]

但我需要使用开关案例或其他逻辑打印此类结果[美国,印度,英国]。所以请让我知道如何做到这一点

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

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

文件名: RecyclerViewAdapter.java(这里我设置了打印值)

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

文件名: Model.java

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

1 个答案:

答案 0 :(得分:0)

简单地遍历列表:

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