我有一个屏幕,其中提供了3个RecyclerViews。每个“回收者”视图都有一个关联的微调器。微调器上的项目有72个值,来自JSON,包括重复值,因此我过滤了删除所有重复值的列表。 现在的问题是,每个回收者视图在屏幕上都设置了72次,重复的结果被过滤掉了。如何使它仅显示一次?下面是我的代码:
public class Options extends AppCompatActivity{
RecyclerView Department_recyclerView, Categories_recyclerView,
SubCat_recyclerView;
String url ="http://192.168.1.11/images/MstArticle.json";
private static List<Department_model> department_modelList;
private static List<Category_model> category_modelList;
private static List<Sub_Cat_model> sub_cat_modelList;
private Department_Adapter department_adapter;
private Category_Adapter category_adapter;
private Sub_Cat_Adapter sub_cat_adapter;
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
Department_recyclerView = (RecyclerView) findViewById(R.id.Department_recyclerView);
Categories_recyclerView = (RecyclerView) findViewById(R.id.Categories_recyclerView);
SubCat_recyclerView = (RecyclerView) findViewById(R.id.SubCat_recyclerView);
ctx = Options.this;
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
Department_recyclerView.setLayoutManager(mLayoutManager);
Department_recyclerView.setItemAnimator(new DefaultItemAnimator());
Department_recyclerView.addItemDecoration(new DividerItemDecoration(Options.this, DividerItemDecoration.VERTICAL));
RecyclerView.LayoutManager rLayoutManager = new LinearLayoutManager(getApplicationContext());
Categories_recyclerView.setLayoutManager(rLayoutManager);
Categories_recyclerView.setItemAnimator(new DefaultItemAnimator());
Categories_recyclerView.addItemDecoration(new DividerItemDecoration(Options.this, DividerItemDecoration.VERTICAL));
RecyclerView.LayoutManager nLayoutManager = new LinearLayoutManager(getApplicationContext());
SubCat_recyclerView.setLayoutManager(nLayoutManager);
SubCat_recyclerView.setItemAnimator(new DefaultItemAnimator());
SubCat_recyclerView.addItemDecoration(new DividerItemDecoration(Options.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;
department_modelList = new ArrayList<>();
category_modelList = new ArrayList<>();
sub_cat_modelList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
InvDepartmentName = jsonObject.getString("InvDepartmentName");
InvDepartmentId = jsonObject.getString("InvDepartmentId");
InvCategoryName = jsonObject.getString("InvCategoryName");
InvCategoryId = jsonObject.getString("InvCategoryId");
InvSubCategoryName = jsonObject.getString("InvSubCategoryName");
InvSubCategoryId = jsonObject.getString("InvSubCategoryId");
Department_model obj = new Department_model();
obj.setDepartment_Name(InvDepartmentName);
obj.setDepartment_ID(InvDepartmentId);
department_modelList.add(obj);
Category_model obj1 = new Category_model();
obj1.setCategory_Name(InvCategoryName);
obj1.setCategory_ID(InvCategoryId);
category_modelList.add(obj1);
Sub_Cat_model obj2 = new Sub_Cat_model();
obj2.setSub_Cat_Name(InvSubCategoryName);
obj2.setSub_Cat_ID(InvSubCategoryId);
sub_cat_modelList.add(obj2);
}
if(department_modelList.size()!=0){
department_adapter = new Department_Adapter(Options.this, department_modelList);
Department_recyclerView.setAdapter(department_adapter);
}
if(category_modelList.size()!= 0){
category_adapter = new Category_Adapter(Options.this, category_modelList);
Categories_recyclerView.setAdapter(category_adapter);
}
if(sub_cat_modelList.size()!=0){
sub_cat_adapter = new Sub_Cat_Adapter(Options.this, sub_cat_modelList);
SubCat_recyclerView.setAdapter(sub_cat_adapter);
}
} 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);
}
}
这是“回收者视图”的设计:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Options">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="@dimen/_100sdp"
android:weightSum="3">
<android.support.v7.widget.RecyclerView
android:id="@+id/Department_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
<android.support.v7.widget.RecyclerView
android:id="@+id/Categories_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
<android.support.v7.widget.RecyclerView
android:id="@+id/SubCat_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
我只是提供其中一个Recycler Views的适配器之一的代码,因为其余的都是相同的:
public class Department_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static Context context;
private ArrayList<Department_model> arraylist;
private List<Department_model> models_list;
private ArrayAdapter<String> spinnerArrayAdapter;
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;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.department_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.text.setText("Department");
String items[] = new String[models_list.size()];
Set<String> Dep_list = new HashSet<String>();
for (int j = 0; j < items.length; j++) {
Dep_list.add((items[j]));
Dep_list.remove(null);
String ID[]= new String[models_list.size()];
ID[j] = models_list.get(j).getDepartment_ID();
items[j] = models_list.get(j).getDepartment_Name();
String[] unique = new HashSet<String>(Arrays.asList(items)).toArray(new String[0]);
spinnerArrayAdapter = new ArrayAdapter<String>(context, R.layout.department_list, unique);
spinnerArrayAdapter.setDropDownViewResource(R.layout.category_list);
myViewHolder.spinner.setAdapter(spinnerArrayAdapter);
}
}
@Override
public int getItemCount() {
return models_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView text, spinner_text;
Spinner spinner;
public ViewHolder(@NonNull View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
spinner = (Spinner) itemView.findViewById(R.id.spinner);
spinner_text = (TextView) itemView.findViewById(R.id.spinner_text);
}
}
}