如何用文本图片和复选框过滤自定义listView?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用文本图片和复选框过滤自定义listView?相关的知识,希望对你有一定的参考价值。
看了一个教程,我有一个 listView
.
我有一个 EditText
并希望通过EditText中的内容过滤listView的内容。
这是我的适配器类。
class ListViewAdapter extends BaseAdapter {
private List<listAudienceFriendsExcept> FriendsExceptList;
private Context context;
ListViewAdapter(Context context, List<listAudienceFriendsExcept> FriendsExceptList) {
this.context = context;
this.FriendsExceptList = FriendsExceptList;
}
@Override
public int getCount() {
return FriendsExceptList.size();
}
@Override
public Object getItem(int pos) {
return FriendsExceptList.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(context).inflate(R.layout.model,viewGroup,false);
TextView name = view.findViewById(R.id.name);
ImageView avatar = view.findViewById(R.id.avatar);
final CheckBox checkbox = view.findViewById(R.id.checkbox);
final listAudienceFriendsExcept thisListAudienceFriendsExcept = FriendsExceptList.get(position);
// Setting data to listView items
checkbox.setTag(thisListAudienceFriendsExcept.getId());
name.setText(thisListAudienceFriendsExcept.getGetFirstName());
if (!thisListAudienceFriendsExcept.getGetAvatar().equals("default_avatar.png")) {
Glide.with(avatar)
.load(API_BASE_URL+"files/avatar_back_files/"+thisListAudienceFriendsExcept.getGetAvatar())
.circleCrop()
.fitCenter()
.into(avatar);
} else if (thisListAudienceFriendsExcept.getGetSex().equals("male")) {
Glide.with(avatar)
.load(API_BASE_URL+"tools/img/default_avatar.png")
.circleCrop()
.fitCenter()
.into(avatar);
} else {
Glide.with(avatar)
.load(API_BASE_URL+"tools/img/female_default_avatar.png")
.circleCrop()
.fitCenter()
.into(avatar);
}
return view;
}
}
这里是我的list,这里是我的populateListView:
static class listAudienceFriendsExcept {
@SerializedName("id")
private int id;
@SerializedName("first_name")
private String first_name;
@SerializedName("last_name")
private String last_name;
@SerializedName("avatar")
private String avatar;
@SerializedName("sex")
private String sex;
public listAudienceFriendsExcept(int id, String first_name, String last_name, String avatar, String sex) {
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.avatar = avatar;
this.sex = sex;
}
int getId() {
return id;
}
String getGetFirstName() {
return first_name;
}
String getGetLastName() {
return last_name;
}
String getGetAvatar() {
return avatar;
}
String getGetSex() {
return sex;
}
}
这里是我的populateListView:
private void populateListView(List<listAudienceFriendsExcept> thisList) {
ListView listViewAudienceFriendsExcept = findViewById(R.id.listViewAudienceFriendsExcept);
ListViewAdapter adapter = new ListViewAdapter(this, thisList);
listViewAudienceFriendsExcept.setAdapter(adapter);
}
我试过了
final TextInputEditText first_name_edit_text = findViewById(R.id.first_name_edit_text);
first_name_edit_text.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence cs, int s, int b, int c) {
friendListAdapter.getFilter().filter(newText); // What is **friendListAdapter** in my case ?
}
我使用Retrofit在活动开始时从服务器获取数据。
private void callServer() {
/*Create handle for the RetrofitInstance interface*/
df myAPIService = RetrofitClientInstance.getRetrofitInstance().create(df.class);
Call<List<listAudienceFriendsExcept>> call = myAPIService.getListAudienceFriendsExcept();
call.enqueue(new Callback<List<listAudienceFriendsExcept>>() {
@SuppressWarnings("NullableProblems")
@Override
public void onResponse(Call<List<listAudienceFriendsExcept>> call, Response<List<listAudienceFriendsExcept>> response) {
populateListView(response.body());
}
@SuppressWarnings("NullableProblems")
@Override
public void onFailure(Call<List<listAudienceFriendsExcept>> call, Throwable throwable) {
Toast.makeText(MainActivity.this, throwable.getMessage()+"èèè", Toast.LENGTH_LONG).show();
}
});
}
我查了一下,发现 friendListAdapter.getFilter().filter(newText)。 但我不知道在我的情况下如何使用。
有什么解决办法吗?
我的另一个问题。我怎样才能把用户ID传给php服务器?
谢谢。
答案
你可以使用流来过滤。
//add this imports on your class or activity
//import com.annimon.stream.Collectors;
//import com.annimon.stream.Stream;
//import com.annimon.stream.function.Predicate;
List<listAudienceFriendsExcept> original_list=new ArrayList<>();
List<listAudienceFriendsExcept> search_list=new ArrayList<>();
EditText serch_field;
void setup()
{
//original_list=whatever the source of the list is
//be sure to populate the original list before proceeding here
populateListView(original_list);
serch_field.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
search_list = Stream.of(original_list).filter(new Predicate<listAudienceFriendsExcept>() {
@Override
public boolean test(listAudienceFriendsExcept item) {
// return item.getGetFirstName().toUpperCase().contains(serch_field.getText().toString().toUpperCase())||item.getGetLastName().toUpperCase().contains(serch_field.getText().toString().toUpperCase());//if you want to filter multiple fields
return item.first_name.toUpperCase().contains(serch_field.getText().toString().toUpperCase());//to filter first name only
}
}).collect(com.annimon.stream.Collectors.<listAudienceFriendsExcept>toList());
populateListView(search_list);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
对于模型
public class listAudienceFriendsExcept {
@SerializedName("id")
private int id;
@SerializedName("first_name")
public String first_name;
@SerializedName("last_name")
private String last_name;
@SerializedName("avatar")
private String avatar;
@SerializedName("sex")
private String sex;
public listAudienceFriendsExcept(int id, String first_name, String last_name, String avatar, String sex) {
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.avatar = avatar;
this.sex = sex;
}
int getId() {
return id;
}
public String getGetFirstName() {
return first_name;
}
public String getGetLastName() {
return last_name;
}
public String getGetAvatar() {
return avatar;
}
public String getGetSex() {
return sex;
}
}
并在Gradle-dependencies上添加
implementation 'com.annimon:stream:1.1.2'
以上是关于如何用文本图片和复选框过滤自定义listView?的主要内容,如果未能解决你的问题,请参考以下文章
自定义 listView 行,带有复选框和旋转时的储蓄复选标记