我有通过在适配器类中使用过滤器方法完成的搜索视图,所以问题是,每当我输入搜索的全名或数字时,它不显示结果只显示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 :(得分: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
}
});