0

我是android开发的新手,所以我对动态列表有疑问。

我有一个 Habit 类和 HabitMemoryDao 类,我想用 Habits 在主类中创建一个 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;
    }
4

3 回答 3

0

What you need is to understand how ListView and Adapters work with each other:

So there are two ways to start: You can use ArrayAdapter, that uses the toString() method of your Habit class to show a list of habits in a ListView or you use the more powerful class BaseAdapter. I would suggest you to learn how BaseAdapter works, since Array adapter is a subclass of BaseAdapter. Simple tutorial for BaseAdapters

Btw: It's not a good idea to ask for people who writes you some concrete code. You are welcome to ask questions about concrete problems and people will answer you (most of the times with code snippets).

于 2013-07-23T10:44:38.477 回答
0

首先在你的类中添加一个toString()方法Habit

    public class Habit{
    ...

    public String toString()
    {
      return getName()+" "+getStartDate()+" "+getDuration()+" "+getFrequencyInWeek()+" "+getAward();
    }
    ...
}

然后在你MainActivity看一下这个基本ListView教程,你就会弄清楚。 http://www.vogella.com/articles/AndroidListView/

于 2013-07-23T10:46:36.023 回答
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 个 TextView(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);
于 2013-07-23T10:50:15.810 回答