我正在使用https://github.com/chemalarrea/Android-wheel。我使用的 View 是 Wheel-demo 中的 Time2。但是我无法获取所选项目的文本。我试过我的运气
Calendar calendar;
ArrayList<TextView> tvlist;
@SuppressLint("SimpleDateFormat")
@Override
public View getItem(int index, View cachedView, ViewGroup parent) {
int day = -daysCount/2 + index;
Calendar newCalendar = (Calendar) calendar.clone();
newCalendar.roll(Calendar.DAY_OF_YEAR, day);
View view = super.getItem(index, cachedView, parent);
TextView monthday = (TextView) view.findViewById(R.id.time2_monthday);
if (day == 0) {
DateFormat format = new SimpleDateFormat("MMM d");
monthday.setText("Today");
monthday.setTextColor(0xFF0000F0);
} else {
DateFormat format = new SimpleDateFormat("EEE, MMM d");
monthday.setText(format.format(newCalendar.getTime()));
monthday.setTextColor(0xFF111111);
tvlist.add(monthday);
}
return view;
}
@Override
public int getItemsCount() {
return daysCount + 1;
}
@Override
public CharSequence getItemText(int index) {
return "";
}
public String getText(int index){
return tvlist.get(index).getText().toString();
}
在 DayArrayAdapter 然后在我的活动中,我调用了:
String temp = this.dayarrayadapter.getText(this.w_date.getCurrentItem());
但是问题是,当我的电视列表添加 TextView 时,它的顺序不正确,因此当我根据索引获得该基础时,它会为我返回错误的值。我在这里和那里搜索了很多,但仍然没有运气。
这是我到目前为止所做的:
public class DayArrayAdapter extends AbstractWheelTextAdapter {
// Count of days to be shown
private final int daysCount = 40;
// Calendar
Calendar calendar;
ArrayList<String> daylist;
/**
* Constructor
*/
public DayArrayAdapter(Context context, Calendar calendar) {
super(context, R.layout.date, NO_RESOURCE);
this.calendar = calendar;
setItemTextResource(R.id.time2_monthday);
daylist = new ArrayList<String>();
}
@SuppressLint("SimpleDateFormat")
@Override
public View getItem(int index, View cachedView, ViewGroup parent) {
int day = -daysCount/2 + index;
Calendar newCalendar = (Calendar) calendar.clone();
newCalendar.roll(Calendar.DAY_OF_YEAR, day);
View view = super.getItem(index, cachedView, parent);
TextView monthday = (TextView) view.findViewById(R.id.time2_monthday);
if (day == 0) {
monthday.setText("Today");
monthday.setTextColor(0xFF0000F0);
} else {
DateFormat format = new SimpleDateFormat("EEE, MMM d");
monthday.setText(format.format(newCalendar.getTime()));
monthday.setTextColor(0xFF111111);
if( daylist.size()>0 ){
for( int i=0; i<40; i++ ){
daylist.add( format.format(newCalendar.getTime()) );
for ( int j = daylist.size(); j > 2; j-- ){
if( daylist.get( daylist.size()-1 ).toString().equals( daylist.get( j-2 ).toString() )){
daylist.remove( daylist.size()-1 );
}
else if ( daylist.get( daylist.size()-1 ).toString().equals( daylist.get( 1 ).toString())
|| daylist.get( daylist.size()-1 ).toString().equals( daylist.get( 0 ).toString() )){
daylist.remove( daylist.size()-1 );
}
}
}
}
else
daylist.add( format.format(newCalendar.getTime()) );
}
return view;
}
@Override
public int getItemsCount() {
return daysCount + 1;
}
@Override
public CharSequence getItemText(int index) {
return "";
}
public ArrayList<String> getText(){
return daylist;
}
}
我的日程表会返回:
07-31 11:34:29.519: D/datevalue(19111): 0: Fri, Aug 2
07-31 11:34:29.519: D/datevalue(19111): 1: Thu, Aug 1
07-31 11:34:29.519: D/datevalue(19111): 2: Tue, Jul 30
07-31 11:34:29.519: D/datevalue(19111): 3: Mon, Jul 29
07-31 11:34:29.519: D/datevalue(19111): 4: Sun, Jul 28
07-31 11:34:29.519: D/datevalue(19111): 5: Sat, Jul 27
07-31 11:34:29.519: D/datevalue(19111): 6: Fri, Jul 26
07-31 11:34:29.519: D/datevalue(19111): 7: Thu, Jul 25
07-31 11:34:29.519: D/datevalue(19111): 8: Wed, Jul 24
07-31 11:34:29.519: D/datevalue(19111): 9: Tue, Jul 23
07-31 11:34:29.519: D/datevalue(19111): 10: Mon, Jul 22
07-31 11:34:29.519: D/datevalue(19111): 11: Sun, Jul 21
07-31 11:34:29.519: D/datevalue(19111): 12: Sat, Jul 20
07-31 11:34:29.519: D/datevalue(19111): 13: Fri, Jul 19
07-31 11:34:29.519: D/datevalue(19111): 14: Thu, Jul 18
07-31 11:34:29.519: D/datevalue(19111): 15: Wed, Jul 17
07-31 11:34:29.519: D/datevalue(19111): 16: Tue, Jul 16
07-31 11:34:29.519: D/datevalue(19111): 17: Mon, Jul 15
07-31 11:34:29.519: D/datevalue(19111): 18: Sun, Jul 14
07-31 11:34:29.519: D/datevalue(19111): 19: Sat, Jul 13
07-31 11:34:29.519: D/datevalue(19111): 20: Fri, Jul 12
07-31 11:34:29.519: D/datevalue(19111): 21: Thu, Jul 11
07-31 11:34:29.519: D/datevalue(19111): 22: Sat, Aug 3
07-31 11:34:29.519: D/datevalue(19111): 23: Sun, Aug 4
07-31 11:34:29.519: D/datevalue(19111): 24: Mon, Aug 5
07-31 11:34:29.519: D/datevalue(19111): 25: Tue, Aug 6
07-31 11:34:29.519: D/datevalue(19111): 26: Wed, Aug 7
07-31 11:34:29.519: D/datevalue(19111): 27: Thu, Aug 8
07-31 11:34:29.519: D/datevalue(19111): 28: Fri, Aug 9
07-31 11:34:29.519: D/datevalue(19111): 29: Sat, Aug 10
07-31 11:34:29.519: D/datevalue(19111): 30: Sun, Aug 11
07-31 11:34:29.519: D/datevalue(19111): 31: Mon, Aug 12
07-31 11:34:29.519: D/datevalue(19111): 32: Tue, Aug 13
07-31 11:34:29.519: D/datevalue(19111): 33: Wed, Aug 14
07-31 11:34:29.519: D/datevalue(19111): 34: Thu, Aug 15
07-31 11:34:29.519: D/datevalue(19111): 35: Fri, Aug 16
07-31 11:34:29.519: D/datevalue(19111): 36: Sat, Aug 17
07-31 11:34:29.519: D/datevalue(19111): 37: Sun, Aug 18
07-31 11:34:29.519: D/datevalue(19111): 38: Mon, Aug 19
07-31 11:34:29.529: D/datevalue(19111): 39: Tue, Aug 20
如您所见,顺序错误,因此在我使用时在我的活动中
ArrayList<String> tmp = this.dayarrayadapter.getText();
this.TextView.setText( tmp.get( this.w_date.getCurrentItem() ));
它返回错误的值