如何仅将JSON中的唯一值添加到recyclerView

时间:2018-11-28 09:57:16

标签: android-recyclerview duplicates android-adapter

我有一个包含列表的JSON。我将该列表设置到“回收者视图”中。问题在于它包含重复的条目。我想要一个只有唯一条目的列表,所有其他重复条目都应被过滤掉。 这是我的活动课

public class Departments extends AppCompatActivity {
String url ="http://192.168.1.11/images/MstArticle.json";
RecyclerView department_recyclerView;
private Department_Adapter department_adapter;
static Context ctx;
private static List<Department_model> departmentModelList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_departments);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ctx = Departments.this;

    department_recyclerView = (RecyclerView) findViewById(R.id.department_recyclerView);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    department_recyclerView.setLayoutManager(mLayoutManager);
    department_recyclerView.setItemAnimator(new DefaultItemAnimator());
    department_recyclerView.addItemDecoration(new DividerItemDecoration(Departments.this, DividerItemDecoration.VERTICAL));

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {

                JSONObject js = new JSONObject(response);
                JSONArray jsonArray = js.optJSONArray("Article");
                String InvDepartmentId = null, InvDepartmentName = null, InvCategoryId = null,InvCategoryName = null,InvSubCategoryId = null, InvSubCategoryName= null,ArticleId = null, ArticleNo= null, ArticleWSP= null,CreatedOn= null, LastUpdate = null;
                departmentModelList = new ArrayList<>();
                String items[] = new String[departmentModelList.size()];
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    InvDepartmentId = jsonObject.getString("InvDepartmentId");
                    InvDepartmentName = jsonObject.getString("InvDepartmentName");
                    InvCategoryId = jsonObject.getString("InvCategoryId");
                    InvCategoryName = jsonObject.getString("InvCategoryName");
                    InvSubCategoryId = jsonObject.getString("InvSubCategoryId");
                    InvSubCategoryName = jsonObject.getString("InvSubCategoryName");
                    ArticleId = jsonObject.getString("ArticleId");
                    ArticleNo = jsonObject.getString("ArticleNo");
                    ArticleWSP = jsonObject.getString("ArticleWSP");
                    CreatedOn = jsonObject.getString("CreatedOn");
                    LastUpdate = jsonObject.getString("LastUpdate");

                    Department_model obj = new Department_model();
                    obj.setDepartment_Name(InvDepartmentName);
                    obj.setDepartment_ID(InvDepartmentId);
                    departmentModelList.add(obj);



                }
                if(departmentModelList.size()!=0){
                    department_adapter = new Department_Adapter(Departments.this, departmentModelList);
                    department_recyclerView.setAdapter(department_adapter);
                }


                SharedpreferenceUtility.getInstance(ctx).putString(Constants.DepID, InvDepartmentId);
                SharedpreferenceUtility.getInstance(ctx).putString(Constants.DepName, InvDepartmentName);
                SharedpreferenceUtility.getInstance(ctx).putString(Constants.CatID, InvCategoryId);
                SharedpreferenceUtility.getInstance(ctx).putString(Constants.CatName, InvCategoryName);
                SharedpreferenceUtility.getInstance(ctx).putString(Constants.SubCatID, InvSubCategoryId);
                SharedpreferenceUtility.getInstance(ctx).putString(Constants.SubCatName, InvSubCategoryName);
                SharedpreferenceUtility.getInstance(ctx).putString(Constants.ArticleId, ArticleId);
                SharedpreferenceUtility.getInstance(ctx).putString(Constants.ArticleNo, ArticleNo);
                SharedpreferenceUtility.getInstance(ctx).putString(Constants.ArticleWSP, ArticleWSP);
                SharedpreferenceUtility.getInstance(ctx).putString(Constants.CreatedOn, CreatedOn);
                SharedpreferenceUtility.getInstance(ctx).putString(Constants.LastUpdate, LastUpdate);

            } catch (Exception e) {

            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }
    );

    RequestQueue requestQueue = Volley.newRequestQueue(ctx);
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            20000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    requestQueue.add(stringRequest);
}

}

这是我的适配器

public class Department_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static Context context;
private ArrayList<Department_model> arraylist;
private List<Department_model> models_list;


public Department_Adapter(Context context, List<Department_model> department_modelList) {
    this.models_list = department_modelList;
    this.arraylist = new ArrayList<Department_model>();
    this.arraylist.addAll(department_modelList);
    this.context = context;
}


public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.departments_childview, viewGroup, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

    final ViewHolder myViewHolder = (ViewHolder)viewHolder;
    final Department_model model = models_list.get(i);
    myViewHolder.department_text.setText(model.getDepartment_Name());
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView department_text;
    ImageView right_arrow;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        department_text = (TextView) itemView.findViewById(R.id.department_text);
        right_arrow = (ImageView) itemView.findViewById(R.id.right_arrow);
    }
}

}

任何人都可以帮助我如何在Recycler视图上仅显示唯一的结果吗?我是Android新手,所以非常感谢您的帮助。谢谢!

0 个答案:

没有答案