Spinner的onselected项目监听器无法正常工作

时间:2017-04-10 05:10:22

标签: android

我的程序运行良好..当我选择一个微调项目时,只是在列表中给出一个空值....

Button Additems;
RecyclerView recyclerview;
MyOrderAdapter adapter;
List<OrderList> orderlist;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.fragment_user_order, container, false);
    Additems = (Button) v.findViewById(R.id.additems);
    recyclerview = (RecyclerView) v.findViewById(R.id.recyclerview);
    orderlist = new ArrayList<OrderList>();
    adapter = new MyOrderAdapter(getActivity(), orderlist);
    RecyclerView.LayoutManager layout = new LinearLayoutManager(getActivity());
    recyclerview.setLayoutManager(layout);
    additems();
    return v;
}


  public void additems(){

    Additems.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final AlertDialog builder = new AlertDialog.Builder(getActivity()).create();
            final View dialog = getActivity().getLayoutInflater().inflate(R.layout.new_item_dialogue, null);
            final Spinner itemname = (Spinner) dialog.findViewById(R.id.dialog_item_name);
            final EditText quantity = (EditText) dialog.findViewById(R.id.dialquantity);
            final EditText price = (EditText) dialog.findViewById(R.id.dialprice);
            final EditText amount = (EditText) dialog.findViewById(R.id.dialamount);
            Button ok = (Button) dialog.findViewById(R.id.dialogok);



            ok.setOnClickListener(new View.OnClickListener() {




                @Override
                public void onClick(View view) {
                    orderlist.add(new OrderList(itemname.getSelectedItem().toString(),quantity.getText().toString() ,price.getText().toString(),amount.getText().toString()));
                    recyclerview.setAdapter(adapter);
                    adapter = new MyOrderAdapter(getActivity(), orderlist);
                    adapter.notifyDataSetChanged();
                    builder.dismiss();
                }
            });

            builder.setView(dialog);
            builder.show();
        }
    });
}

和我的orderlist.class

public class OrderList {
String ordername;
String orderquantity;
String orderprice;
String amount;

public OrderList(String s, String orderquantity, String orderprice, String amount) {
    this.ordername = ordername;
    this.orderquantity = orderquantity;
    this.orderprice = orderprice;
    this.amount = amount;
}

public String getOrdername() {
    return ordername;
}

public void setOrdername(String ordername) {
    this.ordername = ordername;
}
public String getOrderquantity() {
    return orderquantity;
}

public void setOrderquantity(String orderquantity) {
    this.orderquantity = orderquantity;
}

public String getOrderprice() {
    return orderprice;
}

public void setOrderprice(String orderprice) {
    this.orderprice = orderprice;
}

public String getAmount() {
    return amount;
}

public void setAmount(String amount) {
    this.amount = amount;
}

}

这是我的recylcerview适配器......

 private Context context;
private List<OrderList> orderlist;

public MyOrderAdapter(Context context,List<OrderList> orderlist){
    this.context = context;
    this.orderlist = orderlist;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_list,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
  OrderList order =  orderlist.get(position);
    holder.ordername.setText(order.getOrdername());
    holder.orderquantity.setText(order.getOrderquantity());
    holder.orderprice.setText(order.getOrderprice());
    holder.orderamount.setText(order.getAmount());
}

@Override
public int getItemCount() {
    return orderlist.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView ordername,orderquantity,orderprice,orderamount;

    public MyViewHolder(View itemView) {
        super(itemView);
        ordername = (TextView) itemView.findViewById(R.id.order_name);
        orderquantity = (TextView) itemView.findViewById(R.id.order_quantity);
        orderprice = (TextView) itemView.findViewById(R.id.order_price);
        orderamount = (TextView) itemView.findViewById(R.id.order_amount);
    }
}

唯一的事情是当我选择微调项目时,它应该在列表中显示值...但它没有显示任何内容......我错在哪里?

1 个答案:

答案 0 :(得分:1)

您需要设置微调器的值以选择它。

        final List<String> list=new ArrayList<String>();
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");

        final Spinner itemname = (Spinner) dialog.findViewById(R.id.dialog_item_name);
        ArrayAdapter<String> adp= new ArrayAdapter<String>(this,
                                    android.R.layout.simple_list_item_1,list);
        itemname .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        itemname .setAdapter(adp);

OR

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.YOUR_ARRAY_NAME, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        itemname .setAdapter(adapter);