2016-07-29 21 views
1

アダプタークラスでフィルターメソッドを使用して行った検索ビューがあるので、フルネームまたは検索番号を入力するたびに、表示されない結果が表示されます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; 

    } 

} 

答えて

0

使用このコード::

アダプタクラス:私はと考えてはいけない私のコード内の正しいすべてを発見

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 
} 
}); 
+0

感謝の気持ちでコードを変更するのは良いことです助けを求める –

関連する問題