2016-05-07 5 views
0

ここではこの件に関する記事が失われていますが、私の状況では誰も助けてくれませんでした。 私は、名前、ナブマー、アパート番号を持つショッピングカートの項目の配列を持っています。 と私は、この配列を受け取り、スピナーに移入するためのアダプタを持っています。位置0のカスタムアダプターでスピナーのデフォルメ値を設定する

これはアダプタです:

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

これは共通の問題であると私はあなたが追加することができます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; 
}} 
+0

しかし、アイテムが数えることを覚えて、今大きいです:はlist.size() + HINT_COUNT –

関連する問題