所以我花了将近3个小时来寻找关于如何使用POJO的ArrayAdapter为我的微调器设置值的答案。
这是我的POJO:
public class ArchiveIssueModel implements Serializable {
private String Issues;
private String id;
private String Picture;
public ArchiveIssueModel(String issues, String id, String picture) {
Issues = issues;
this.id = id;
Picture = picture;
}
public ArchiveIssueModel() {
}
public ArchiveIssueModel(String issue) {
this.Issues = issue;
}
public String getIssues() {
return Issues;
}
public void setIssues(String issues) {
Issues = issues;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPicture() {
return Picture;
}
public void setPicture(String picture) {
Picture = picture;
}
//to display object as a string in spinner
@Override
public String toString() {
return Issues;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof ArchiveIssueModel){
ArchiveIssueModel c = (ArchiveIssueModel )obj;
if(c.getIssues().equals(Issues) && c.getId()== id ) return true;
}
return false;
}
我知道如何使用String的ArrayAdapter设置值。 我这样做了,这很容易理解。
ArrayAdapter<String> spinnerArrayAdapter =
new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, yearList);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_year.setAdapter(spinnerArrayAdapter);
sp_year.setSelection(spinnerArrayAdapter.getPosition(year));
但是我无法弄清楚如何在ArrayAdapter o ArchiveIssueModel(即我的POJO)的情况下为我的微调器设置一个值。任何帮助,将不胜感激。 TY:)
答案 0 :(得分:0)
它与您编写的相同,但只是向适配器提供了POJO类的列表。
import android.content.Context
import android.util.AttributeSet
import android.widget.Button
public class ResizableButton(context: Context, attrs: AttributeSet) : Button(context, attrs) {
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val width = MeasureSpec.getSize(widthMeasureSpec)
setMeasuredDimension(width, width)
}
}
答案 1 :(得分:0)
我们需要定义适配器来描述将Java对象转换为View的过程(在getView方法中)。这种天真的方法(没有任何视图缓存)如下所示:
public class UsersAdapter extends ArrayAdapter<User> {
// View lookup cache
private static class ViewHolder {
TextView name;
TextView home;
}
public UsersAdapter(Context context, ArrayList<User> users) {
super(context, R.layout.item_user, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_user, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.tvName);
viewHolder.home = (TextView) convertView.findViewById(R.id.tvHome);
// Cache the viewHolder object inside the fresh view
convertView.setTag(viewHolder);
} else {
// View is being recycled, retrieve the viewHolder object from tag
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data from the data object via the viewHolder object
// into the template view.
viewHolder.name.setText(user.name);
viewHolder.home.setText(user.hometown);
// Return the completed view to render on screen
return convertView;
}
}
注意:将用户pojo更改为pojo类。