c#中 如何用listview显示图片
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#中 如何用listview显示图片相关的知识,希望对你有一定的参考价值。
我想在listview控件中的一列中都显示同一张图片,listview的view属性我要求使用的是Details。其实我要加图片的目的是为了让listview中显示的“项”的间隔大一点,如果大家有什么好的方法也可以给我说说,谢谢了
能不能说的详细点?谢谢
加一个imagelist控件...然后设置imagelist里面的images属性...往里面加入图片集合..
再在listview里面设置stateimagelist或者bigimagelist或者smallimagelist属性..为新添加那个imagelist控件...
在listview添加items时 选择state\\big\\small imageindex
注意..选择要对应的imagelist
如果设置的是 stateimagelist属性
便要选择 stateimageindex...
参考技术A ListView 只能在第一列显示图片,叫做 stateimage如何用文本图片和复选框过滤自定义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'
以上是关于c#中 如何用listview显示图片的主要内容,如果未能解决你的问题,请参考以下文章