我在android中创建了一个列表视图并实现了自定义过滤方法,但是在使用过滤器后,列表视图中的图像与文本不对应。这是因为它只过滤了namesList列表。我需要同时过滤namesList和imagesList。我该怎么做?
public class MyCustomList extends ArrayAdapter<String> implements Filterable {
private final Activity context;
private List<String> filterList;
private List<String> namesList;
private List<String> imagesList;
public CustomList(Activity context,List<String> namesList, List<String> imagesList) {
super(context, R.layout.list_single, namesList);
this.context = context;
this.namesList = namesList;
this.imagesList = imagesList;
this.filterList= namesList;
}
@Override
public int getCount(){
return namesList.size();
}
@Override
public String getItem(int pos){
return namesList.get(pos);
}
@Override
public long getItemId(int pos){
return namesList.indexOf(getItem(pos));
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(namesList.get(position));
imageView.setImageResource(context.getResources().getIdentifier(imagesList.get(position), "drawable", context.getPackageName()));
return rowView;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
namesList = (List<String>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
namesList = filterList;
FilterResults results = new FilterResults();
if(constraint != null && constraint.length()>0){
List<String> FilteredArrayNames = new ArrayList <String>();
constraint = constraint.toString().toLowerCase();
for (int i = 0; i <namesList.size(); i++) {
String dataNames = namesList.get(i);
if (dataNames.toLowerCase().contains(constraint.toString())) {
FilteredArrayNames.add(dataNames);
}
}
results.count = FilteredArrayNames.size();
results.values = FilteredArrayNames;
Log.e("VALUES", results.values.toString());
} else {
results.count = namesList.size();
results.values = namesList;
}
return results;
}
};
return filter;
}
}
答案 0 :(得分:0)
使用以下代码修改代码:
List<String> FilteredArrayNames = new ArrayList <String>();
List<String> FilteredArrayImages = new ArrayList <String>();
constraint = constraint.toString().toLowerCase();
for (int i = 0; i <namesList.size(); i++) {
String dataNames = namesList.get(i);
String images = imagesList.get(i);
if (dataNames.toLowerCase().contains(constraint.toString())) {
FilteredArrayNames.add(dataNames);
FilteredArrayImages.add(images);
}
}
然后将这两个List作为FilterResults
对象返回。
答案 1 :(得分:0)
试试这个例子 为我工作 MainActivity
public class SearchActivity extends AppCompatActivity {
private ListView listView;
private MyAppAdapter myAppAdapter;
private ArrayList<Post> postArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
listView = (ListView) findViewById(R.id.listView);
postArrayList = new ArrayList<>();
postArrayList.add(new Post("Dummy Title", "Dummy Sub Title"));
postArrayList.add(new Post("Searchview in actionbar", "enjoy search functionality from actionbar in android"));
postArrayList.add(new Post("Search in listview", "search feature that filter listview item"));
postArrayList.add(new Post("Android Search Bar", "adding search feature in toolbar using appcompat library"));
postArrayList.add(new Post("Android Studio SearchView example", "Android SearchView tutorial in android studio"));
postArrayList.add(new Post("Android Tutorial", "Get latest android material with simple solution"));
postArrayList.add(new Post("nkDroid tutorials", "A to Z Android tutorials at one place"));
myAppAdapter = new MyAppAdapter(postArrayList, SearchActivity.this);
listView.setAdapter(myAppAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
//*** setOnQueryTextFocusChangeListener ***
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String searchQuery) {
myAppAdapter.filter(searchQuery.toString().trim());
listView.invalidate();
return true;
}
});
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
适配器示例
public class MyAppAdapter extends BaseAdapter {
public class ViewHolder {
TextView txtTitle, txtSubTitle;
}
public List<Post> parkingList;
public Context context;
ArrayList<Post> arraylist;
public MyAppAdapter(List<Post> apps, Context context) {
this.parkingList = apps;
this.context = context;
arraylist = new ArrayList<Post>();
arraylist.addAll(parkingList);
}
@Override
public int getCount() {
return parkingList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.item_post, null);
// configure view holder
viewHolder = new ViewHolder();
viewHolder.txtTitle = (TextView) rowView.findViewById(R.id.title);
viewHolder.txtSubTitle = (TextView) rowView.findViewById(R.id.subtitle);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtTitle.setText(parkingList.get(position).getPostTitle() + "");
viewHolder.txtSubTitle.setText(parkingList.get(position).getPostSubTitle() + "");
return rowView;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
parkingList.clear();
if (charText.length() == 0) {
parkingList.addAll(arraylist);
} else {
for (Post postDetail : arraylist) {
if (charText.length() != 0 && postDetail.getPostTitle().toLowerCase(Locale.getDefault()).contains(charText)) {
parkingList.add(postDetail);
}
}
}
notifyDataSetChanged();
}
}
发布
public class Post {
private String postTitle;
private String postSubTitle;
public String getPostTitle() {
return postTitle;
}
public void setPostTitle(String postTitle) {
this.postTitle = postTitle;
}
public String getPostSubTitle() {
return postSubTitle;
}
public void setPostSubTitle(String postSubTitle) {
this.postSubTitle = postSubTitle;
}
public Post(String postTitle, String postSubTitle) {
this.postTitle = postTitle;
this.postSubTitle = postSubTitle;
}
}