gpt4 book ai didi

java - Android Spinner 问题 - 构造函数 ArrayAdapter(NewSRO, int, String[]) 未定义

转载 作者:行者123 更新时间:2023-11-30 03:23:52 24 4
gpt4 key购买 nike

我在 PagerAdapter 下的微调器上玩得很开心。请查看提供您可能有的任何建议。我只是树桩,可能是晚上来不及了。我试图将代码包装到单独的类中,然后调用该类但没有成功。接下来我想我需要做的就是从 Fragment 添加 getActivity(),这也是我遇到的问题。

    public class NewSRO extends PagerAdapter {

//---return the total number of pages---
public int getCount() {
return 3;
}

public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
View view = null; // setting view variable with null value

switch (position) {
case 0:
resId = R.layout.new_client_1;
view = inflater.inflate(resId, null); // inflating view to attach spinner data
final Spinner s1_clients;
final String[] clients = {
"client1",
"client2",
"3",
"4"
};
//---Spinner View---
s1_clients = (Spinner) view.findViewById(R.id.spinner_client_select);
SpinnerAdapter new_adapter = new ArrayAdapter<String>(
NewSRO.this,
android.R.layout.simple_spinner_item,
clients);
s1_clients.setAdapter(new_adapter);
s1_clients.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = s1_clients.getSelectedItemPosition(); Toast.makeText(null, "You have selected item : " + clients[index],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});


break;
case 1:
resId = R.layout.new_client_1;
break;
case 2:
resId = R.layout.new_client_1;
break;
}

//View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
return null;
}
}

最佳答案

起初似乎添加 collection.getContext() 是正确的方向。尽管如此,它只是让我不再关注膨胀的 View ,这最终是问题所在。每个 case 语句都需要一个 View 来扩充,没有它应用程序就会崩溃。

基本上我需要在每个案例语句中添加以下内容:

view = inflater.inflate(resId, null);

然后我需要用 view.getContext() 修改我的 SpinnerAdapter:

  SpinnerAdapter new_adapter = new ArrayAdapter<String>(
view.getContext(),
android.R.layout.simple_spinner_item,
clients);

我希望这对其他人有帮助,因为 viewPager 是增强用户界面的好方法。

干杯,白兰地李卡马乔

关于java - Android Spinner 问题 - 构造函数 ArrayAdapter<String>(NewSRO, int, String[]) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18605863/

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