嗨我有一个自定义列表视图,但我无法编码正确的方法只过滤/搜索CustomListView中的特定人名(基本上我从JSON获取并存储在ArrayList中)...请任何人帮助我使用此方法筛选CustomListViewAdapter.java
这是我的Main_Activity.Java代码
array111.add(new AshTable(test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15, test16, test17, test18, test19, test20, test21, test22, test23, test24, test25, test26, test27, test28, test29, test30, test31, test32, test33, test34, test35, test36, test37, test38, test39, test40, test41, test42));
}
} catch (JSONException e) {
e.printStackTrace();
}
final CustomListViewAdapter adapter = new CustomListViewAdapter(getActivity(), R.layout.home_list_view, array111);
lstv11.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = inputSearch.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
}
});
mSwipeRefreshLayout.setRefreshing(false);
}
}
这是我的CustomListViewAdapter.java代码
public class CustomListViewAdapter extends ArrayAdapter实现了Filterable {
ArrayList<AshTable> prod;
Context context;
int resource;
public CustomListViewAdapter(Context context, int resource, ArrayList<AshTable> prod) {
super(context, resource, prod);
this.context=context;
this.resource=resource;
this.prod=prod;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView==null){
LayoutInflater layoutInflater=(LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView=layoutInflater.inflate(R.layout.list_view,null,true);
}
AshTable pro=getItem(position);
final String st1=pro.getCompname();
final String st2=pro.getPersonname();
final String st3=pro.getAddress();
final String st4=pro.getMailid();
final String st5=pro.getMob1();
final String st6=pro.getMob2();
final String st7=pro.getShowname();
final String st8=pro.getDispatchdate();
final String st9=pro.getDispatchtime();
final String st10=pro.getStatedate();
final String st11=pro.getStarttime();
final String st12=pro.getSetupdate();
final String st13=pro.getSetuptime();
final String st14=pro.getEnddate();
final String st15=pro.getEndtime();
final String st16=pro.getDismanteldate();
final String st17=pro.getDismanteltime();
final String st18=pro.getVenu();
final String st19=pro.getVenuaddress();
final String st20=pro.getBoardsize();
final String st21=pro.getOverallsqft();
final String st22=pro.getRatepersqft();
final String st23=pro.getTotalamt();
final String st24=pro.getTransport();
final String st25=pro.getStage();
final String st26=pro.getPower();
final String st27=pro.getOthercost1();
final String st28=pro.getOthercost2();
final String st29=pro.getGrossamt();
final String st30=pro.getBillrequired();
final String st31=pro.getBillinnameof();
final String st32=pro.getServicetax();
final String st33=pro.getTotalamt2();
final String st34=pro.getAdvanceamt();
final String st35=pro.getCreditperiod();
final String st36=pro.getPhotoname();
final String st37=pro.getPhotomob();
final String st38=pro.getPhotoemail();
final String st39=pro.getMarketingname();
final String st40=pro.getRemark();
TextView txt1=(TextView)convertView.findViewById(R.id.textView11);
txt1.setText(pro.getPersonname());
TextView txt2=(TextView)convertView.findViewById(R.id.textView22);
txt2.setText(pro.getMob1());
TextView txt3=(TextView)convertView.findViewById(R.id.textView33);
txt3.setText(pro.getShowname());
TextView txt5=(TextView)convertView.findViewById(R.id.textView44);
txt5.setText(pro.getStatedate());
TextView txt4=(TextView)convertView.findViewById(R.id.textView6);
txt4.setText(pro.getEnddate());
TextView txt6=(TextView)convertView.findViewById(R.id.textView55);
txt6.setText(pro.getBoardsize());
TextView txt7=(TextView)convertView.findViewById(R.id.teextView);
txt7.setText("( "+pro.getCompname()+" )");
Button imgb=(Button) convertView.findViewById(R.id.imageButton2);
imgb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent().setClass(getContext(), Full_info_page.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.putExtra("name1", String.valueOf(st1));
intent.putExtra("name2", String.valueOf(st2));
intent.putExtra("name3", String.valueOf(st3));
intent.putExtra("name4", String.valueOf(st4));
intent.putExtra("name5", String.valueOf(st5));
intent.putExtra("name6", String.valueOf(st6));
intent.putExtra("name7", String.valueOf(st7));
intent.putExtra("name8", String.valueOf(st8));
intent.putExtra("name9", String.valueOf(st9));
intent.putExtra("name10", String.valueOf(st10));
intent.putExtra("name11", String.valueOf(st11));
intent.putExtra("name12", String.valueOf(st12));
intent.putExtra("name13", String.valueOf(st13));
intent.putExtra("name14", String.valueOf(st14));
intent.putExtra("name15", String.valueOf(st15));
intent.putExtra("name16", String.valueOf(st16));
intent.putExtra("name17", String.valueOf(st17));
intent.putExtra("name18", String.valueOf(st18));
intent.putExtra("name19", String.valueOf(st19));
intent.putExtra("name20", String.valueOf(st20));
intent.putExtra("name21", String.valueOf(st21));
intent.putExtra("name22", String.valueOf(st22));
intent.putExtra("name23", String.valueOf(st23));
intent.putExtra("name24", String.valueOf(st24));
intent.putExtra("name25", String.valueOf(st25));
intent.putExtra("name26", String.valueOf(st26));
intent.putExtra("name27", String.valueOf(st27));
intent.putExtra("name28", String.valueOf(st28));
intent.putExtra("name29", String.valueOf(st29));
intent.putExtra("name30", String.valueOf(st30));
intent.putExtra("name31", String.valueOf(st31));
intent.putExtra("name32", String.valueOf(st32));
intent.putExtra("name33", String.valueOf(st33));
intent.putExtra("name34", String.valueOf(st34));
intent.putExtra("name35", String.valueOf(st35));
intent.putExtra("name36", String.valueOf(st36));
intent.putExtra("name37", String.valueOf(st37));
intent.putExtra("name38", String.valueOf(st38));
intent.putExtra("name39", String.valueOf(st39));
intent.putExtra("name40", String.valueOf(st40));
context.startActivity(intent);
}
});
return convertView;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
if (charText.length() == 0) {
} else {
}
notifyDataSetChanged();
}
}
有人请帮助我使用此方法过滤器仅过滤人名或日期.... 提前感谢
答案 0 :(得分:0)
基本上,您需要检查列表中的每个值以查看它是否符合过滤器要求,然后将其添加到临时列表中。获得完整列表后,将临时列表换成数组适配器列表,在您的情况prod
中。您还必须考虑无过滤器或检索完整列表,因此您还需要保留对该列表的引用。您所要做的就是创建另一个数组,并将原始列表的副本分配给引用列表。所有这些看起来都像这样:
//Create reference list to keep track of complete list.
ArrayList<AshTable> refList = new ArrayList<AshTable>();
public CustomListViewAdapter(Context context, int resource, ArrayList<AshTable> prod) {
super(context, resource, prod);
this.context=context;
this.resource=resource;
this.prod=prod;
this.refList = prod;
}
//Filter method
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
if (charText.length() == 0) {
//if there is no data to filter, replace complete list
prod.clear();
prod.addAll(refList);
} else {
//Temp array to store values that meet filter requirements
ArrayList<AshTable> tempTable = new ArrayList<AshTable>();
for(AshTable pro : prod){
//here you can add any other filter requirements
//just iterate through each item and check it.
if(pro.getPersonname().contains(charText)){
tempTable.add(pro);
}
}
//put filtered data into arraylist
prod.clear();
prod.addAll(tempTable);
}
notifyDataSetChanged();
}
答案 1 :(得分:0)
根据此示例代码更改adapeter类,并专注于 getFilter方法,尽管我已粘贴完整的适配器类供您参考。
public class CustomListAdapter extends BaseAdapter
{
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
private List<Movie> movieItemSearched = null;
private List<Movie> originalMovieList;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
this.originalMovieList = new ArrayList<Movie>(movieItems);
this.movieItemSearched = new ArrayList<Movie>();
this.movieItemSearched.addAll(movieItems);
}
//Add Below Method
public void reloadData(){
this.originalMovieList = new ArrayList<Movie>(movieItems);
notifyDataSetChanged();
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView rating = (TextView) convertView.findViewById(R.id.rating);
TextView genre = (TextView) convertView.findViewById(R.id.genre);
TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
TextView appuser = (TextView) convertView.findViewById(R.id.appuser);
TextView reg = (TextView) convertView.findViewById(R.id.reg);
// LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
// iv.setLayoutParams(layoutParams);
// getting movie data for the row
Movie m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
// rating
rating.setText(m.getRating());
// genre
genre.setText(m.getGenre());
appuser.setText(m.getApp_user());
reg.setText(m.getReg());
year.setText(String.valueOf(m.getYear()));
return convertView;
}
public Filter getFilter(ComponentName cn) {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<Movie> list = originalMovieList;
int count = list.size();
final ArrayList<Movie> nlist = new ArrayList<Movie>(count);
String filterableString1,filterableString2;
/*
for (int i = 0; i < count; i++) {
filterableString = list.get(i).getTitle();
if (filterableString.toLowerCase().contains(filterString)) {
// nlist.add(filterableString);
nlist.add(list.get(i));
}
}
*/
for (Movie movie: originalMovieList) {
filterableString1 = movie.getTitle();
filterableString2 = movie.getRating();
if (filterableString1.toLowerCase().contains(filterString)||filterableString2.toLowerCase().contains(filterString)) {
// nlist.add(filterableString);
nlist.add(movie);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
movieItems.clear();
movieItems.addAll((ArrayList<Movie>) results.values);
notifyDataSetChanged();
}
};
}
}
在搜索视图中使用此方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.main, menu);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter(getComponentName()).filter(newText);
adapter.notifyDataSetChanged();
return false;
}
});
return true;
}
答案 2 :(得分:0)
请使用此代码:
array111.add(new AshTable(test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15, test16, test17, test18, test19, test20, test21, test22, test23, test24, test25, test26, test27, test28, test29, test30, test31, test32, test33, test34, test35, test36, test37, test38, test39, test40, test41, test42));
}
} catch (JSONException e) {
e.printStackTrace();
}
final CustomListViewAdapter adapter = new CustomListViewAdapter(getActivity(), R.layout.home_list_view, array111);
lstv11.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@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) {
if (YourClassName.this.adapter != null)
((YourAdapterClassName) YourClassName.this.adapter).getFilter().filter(arg0);
}
});
mSwipeRefreshLayout.setRefreshing(false);
}
}
和您的适配器类
public class YourAdapterClass extends BaseAdapter implements Filterable {
ArrayList<AshTable> prod;
ArrayList<AshTable> tempList;
Context context;
int resource;
public CustomListViewAdapter(Context context, int resource, ArrayList<AshTable> prod) {
super(context, resource, prod);
this.context=context;
this.resource=resource;
this.prod=prod;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView==null){
LayoutInflater layoutInflater=(LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView=layoutInflater.inflate(R.layout.list_view,null,true);
}
AshTable pro=getItem(position);
final String st1=pro.getCompname();
final String st2=pro.getPersonname();
final String st3=pro.getAddress();
final String st4=pro.getMailid();
final String st5=pro.getMob1();
final String st6=pro.getMob2();
final String st7=pro.getShowname();
final String st8=pro.getDispatchdate();
final String st9=pro.getDispatchtime();
final String st10=pro.getStatedate();
final String st11=pro.getStarttime();
final String st12=pro.getSetupdate();
final String st13=pro.getSetuptime();
final String st14=pro.getEnddate();
final String st15=pro.getEndtime();
final String st16=pro.getDismanteldate();
final String st17=pro.getDismanteltime();
final String st18=pro.getVenu();
final String st19=pro.getVenuaddress();
final String st20=pro.getBoardsize();
final String st21=pro.getOverallsqft();
final String st22=pro.getRatepersqft();
final String st23=pro.getTotalamt();
final String st24=pro.getTransport();
final String st25=pro.getStage();
final String st26=pro.getPower();
final String st27=pro.getOthercost1();
final String st28=pro.getOthercost2();
final String st29=pro.getGrossamt();
final String st30=pro.getBillrequired();
final String st31=pro.getBillinnameof();
final String st32=pro.getServicetax();
final String st33=pro.getTotalamt2();
final String st34=pro.getAdvanceamt();
final String st35=pro.getCreditperiod();
final String st36=pro.getPhotoname();
final String st37=pro.getPhotomob();
final String st38=pro.getPhotoemail();
final String st39=pro.getMarketingname();
final String st40=pro.getRemark();
TextView txt1=(TextView)convertView.findViewById(R.id.textView11);
txt1.setText(pro.getPersonname());
TextView txt2=(TextView)convertView.findViewById(R.id.textView22);
txt2.setText(pro.getMob1());
TextView txt3=(TextView)convertView.findViewById(R.id.textView33);
txt3.setText(pro.getShowname());
TextView txt5=(TextView)convertView.findViewById(R.id.textView44);
txt5.setText(pro.getStatedate());
TextView txt4=(TextView)convertView.findViewById(R.id.textView6);
txt4.setText(pro.getEnddate());
TextView txt6=(TextView)convertView.findViewById(R.id.textView55);
txt6.setText(pro.getBoardsize());
TextView txt7=(TextView)convertView.findViewById(R.id.teextView);
txt7.setText("( "+pro.getCompname()+" )");
Button imgb=(Button) convertView.findViewById(R.id.imageButton2);
imgb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent().setClass(getContext(), Full_info_page.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.putExtra("name1", String.valueOf(st1));
intent.putExtra("name2", String.valueOf(st2));
intent.putExtra("name3", String.valueOf(st3));
intent.putExtra("name4", String.valueOf(st4));
intent.putExtra("name5", String.valueOf(st5));
intent.putExtra("name6", String.valueOf(st6));
intent.putExtra("name7", String.valueOf(st7));
intent.putExtra("name8", String.valueOf(st8));
intent.putExtra("name9", String.valueOf(st9));
intent.putExtra("name10", String.valueOf(st10));
intent.putExtra("name11", String.valueOf(st11));
intent.putExtra("name12", String.valueOf(st12));
intent.putExtra("name13", String.valueOf(st13));
intent.putExtra("name14", String.valueOf(st14));
intent.putExtra("name15", String.valueOf(st15));
intent.putExtra("name16", String.valueOf(st16));
intent.putExtra("name17", String.valueOf(st17));
intent.putExtra("name18", String.valueOf(st18));
intent.putExtra("name19", String.valueOf(st19));
intent.putExtra("name20", String.valueOf(st20));
intent.putExtra("name21", String.valueOf(st21));
intent.putExtra("name22", String.valueOf(st22));
intent.putExtra("name23", String.valueOf(st23));
intent.putExtra("name24", String.valueOf(st24));
intent.putExtra("name25", String.valueOf(st25));
intent.putExtra("name26", String.valueOf(st26));
intent.putExtra("name27", String.valueOf(st27));
intent.putExtra("name28", String.valueOf(st28));
intent.putExtra("name29", String.valueOf(st29));
intent.putExtra("name30", String.valueOf(st30));
intent.putExtra("name31", String.valueOf(st31));
intent.putExtra("name32", String.valueOf(st32));
intent.putExtra("name33", String.valueOf(st33));
intent.putExtra("name34", String.valueOf(st34));
intent.putExtra("name35", String.valueOf(st35));
intent.putExtra("name36", String.valueOf(st36));
intent.putExtra("name37", String.valueOf(st37));
intent.putExtra("name38", String.valueOf(st38));
intent.putExtra("name39", String.valueOf(st39));
intent.putExtra("name40", String.valueOf(st40));
context.startActivity(intent);
}
});
return convertView;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
Filter.FilterResults results = new FilterResults();
int count = tempList.size();
final ArrayList<AshTable> nlist = new ArrayList<>();
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = tempList.get(i).toString();
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(tempList.get(i));
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
prod = (ArrayList<AshTable>) results.values;
notifyDataSetChanged();
}
}
}
}