基于微调器选择显示ListView

时间:2019-07-10 08:05:34

标签: java android

当我选择一个选项E.G时,我已经创建了一个微调框:“资产编号”使用资产编号显示数据,或者如果我选择“序列号”则必须使用序列号显示数据

我尝试使用Radio Group,它可以显示数据,但现在我希望它旋转。

MainActivity类

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

    setContentView(R.layout.activity_main);


    radiogroup = findViewById(R.id.radiogroup);
    radio_assert =  findViewById(R.id.radio_assert);
    radio_serial = findViewById(R.id.radio_serial);
    searchview=  findViewById(R.id.searchview);



    listview = findViewById(R.id.listview);
    ArrayList list = getListData();
    Adapter = new ListAdapter(this, list);
    listview.setAdapter(Adapter);



    radiogroup.setOnCheckedChangeListener(new 
    RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
    switch(checkedId)
    {
    case R.id.radio_assert:
    adapter.getFilter().filter("ASSET");
    break;
    case R.id.radio_serial:
    adapter.getFilter().filter("SERIAL");
    break;
    }
    }
    });



    searchview.setOnQueryTextListener(new 
    SearchView.OnQueryTextListener()
    {
     @Override
     public boolean onQueryTextSubmit(String s)
     {
     Adapter.getFilter().filter(s);
     return true;
     }

     @Override
     public boolean onQueryTextChange(String s)
     {
     Adapter.getFilter().filter(s);
     return true;
     }
     });
     }



     private ArrayList getListData()
     {
     ArrayList<ListItem> results = new ArrayList<>();
     ListItem l1 = new ListItem();
     l1.setNo("ASS001");
     l1.setType("ASSET");
     l1.setMake("LG");
     l1.setModel("Y9");
     results.add(l1);

     ListItem l2 = new ListItem();
     l2.setNo("ASS002");
     l2.setType("ASSET");
     l2.setMake("SAMSUNG");
     l2.setModel("Y5");
     results.add(l2);

     ListItem l3;
     l3 = new ListItem();
     l3.setNo("SE001");
     l3.setType("SERIAL");
     l3.setMake("OOPO");
     l3.setModel("Y1");
     results.add(l3);

     ListItem l4 = new ListItem();
     l4.setNo("SE002");
     l4.setType("SERIAL");
     l4.setMake("VIVO");
     l4.setModel("VI5");
     results.add(l4);
     return results;
     }

ListAdapter类

     public class ListAdapter extends BaseAdapter implements Filterable 
     {
     private ArrayList<ListItem> listData;
     private ArrayList<ListItem> listFilteredData;
     private LayoutInflater layoutInflater;


    public ListAdapter(Context aContext, ArrayList<ListItem> listData) {
    this.listData = listData;
    this.listFilteredData = listData;
    layoutInflater = LayoutInflater.from(aContext);
    }

    @Override
    public int getCount() {
    return listFilteredData.size();
    }
    @Override
    public Object getItem(int position) {
    return listFilteredData.get(position);
    }
    @Override
    public long getItemId(int position) {
    return position;
    }
    public View getView(int position, View v, ViewGroup vg) {
    ViewHolder holder;
    if (v == null) {
        v = layoutInflater.inflate(R.layout.list_row, null);
        holder = new ViewHolder();
        holder.no = (TextView) v.findViewById(R.id.list_row_no);
        holder.make = (TextView) v.findViewById(R.id.list_row_make);
        holder.model = (TextView) v.findViewById(R.id.list_row_model);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    holder.no.setText(listFilteredData.get(position).getNo());
    holder.make.setText(listFilteredData.get(position).getMake());
    holder.model.setText(listFilteredData.get(position).getModel());
    return v;
    }

   @Override
   public Filter getFilter() {
   return new Filter() {
   @Override
   protected FilterResults performFiltering(CharSequence constraint) {
       FilterResults results = new FilterResults();
       if (constraint == null || constraint.length() == 0) {
           results.count = listData.size();
           results.values = listData;
            }
            else
            {
                ArrayList<ListItem> resultsData = new ArrayList<>();
                ArrayList<ListItem> resultsData1 = new ArrayList<>();
                String searchStr = constraint.toString();

                if(searchStr.equalsIgnoreCase("assert")) {
                    for (ListItem o : listData)
                        if (o.getType().equalsIgnoreCase(searchStr)) 
             resultsData.add(o);
                } else if(searchStr.equalsIgnoreCase("serial")) {
                    for (ListItem o : listData)
                        if (o.getType().equalsIgnoreCase(searchStr)) 
             resultsData.add(o);
                } else
                    {
                    resultsData = listData;
                }

                for (ListItem o : resultsData)
                    if (o.getNo().contains(searchStr)) 
             resultsData1.add(o);

                if(resultsData1.size() == 0)
                    resultsData1 = resultsData;

                results.count = resultsData1.size();
                results.values = resultsData1;
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, 
        FilterResults results) {
            listFilteredData = (ArrayList<ListItem>) results.values;
            notifyDataSetChanged();
              }
           };
        }
        static class ViewHolder {
        TextView no;
        TextView make;
        TextView model;
          }
        }

ListItem类

        public class ListItem {

        private String no;
        private String type;
        private String make;
        private String model;

        public ListItem() {
        }

        public ListItem(String no, String type, String make, String 
        model) {
          this.no = no;
          this.type = type;
          this.make = make;
          this.model = model;
          }
        public String getType() {
        return type;
        }
        public void setType(String type) {
        this.type = type;
        }
        public String getNo() {
        return no;
        }
        public void setNo(String no) {
        this.no = no;
        }
        public String getMake() {
        return make;
        }
        public void setMake(String make) {
        this.make = make;
        }
        public String getModel() {
        return model;
        }
        public void setModel(String model) {`enter code here`
        this.model = model;
               }
        }

预期输出是:如果我在微调器中选择AssetNo,则在列表视图中应使用AssetNo显示详细信息,或者如果我选择Serial No,则应使用Serilno在列表视图中显示详细信息。

1 个答案:

答案 0 :(得分:0)

您将必须实现Spinner(类似于实现无线电组的方式)。 设置微调器后,可以使用OnItemSelectedListener替代用于无线电组的OnCheckedChangeListener。

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        ListItem item = list.get(position)
        adapter.getFilter().filter(item.getType());
    }

    @Override
public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }
});

您可以使用onitemSelected中的位置来确定选择了哪个项目。例如,如果微调器使用项目列表,则可以使用该列表并在指定给bby onItemSelected的位置访问该项目以获取当前选择的项目。

希望对您有所帮助。