2013-07-30 53 views
2

我正在使用https://github.com/chemalarrea/Android-wheel。我使用的视图是Wheel2演示中的Time2。但是我无法获得所选项目的文本。我已经尽我的运气获取WheelView选定的值

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()); 

然而,问题是,当我tvlist添加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; 
    } 

} 

和我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())); 

则返回错误值

+0

什么是“我”在你的getText()方法,你是如何让您的daysCount? –

+0

对不起,我是索引。 daysCount是一个int变量,我将它设置为40,这样轮子将显示40天供用户选择。如果可能的话,我认为你应该首先看看GitHub上的项目,因为我不能在这里发布WheelView类和所有的代码,这可能太长了。抱歉给你带来不便。 – DucTran

回答

2

在您的主要活动中试用此功能之前,请将wheelview课程与mainactivity课程一起使用,然后才能正常工作。

final WheelView country = (WheelView)v11.findViewById(R.id.country); 
country.setVisibleItems(3); 
country.setViewAdapter(new CountryAdapter(this)); 

country.addScrollingListener(new OnWheelScrollListener() 
{ 
public void onScrollingStarted(WheelView wheel) 
{ 
//scrolling = true; 
} 
public void onScrollingFinished(WheelView wheel) 
{ 
//scrolling = false 
//updateCities(city, cities, country.getCurrentItem()); 
//Toast.makeText(MainActivity.this, "wheel : "+wheel.getCurrentItem(), 120).show(); 

int wheel_item = wheel.getCurrentItem(); 

if (wheel_item == 0) { 
//Toast.makeText(MainActivity.this, "wheel_item : "+wheel_item, 30).show(); 

// do whatever you want here 
} 
else if (wheel_item == 1) { 
//Toast.makeText(MainActivity.this, "wheel_item : "+wheel_item, 30).show(); 
// do whatever you want here 
} 
else if (wheel_item == 2) { 
//Toast.makeText(MainActivity.this, "wheel_item : "+wheel_item, 30).show(); 
// do whatever you want here 
} 
else if (wheel_item == 3) 
{ 
// do whatever you want here  
} 
    } 
+0

嗨Harikrishnan,非常感谢您的回复。不过,正如我之前所说,我可以得到目前的项目顺序,但我不知道谁来得到它的文本。我在DayArrayAdapter和ArrayWheelTextAdapter中使用了Time2,它们与“城市中的适配器”活动有所不同。在NumericWheelAdapter它有getItemText()方法,我尝试复制和修改它为我的用法,但仍然没有运气:( – DucTran

1

public AbstractWheel ringsize = (AbstractWheel)findViewById(R.id.ringsize);

设置在AbstractWheel适配器后。 并设置addChangingListener事件

addChangingListener(ringsize, "ringsize"); 


private void addChangingListener(final AbstractWheel wheel, 
     final String label) { 
    wheel.addChangingListener(new com.theengager.spinnerwheel.OnWheelChangedListener() { 
     public void onChanged(AbstractWheel wheel, int oldValue, 
       int newValue) { 
      setData(rsizearr[newValue]); 
     } 
    }); 
} 

更多演示点击此链接android-spinnerwheel