在我的代码中,我有RecyclerView
。我将ID和姓名设置为TextView
。
private List<Listitem> itemList;
private Context context;
//For Spinner
private String[] items = new String[itemList.size()];
private ArrayAdapter<String> spinnerArrayAdapter;
public ListItemAdapter(Context context, List<Listitem> itemList) {
this.context = context;
this.itemList = itemList;
// this.items = items;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// Simple Text Data
holder.id.setText(itemList.get(position).getProducts_id());
holder.name.setText(itemList.get(position).getProducts_name());
holder.description.setText(itemList.get(position).getProducts_description());
// holder.price.setText(itemList.get(position).getProducts_regular_price());
spinnerArrayAdapter = new ArrayAdapter<String>(context, R.layout.spinner, items);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner);
holder.spinner1.setAdapter(spinnerArrayAdapter);
// String spinnerPos = spinnerArrayAdapter.getPosition (itemList.get());
for (int i = 0; i < itemList.size(); i++) {
items[i] = itemList.get(position).getProducts_regular_price();
holder.price.setText(items[i]);
我想将价格数据设置为Spinner。我在获取NullPointerException
这里有items.please帮助。
private String[] items = new String[itemList.size()];
public class Listitem {
@SerializedName("products_id")
@Expose
private String products_id;
@SerializedName("products_name")
@Expose
private String products_name;
@SerializedName("products_description")
@Expose
private String products_description;
@SerializedName("products_regular_price")
@Expose
private String products_regular_price;
//getter and setter
}
我如何获得products_regular_price以及如何设置Spinner
。获取我的json响应中的8值。
我只想将数据设置为Spinner
List<ListItem> itemList
答案 0 :(得分:1)
您正在使用尚未初始化的itemList.size()
,因此可能是NullPointerException
你可以这样做:
private List<Listitem> itemList;
private Context context;
//For Spinner
private String[] items ;
private ArrayAdapter<String> spinnerArrayAdapter;
public ListItemAdapter(Context context, List<Listitem> itemList) {
this.context = context;
this.itemList = itemList;
items = new String[itemList.size()];
}
试试这个,希望它可以帮助你。