【问题标题】:Not showing proper result on listview after filtering it过滤后未在列表视图上显示正确的结果
【发布时间】:2016-07-29 04:17:43
【问题描述】:

我有通过在适配器类中使用过滤器方法完成的搜索视图,所以问题是,每当我输入全名或数字进行搜索时,它不显示结果仅显示 ListView 的第一项但如果我输入第一个字符或编号它的显示结果,所以我希望每当我输入全名或数字时都应该有结果,我进行了调试,我发现我的代码中的所有内容都是正确的,请帮助。

public class AdmitPatientAdapter extends BaseAdapter {

    private Activity activity;
    ArrayList<HashMap<String, String>> data;
    ArrayList<HashMap<String, String>> TempArrList = new ArrayList<>();
    private static LayoutInflater inflater = null;
    public static final String TAG_MRDNO = "mrd_no";


    public AdmitPatientAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data = d;
        TempArrList.addAll(d);
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    public int getCount() {
        return TempArrList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        TempArrList.clear();
        if (charText.length() == 0) {
            TempArrList.addAll(data);

        } else {
            for (int i = 0; i < data.size(); i++) {
                HashMap<String, String> tMap;
                tMap = data.get(i);
                if (charText.length() != 0 && tMap.get("mrd_no").toLowerCase(Locale.getDefault()).contains(charText)) {//mrd_no
                    TempArrList.add(tMap);
                } else if (charText.length() != 0 && tMap.get("pname").toLowerCase(Locale.getDefault()).contains(charText)) {//pname
                    TempArrList.add(tMap);
                }
            }

        }
        notifyDataSetChanged();

    }


    public View getView(int position, View convertView, ViewGroup parent) {

        View viw = convertView;
        if (convertView == null)
            viw = inflater.inflate(R.layout.ip_ptn_items, null);
        TextView txt_Mr_dno = (TextView) viw.findViewById(R.id.txtMrdno);
        TextView txt_pitnt_Name = (TextView) viw.findViewById(R.id.txtpitntName);
        TextView txt_Bed_no = (TextView) viw.findViewById(R.id.txtBedno);
        TextView txt_Dob = (TextView) viw.findViewById(R.id.txtDob);

        TextView txt_drNme = (TextView) viw.findViewById(R.id.txtDr);
        TextView txt_Sex = (TextView) viw.findViewById(R.id.txtSex);
        TextView txt_Wrdnm = (TextView) viw.findViewById(R.id.txtWrdnm);

        HashMap<String, String> item = new HashMap<String, String>();
        item = data.get(position);
        String mrd_no = item.get(TAG_MRDNO);
        item.put(TAG_MRDNO, mrd_no);
        mrd_no = item.get(TAG_MRDNO);

        if (mrd_no.endsWith("*")) {
            txt_Mr_dno.setTextColor(Color.MAGENTA);
            txt_pitnt_Name.setTextColor(Color.MAGENTA);
            txt_Dob.setTextColor(Color.MAGENTA);
            txt_Sex.setTextColor(Color.MAGENTA);
            txt_Wrdnm.setTextColor(Color.MAGENTA);
            txt_Bed_no.setTextColor(Color.MAGENTA);


        } else {
            txt_Mr_dno.setTextColor(Color.BLACK);
            txt_pitnt_Name.setTextColor(Color.BLACK);
            txt_Dob.setTextColor(Color.BLACK);
            txt_Sex.setTextColor(Color.BLACK);
            txt_Wrdnm.setTextColor(Color.BLUE);
            txt_Bed_no.setTextColor(Color.BLUE);

        }

        //Setting all values in listview
        txt_Mr_dno.setText(item.get("mrd_no"));
        txt_pitnt_Name.setText(item.get("pname"));
        txt_Bed_no.setText(item.get("bed_no"));
        txt_Dob.setText(item.get("dob"));
        //txt_admit_Date.setText(item.get("admission_date"));
        txt_Sex.setText(item.get("sex"));
        txt_Wrdnm.setText(item.get("nursingstation"));
        txt_drNme.setText(item.get("doctor"));


        //  item = data.get(position);
        ///  String userType = item.get(TAG_UTYPE);
        //  item.put(TAG_UTYPE, mrd_no);
        //  userType = item.get(TAG_UTYPE);

        try {
            if (item.get("userType").equals("doctor")) {
                txt_drNme.setVisibility(View.INVISIBLE);
            } else {
                txt_drNme.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return viw;

    }

}

【问题讨论】:

    标签: android listview filter adapter


    【解决方案1】:

    使用此代码::

    适配器类:

    p

    ublic class ListViewAdapter extends BaseAdapter { 
    
    // Declare Variables 
    Context mContext; 
    
    
    
    LayoutInflater inflater; 
    private List<WorldPopulation> worldpopulationlist = null; 
    private ArrayList<WorldPopulation> arraylist; 
    
    public ListViewAdapter(Context context, List<WorldPopulation> worldpopulationlist) { 
    mContext = context; 
    this.worldpopulationlist = worldpopulationlist; 
    inflater = LayoutInflater.from(mContext); 
    this.arraylist = new ArrayList<WorldPopulation>(); 
    this.arraylist.addAll(worldpopulationlist); 
    
    } 
    
    public class ViewHolder { 
    TextView beamword; 
    
    } 
    
    @Override 
    public int getCount() { 
    return worldpopulationlist.size(); 
    } 
    
    @Override 
    public WorldPopulation getItem(int position) { 
    return worldpopulationlist.get(position); 
    } 
    
    @Override 
    public long getItemId(int position) { 
    return position; 
    } 
    
    public View getView(final int position, View view, ViewGroup parent) { 
    final ViewHolder holder; 
    if (view == null) { 
    holder = new ViewHolder(); 
    view = inflater.inflate(R.layout.list_single, null); 
    // Locate the TextViews in listview_item.xml 
    holder.beamword = (TextView) view.findViewById(R.id.textviewitem); 
    
    view.setTag(holder); 
    } else { 
    holder = (ViewHolder) view.getTag(); 
    } 
    // Set the results into TextViews 
    holder.beamword.setText(worldpopulationlist.get(position).getBeamword()); 
    
    
    return view; 
    } 
    
    // Filter Class 
    public void filter(String charText) { 
    charText = charText.toLowerCase(Locale.getDefault()); 
    worldpopulationlist.clear(); 
    if (charText.length() == 0) { 
    worldpopulationlist.addAll(arraylist); 
    }  
    else  
    { 
    for (WorldPopulation wp : arraylist)  
    { 
    if (wp.getBeamword().toLowerCase(Locale.getDefault()).contains(charText))  
    { 
    worldpopulationlist.add(wp); 
    } 
    } 
    } 
    notifyDataSetChanged(); 
    } 
    
    } 
    

    在你的课堂上

    editsearch.addTextChangedListener(new TextWatcher() { 
    
    @Override 
    public void afterTextChanged(Editable arg0) { 
    // TODO Auto-generated method stub 
    String text = editsearch.getText().toString().toLowerCase(Locale.getDefault()); 
    adapter.filter(text); 
    } 
    
    @Override 
    public void beforeTextChanged(CharSequence arg0, int arg1, 
    int arg2, int arg3) { 
    // TODO Auto-generated method stub 
    } 
    
    @Override 
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, 
    int arg3) { 
    // TODO Auto-generated method stub 
    } 
    }); 
    

    【讨论】:

    • 在我的代码中发现所有内容都正确我认为顺便更改代码并不好,谢谢您的帮助
    猜你喜欢
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多