我是android开发的新手,所以我的动态列表有问题。
我有一个习惯类和HabitMemoryDao类,我想在主类中创建一个ListView 与习惯。 有人可以写下Listview将展示所有习惯的主要课程。 感谢
public class Habit {
private String name;
private Date startDate;
private int duration;
private int frequencyInWeek; // from 1 to 7
private String awardText;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public int getFrequencyInWeek() {
return frequencyInWeek;
}
public void setFrequencyInWeek(int frequency) {
this.frequencyInWeek = frequency;
}
public String getAward() {
return awardText;
}
public void setAward(String award) {
this.awardText = award;
}
}
public class HabitMemoryDao {
private List<Habit> habitList = new LinkedList<Habit>();
public HabitMemoryDao() {
Habit habit1 = new Habit();
habit1.setName("Running");
habit1.setDuration(30);
habit1.setFrequencyInWeek(2);
habit1.setStartDate(new Date());
habitList.add(habit1);
Habit habit2 = new Habit();
habit2.setName("Swimming");
habit2.setDuration(15);
habit2.setFrequencyInWeek(7);
habit2.setStartDate(new Date());
habitList.add(habit2);
}
public List<Habit> getHabitList() {
return habitList;
}
答案 0 :(得分:0)
您需要了解ListView和Adapters如何相互协作:
因此有两种方法可以启动:您可以使用ArrayAdapter,它使用Habit类的toString()方法在ListView中显示习惯列表,或者使用更强大的类BaseAdapter。我建议你学习BaseAdapter的工作原理,因为Array adapter是BaseAdapter的子类。 Simple tutorial for BaseAdapters
顺便说一下:要求那些给你写一些具体代码的人并不是一个好主意。欢迎您提出有关具体问题的问题,人们会回答您(大多数情况下都会使用代码段)。答案 1 :(得分:0)
首先在toString()
类
Habit
方法
public class Habit{
...
public String toString()
{
return getName()+" "+getStartDate()+" "+getDuration()+" "+getFrequencyInWeek()+" "+getAward();
}
...
}
然后在你的MainActivity
中查看这个基本的ListView
教程,你就会明白这一点。
http://www.vogella.com/articles/AndroidListView/
答案 2 :(得分:0)
最好的方法是创建自己的ArrayAdapter。示例实现:
public class HabitAdapter extends ArrayAdapter<Habit> {
private ArrayList<Habit> items;
Activity activity;
static class ViewHolder {
public TextView nameTv;
public TextView startDateTv;
public TextView awardTextTv;
}
public HabitAdapter(Activity activity, int textViewResourceId,
ArrayList<Habit> items) {
super(activity, textViewResourceId, items);
this.items = items;
this.activity = activity;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getLayoutInflater();
rowView = inflater.inflate(R.layout.row_layout_for_one_habit, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.nameTv = (TextView) rowView.findViewById(R.id.nameTv);
viewHolder.startDateTv = (TextView) rowView
.findViewById(R.id.startDateTv);
viewHolder.awardTextTv = (TextView) rowView
.findViewById(R.id.awardTextTv);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
Habit habit = items.get(position);
holder.nameTv.setText(habit.getName());
holder.startDateTv.setText(habit.getStartDate());
holder.awardTextTv.setText(habit.getAwardText());
return rowView;
}
}
您需要为ListView的一行创建布局。根据我的实现,你应该有3个TextViews(nameTv,startDateTv,awardTextTv)。
然后在你的活动中:
ListView myList = (ListView) findViewById(R.id.myList);
HabitAdapter ha = new HabitAdapter(this, R.layout.row_layout_for_one_habit, HabitMemoryDao.getHabitList());
myList.setAdapter(ha);