0

我有 7 个 textview 代表一个日期前。8 月 1 日,8 月 2 日,8 月 3 日...这 7 个文本视图显示在列表视图的每一行中,问题是当我单击按钮时,所有文本视图都显示在列表视图的最后一项中。

这就是我试图实现的目标 http://img818.imageshack.us/img818/416/q39d.png

列表项0

8 月 1 日

列表项1

8 月 2 日

……

这是我在 getview 中的代码

public View getView(int position, View convertView, ViewGroup parent) {

    //august1
    Calendar mCalendar = new GregorianCalendar();
    mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    SimpleDateFormat mDF = new SimpleDateFormat("dd MMMM");
    String fDate = mDF.format(mCalendar.getTime());
    //august2
    mCalendar.add(Calendar.DAY_OF_MONTH, 1);
    String secDate = mDF.format(mCalendar.getTime());

    mCalendar.add(Calendar.DAY_OF_MONTH, 1);
    String tDate = mDF.format(mCalendar.getTime());

    mCalendar.add(Calendar.DAY_OF_MONTH, 1);
    String fourthDate = mDF.format(mCalendar.getTime());

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.activity_main, parent, false);
    fDay = (TextView) rowView.findViewById(R.id.fDate);
    secDay = (TextView) rowView.findViewById(R.id.secDate);
    tDay = (TextView) rowView.findViewById(R.id.tDate);
    fourthDay = (TextView) rowView.findViewById(R.id.fourthDate);



    Button test = (Button) rowView.findViewById(R.id.btn);

    test.setOnClickListener(new OnClickListener() {
        //to increment the date
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            weekNumber = weekNumber + 1;
            fDay.setText(getNextWeek(weekNumber));

        }

    });

    fDay.setText(values[position]);
    //text.setText(values[position]);

    // Change icon based on name
    String s = values[position];
    //String bs = values[position];

    System.out.println(s);
//System.out.println(bs);
     //represent date in a row
    //listitem0
    if (s.equals("Date 1")) {
        fDay.setText(fDate);
    //listitem1
    } else if (s.equals("Date 2")) {
    secDay.setText(secDate);
    //listitem2
    } else if (s.equals("Date 3")) {
    tDay.setText(tDate);

    } else if (s.equals("Date 4")) {
        fourthDay.setText(fourthDate);

     ......


    } else {

        // textView.setText(fDate);
    }

    return rowView;
}

// method to adjust the date
private static String getNextWeek(int weekFromToday) {
    Calendar mCalendar = new GregorianCalendar();
    mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    mCalendar.set(Calendar.WEEK_OF_YEAR,
            mCalendar.get(Calendar.WEEK_OF_YEAR) + weekFromToday);

    SimpleDateFormat mDF = new SimpleDateFormat("dd MMMM");
    String fDate = mDF.format(mCalendar.getTime());
    fDay.setText(fDate);

    mCalendar.add(Calendar.DAY_OF_MONTH, 1);
    String secDate = mDF.format(mCalendar.getTime());
    mCalendar.add(Calendar.DAY_OF_MONTH, 1);
    String tDate = mDF.format(mCalendar.getTime());
    mCalendar.add(Calendar.DAY_OF_MONTH, 1);
    String fourthDate = mDF.format(mCalendar.getTime());

    fDay.setText(fDate);
    secDay.setText(secDate);
    tDay.setText(tDate);
    fourthDay.setText(fourthDate);

    return fDate;

}

}

4

1 回答 1

0

这是实现getView列表视图的方法重用的更准确的方法:

public View getView(int position, View convertView, ViewGroup parent) {
  View view = null;
  if (convertView == null) {
    LayoutInflater inflater = context.getLayoutInflater();
    view = inflater.inflate(textViewResourceId, parent, false);

  } else {
    view = convertView;
  }

  return view;
}
于 2013-07-31T06:37:53.353 回答