我想创建一个ToDo-List应用,我需要一个自定义ArrayAdapter<OwnClass>
来显示用户输入。
我用来存储用户输入的类是:
public class ListElement {
private String title;
private String description;
private PriorityEnum priority;
ListElement (String title, String description, PriorityEnum pe) {
this.title = title;
this.description = description;
this.priority = pe;
}
public String getDescription() {return description;}
public String getTitle() {return title;}
public PriorityEnum getPriority() {return priority;}
}
PriorityEnum
可以高,中或低。
我在tutorial的帮助下制作了当前的工作 ListAdapter。这是我正在使用的修改版本:
public class ListElementArrayAdapter extends ArrayAdapter<ListElement> {
// declaring our ArrayList of items
private ArrayList<ListElement> objects;
/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<Item> objects,
* because it is the list of objects we want to display.
*/
public ListElementArrayAdapter(Context context, int textViewResourceId, ArrayList<ListElement> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
public View getView(int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, it refers to the current Item object.
*/
ListElement i = objects.get(position);
if (i != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView ttd = (TextView) v.findViewById(R.id.toptextdata);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
TextView btd = (TextView) v.findViewById(R.id.desctext);
// check to see if each individual textview is null.
// if not, assign some text!
if (tt != null){
tt.setText("Title: ");
}
if (ttd != null){
ttd.setText(i.getTitle());
}
if (bt != null){
bt.setText("Description: ");
}
if (btd != null){
btd.setText(i.getDescription());
}
}
// the view must be returned to our activity
return v;
}
}
(评论来自教程)
它正确描绘了List,但文本大小太小。
我已经尝试过了:
getView()
返回TextView
元素
- &GT;问题:我不知道如何将其应用于工作ArrayAdapter
,而且我自己写一篇新文章太糟糕了。提前谢谢!