gpt4 book ai didi

android - 获取 WheelView 选择值

转载 作者:搜寻专家 更新时间:2023-11-01 08:57:28 24 4
gpt4 key购买 nike

我正在使用 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 中然后在我的 Activity 中调用:

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

所以如你所见,我使用时在我的 Activity 中顺序是错误的

ArrayList<String> tmp = this.dayarrayadapter.getText();
this.TextView.setText( tmp.get( this.w_date.getCurrentItem() ));

它返回错误的值

最佳答案

在将 wheelview 类与 mainactivity 类一起包含之前,在您的主要 Activity 中尝试此操作,然后它将起作用。

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

关于android - 获取 WheelView 选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17938037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com