过滤后为Recyclerview设置数据集

时间:2017-11-01 06:08:18

标签: java android

我使用onQueryTextChange过滤我的数据,列表过滤器但它仍显示旧数据集(未过滤),这会导致旧列表中的数据,即使它显示新的过滤列表。

有人可以修复我的代码,该代码会在过滤后显示正确的数据集,而未修改的数据集会在用户清除搜索文本时显示。

我会用截图更好地解释它。 在过滤Screenshot 1之前,点击某个项目会为其上的文字干杯。

过滤Screenshot 2后,点击它应该说是HDFC BANK,而是仍然显示未过滤数据集中位置1的数据。

我被告知它是因为我在点击时使用了BankNames List(未过滤)文本,因此发生了这种情况。

有人可以在这里修改我的逻辑,我无法弄明白,即使看起来很简单,我是Android新手。

MainActivity.java

    public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener
 {

    List<DataAdapter> DataAdapterClassList;

    RecyclerView recyclerView;

     final List<DataAdapter> filteredModelList = new ArrayList<>();

    RecyclerView.LayoutManager recyclerViewlayoutManager;

    RecyclerViewAdapter recyclerViewadapter;

    ProgressBar progressBar;

    JsonArrayRequest jsonArrayRequest;

    ArrayList<String> BankNames;

    RequestQueue requestQueue;

    String HTTP_SERVER_URL = "http://hiddenforsecurity.com";

    View ChildView;

    int RecyclerViewClickedItemPOS;

    LinearLayout lyt, icicBtn, hdfcBtn, axisBtn, sbiBtn, pnbBtn, yesBtn;

     private static int firstVisibleInListview;

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


        DataAdapterClassList = new ArrayList<>();

        BankNames = new ArrayList<>();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);

        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        recyclerView.setHasFixedSize(true);

        recyclerViewlayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(recyclerViewlayoutManager);

        lyt = (LinearLayout) findViewById(R.id.Layout1);

        final Intent go = new Intent(MainActivity.this, StateList.class);


        icicBtn = (LinearLayout) findViewById(R.id.icicBtn);
        hdfcBtn = (LinearLayout) findViewById(R.id.hdfcBtn);
        axisBtn = (LinearLayout) findViewById(R.id.axisBtn);
        sbiBtn = (LinearLayout) findViewById(R.id.sbiBtn);
        pnbBtn = (LinearLayout) findViewById(R.id.pnbBtn);
        yesBtn = (LinearLayout) findViewById(R.id.yesBtn);

        final Intent i = new Intent(this, StateList.class);


        // JSON data web call function call from here.
        JSON_WEB_CALL();

        //RecyclerView Item click listener code starts from here.
        recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

            GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {

                @Override
                public boolean onSingleTapUp(MotionEvent motionEvent) {

                    return true;
                }

            });

            @Override
            public boolean onInterceptTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

                ChildView = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

                if (ChildView != null && gestureDetector.onTouchEvent(motionEvent)) {

                    //Getting RecyclerView Clicked item value.
                    RecyclerViewClickedItemPOS = Recyclerview.getChildAdapterPosition(ChildView);
                    Toast.makeText(MainActivity.this, BankNames.get(RecyclerViewClickedItemPOS), Toast.LENGTH_SHORT).show();
                    //Printing RecyclerView Clicked item clicked value using Toast Message.
                    //i.putExtra("selectedbank", BankNames.get(RecyclerViewClickedItemPOS));
                    //startActivity(i);

                }

                return false;
            }

            @Override
            public void onTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });

        icicBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                go.putExtra("selectedbank", "ICICI BANK LIMITED");
                startActivity(go);
            }
        });

        axisBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                go.putExtra("selectedbank", "AXIS BANK");
                startActivity(go);
            }
        });

        hdfcBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                go.putExtra("selectedbank", "HDFC BANK");
                startActivity(go);
            }
        });

        sbiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                go.putExtra("selectedbank", "STATE BANK OF INDIA");
                startActivity(go);
            }
        });
        pnbBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                go.putExtra("selectedbank", "PUNJAB NATIONAL BANK");
                startActivity(go);
            }
        });
        yesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                go.putExtra("selectedbank", "YES BANK");
                startActivity(go);
            }
        });

    }


    public void JSON_WEB_CALL(){

        progressBar.setVisibility(View.VISIBLE);

        jsonArrayRequest = new JsonArrayRequest(HTTP_SERVER_URL,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

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

                    }
                });

        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonArrayRequest);
    }

    public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

        for(int i = 0; i<array.length(); i++) {

            DataAdapter GetDataAdapter2 = new DataAdapter();

            JSONObject json = null;
            try {
                json = array.getJSONObject(i);


                GetDataAdapter2.setBankName(json.getString("BANK"));

                //Adding subject name here to show on click event.
                BankNames.add(json.getString("BANK"));

            }
            catch (JSONException e)
            {

                e.printStackTrace();
            }

            DataAdapterClassList.add(GetDataAdapter2);

        }

        progressBar.setVisibility(View.GONE);

        recyclerViewadapter = new RecyclerViewAdapter(DataAdapterClassList, this);

        recyclerView.setAdapter(recyclerViewadapter);

    }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.menu_search, menu);

         MenuItem item = menu.findItem(R.id.action_search);
         SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);

         searchView.setOnQueryTextListener(this);

         return super.onCreateOptionsMenu(menu);
     }

     @Override
     public boolean onQueryTextSubmit(String query) {
         return false;
     }

     @Override
     public boolean onQueryTextChange(String newText) {


         final List<DataAdapter> filteredModelList = filter(DataAdapterClassList, newText);
         if (filteredModelList.size() > 0) {
             recyclerViewadapter.setFilter(filteredModelList);
             return true;
         } else {
             Toast.makeText(MainActivity.this, "Not Found", Toast.LENGTH_SHORT).show();
             return false;
         }

     }

     private List<DataAdapter> filter(List<DataAdapter> models, String query) {
         query = query.toLowerCase();

         final List<DataAdapter> filteredModelList = new ArrayList<>();
         for (DataAdapter model : models) {
             final String text = model.getBankName().toLowerCase();
             if (text.contains(query)) {
                 filteredModelList.add(model);
             }
         }

         recyclerViewadapter = new RecyclerViewAdapter(filteredModelList, MainActivity.this);
         recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
         recyclerView.setAdapter(recyclerViewadapter);
         recyclerViewadapter.notifyDataSetChanged();
         return filteredModelList;
     }

 } 

RecyclerviewAdapter.java

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    Context context;

    public DataAdapter dataAdapter;

    List<DataAdapter> dataAdapters;

    public RecyclerViewAdapter(List<DataAdapter> getDataAdapter, Context context){

        super();

        this.dataAdapters = getDataAdapter;
        this.context = context;
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }


    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        dataAdapter =  dataAdapters.get(position);

        viewHolder.TextViewName.setText(dataAdapter.getBankName());

    }

    @Override
    public int getItemCount() {

        return dataAdapters.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView TextViewName;

        public ViewHolder(View itemView) {
            super(itemView);
            TextViewName = (TextView) itemView.findViewById(R.id.TextViewCard) ;

        }
    }

    public void setFilter(List<DataAdapter> bankNames) {
        dataAdapters = new ArrayList<>();
        dataAdapters.addAll(bankNames);
        notifyDataSetChanged();
    }
} 

2 个答案:

答案 0 :(得分:0)

为filteredList创建全局变量并从该列表中获取数据。发生错误是因为您正在从完整列表中进行烘烤。未过滤列表

答案 1 :(得分:0)

试试这个

  

这个问题可能是您的 BankNames.get(RecyclerViewClickedItemPOS) 而不是来自 Oldlist

打印filteredModelList的吐司

删除此

//Getting RecyclerView Clicked item value.
  RecyclerViewClickedItemPOS = Recyclerview.getChildAdapterPosition(ChildView);
  Toast.makeText(MainActivity.this, BankNames.get(RecyclerViewClickedItemPOS), Toast.LENGTH_SHORT).show();

并在Adapter类中设置Toast

viewHolder.TextViewName..setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Toast.makeText(context,  dataAdapter.getBankName(), Toast.LENGTH_SHORT).show();
            }
        });