获取WheelView选择的值

时间:2013-07-30 03:55:47

标签: android

我正在使用https://github.com/chemalarrea/Android-wheel。我使用的视图是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中然后在我的Activity中调用:

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;
    }

}

我的日志将会返回:

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

它返回错误的值

2 个答案:

答案 0 :(得分: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    
}
  }

答案 1 :(得分: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