我在这里看到有关这个主题的帖子丢失了但是在我的情况下他们都没有帮助我。 我有一系列商店购物车项目,包括:姓名,nubmer,公寓号码。 我有适配器来接收这个数组并填充微调器。
这是适配器:
public class SpinnerAdapter extends BaseAdapter {
ArrayList<ShopCartModel> list;
Context context;
LayoutInflater inflater;
public SpinnerAdapter(Context c, ArrayList<ShopCartModel> list) {
this.context = c;
this.list = list;
inflater = (LayoutInflater.from(c));
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.custom_spinner_item, null);
TextView name = (TextView) convertView.findViewById(R.id.textView);
ShopCartModel tmp = list.get(position);
name.setText(tmp.getName());
return convertView;
}
}
这是我的主要内容:
public class ShopCartScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spin;
ArrayList<ShopCartModel> shopCarts;
SpinnerAdapter spinnerAdapter;
GetShopLists getShopList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shop_carts_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
cartItems = (RecyclerView) findViewById(R.id.newListItem);
spin = new Spinner(ShopCartScreen.this);
db = DBHelper.getInstance(this);
int apartmentNumber = preferences.getInt("apartmentNumber", 0);
apartment = new ApartmentModel(apartmentNumber);
getShopList = new GetShopLists(this, shopCarts, spin, spinnerAdapter);
getShopList.execute("link to the query");
toolbar.addView(spin, 0);
spin.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
preferences = getSharedPreferences("appData", 0);
boolean flag = preferences.getBoolean("ItemsLoadedFromDb", false);
ShopCartModel tmp = shopCarts.get(position);
getShopingCartList = new GetShopingCartList(this, list, adapter, cartItems, tmp.getNumber());
getShopingCartList.execute("link to the query");
}
答案 0 :(得分:1)
这是一个常见问题,并使用本网站的一些答案,我创建了一个adapter,允许您为微调器添加默认值。
因此,对于您的情况,您需要创建ShopCartModel的对象,其名称要用作默认值,然后使用此适配器:
ShopCartModel defaultModel = new ShopCartModel("Please choose cart model");
public class Adapter extends ArrayAdapter<ShopCartModel> {
public static final int HINT_COUNT = 1;
public static final int HINT_POSITION = 0;
public Adapter(Context context, int resource, List<ShopCartModel> valuesList, ShopCartModel defaultValue) {
super(context, resource);
List<ShopCartModel> list = new ArrayList<>();
list.add(0, defaultValue);
list.addAll(valuesList);
addAll(list);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView view;
if (position == 0) {
TextView textView = new TextView(getContext());
textView.setHeight(0);
textView.setVisibility(View.GONE);
view = textView;
} else {
view = (TextView) super.getDropDownView(position, null, parent);
}
parent.setVerticalScrollBarEnabled(false);
return view;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_spinner_item, null);
TextView name = (TextView) convertView.findViewById(R.id.textView);
ShopCartModel tmp = getItem(position);
name.setText(tmp.getName());
return convertView;
}}