Recyclerview数据清算

时间:2016-12-05 04:33:33

标签: android android-recyclerview spinner adapter recycler-adapter

当我从spinner中选择一个项目时,我试图从Recyclerview中删除所有数据。我希望在每个微调项目选项中显示不同的数据。虽然数据正在显示,但它会附加到先前的微调器选择数据。 我希望recyclerview适配器被清除并加载微调器选择的新数据。有人请帮我解决这个问题。 这是我的代码

MainActivity

public class MainActivity extends AppCompatActivity {
    Spinner spinner1;
    RecyclerView recyclerView1;
    private String spinnerValues[];
    private ProgressDialog pdlog;
    public RecyclerDataAdapter adapter;
    ArrayList<ProductModel> productDetails;
    String url="aaa=1";
    ProductModel pm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView1=(RecyclerView)findViewById(R.id.recyclerView1);
        RecyclerView.LayoutManager mLayoutmanager=new GridLayoutManager(getApplicationContext(),2);
        recyclerView1.setLayoutManager(mLayoutmanager);


adapter=new RecyclerDataAdapter();
productDetails=new ArrayList<ProductModel>();
        spinner1=(Spinner)findViewById(R.id.spinner1);
        spinner1.getBackground().setColorFilter(Color.parseColor("#d62041"), PorterDuff.Mode.SRC_ATOP);
        spinnerValues=new String[]{"a","b","c","d","e","f"};
        final ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,R.layout.spinner_style,spinnerValues);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String selectedValue=spinner1.getSelectedItem().toString().replaceAll("\\s+","").toUpperCase();
        Toast.makeText(getApplicationContext(),"selected : "+selectedValue,Toast.LENGTH_SHORT).show();
        if(selectedValue.equals("a")){
            url="aaa=1";}

         if(selectedValue.equals("b")){

            url="aaa=2";}
         if(selectedValue.equals("c")){

            url="aaa=3";}
         if(selectedValue.equals("d")){

            url="aaa=4";}
        if(selectedValue.equals("e")){

            url="aaa=5";}
        if(selectedValue.equals("f")){

            url="aaa=6";}

        Toast.makeText(getApplicationContext(),url,Toast.LENGTH_SHORT).show();
        new FetchDetails().execute();

    }



    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
});
    }


    class FetchDetails extends AsyncTask<String,String,String>
    {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pdlog=new ProgressDialog(MainActivity.this);
            pdlog.setMessage("Loading");
            pdlog.setCancelable(false);
            pdlog.show();
        }

        @Override
        protected String doInBackground(String... strings) {
            pm=null;

            HttpHandler h=new HttpHandler();
            String jsonString=h.makeServiceCall(url);
            if(jsonString!=null){
                try{
                    JSONArray imageArray=new JSONArray(jsonString);
                    for(int i=0;i<imageArray.length();i++){
                        JSONObject jsonData=imageArray.getJSONObject(i);
                         pm=new ProductModel();

                        pm.pro_id=Integer.parseInt(jsonData.getString("pro_id"));
                        pm.pro_name=jsonData.getString("pro_name");
                        pm.pro_image=jsonData.getString("pro_image");

                        productDetails.add(pm);

                        System.out.println("***************************************************");
                        System.out.println("(MainActivity)product  name: "+jsonData.getString("pro_name")+"\n");
                        System.out.println("(MainActivity)pro url : "+jsonData.getString("pro_image"));
                        System.out.println("***************************************************");

                        adapter=new RecyclerDataAdapter(getApplicationContext(),productDetails);

                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if(pdlog.isShowing())
                pdlog.dismiss();

            recyclerView1.setAdapter(adapter);

        }

    }



}

RecyclerDataAdapter

public class RecyclerDataAdapter extends RecyclerView.Adapter<RecyclerDataAdapter.ViewHolder> {
public ArrayList<ProductModel>productDetails;
public Context context1;



public  RecyclerDataAdapter(){

}

public RecyclerDataAdapter(Context context, ArrayList<ProductModel> getproductDetails){
    this.context1=context;
    this.productDetails=getproductDetails;

    System.out.println("********************** inside adapter class constructor *************************");
    for(int i=0;i<productDetails.size();i++){
        System.out.println("/////////////////////// product name : "+productDetails.get(i).pro_name);
        System.out.println("/////////////////////// image url : "+productDetails.get(i).pro_image);
    }
}



@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
    return new ViewHolder(view);
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.product_id.setText(Integer.toString(productDetails.get(position).getPro_id()));
    holder.product_name.setText(productDetails.get(position).getPro_name());
    Picasso.with(context1)
            .load(productDetails.get(position).pro_image)
            .fit()
            .into(holder.imageview1);
}




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

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView product_id,product_name;
    public ImageView imageview1;

    public ViewHolder(View itemView) {
        super(itemView);
        imageview1=(ImageView)itemView.findViewById(R.id.imageView1);
        product_id=(TextView)itemView.findViewById(R.id.product_id);
        product_name=(TextView)itemView.findViewById(R.id.product_name);



    }
}

}

1 个答案:

答案 0 :(得分:1)

你可以做一件事,你必须有一个arrayList(我已使用List名称),数据在recyclelerView中显示。因此,最初将其中的所有数据复制到新的mainarrayList,并且每当您需要为微调器设置新数据时,最初只需使用方法:List.clear()并将所需数据从mainArrayList复制到此List并在recyclerView中显示。

我已经在使用与您提出的概念相同的recyclerView中使用搜索概念。

希望这有帮助!