列表

时间:2016-01-07 04:31:00

标签: android android-spinner

这是我的代码........我想在列表中发送微调项目和编辑文本值。值出现在日志中,但我认为这些值不是在适配器类中发送的。

这是我的主要课程.............

  public class CalorieMeter extends Fragment {
 Spinner Spinner1, Spinner2;
ListView listView_caloriemeter;
TextView txtView_empty;
CaloriemeterAdapter caloriemeterAdapter;
List<CaloriemeterModel> list_caloriemeter;
CaloriemeterModel model;
Button btn_add_to_list;
String qty, item;
private EditText tv_plates;
private TextInputLayout inputLayoutName;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_calorie_meter, container, false);
    inputLayoutName = (TextInputLayout) view.findViewById(R.id.input_layout_name);
    tv_plates = (EditText) view.findViewById(R.id.input_numberofPlates);
    Spinner1 = (Spinner) view.findViewById(R.id.calorie_meter_dropdown1);
    Spinner2 = (Spinner) view.findViewById(R.id.calorie_meter_dropdown2);
    listView_caloriemeter = (ListView) view.findViewById(R.id.calorie_meter_list);
    txtView_empty = (TextView) view.findViewById(R.id.empty);
    btn_add_to_list = (Button) view.findViewById(R.id.calorie_meter_btn);

    listView_caloriemeter.setEmptyView(txtView_empty);

    list_caloriemeter = new ArrayList<CaloriemeterModel>();


    String[] items = new String[]{"Chai Latte", "Green Tea", "Black Tea", "Cappuccino", "Mocha"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            R.layout.simple_spinner_item, items);

    Spinner1.setAdapter(adapter);
    Spinner2.setAdapter(adapter);


    //   tv_plates.addTextChangedListener(new MyTextWatcher(tv_plates));

    btn_add_to_list.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            qty = tv_plates.getText().toString();

            //txtView_empty = (TextView)view. findViewById(R.id.empty);
            item = (String) Spinner2.getSelectedItem();


            model = new CaloriemeterModel(item,200,Integer.parseInt(qty),0);



            Log.v("item", model.getItem());
            Log.v("item", "" + model.getQuantity());
            Log.v("item", "" + list_caloriemeter.add(model));


            caloriemeterAdapter = new CaloriemeterAdapter(getActivity(), list_caloriemeter);
            listView_caloriemeter.setAdapter(caloriemeterAdapter);


        }
    });

    return view;
}


}

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

 public class CaloriemeterAdapter extends BaseAdapter {
Context  context;
List<CaloriemeterModel> caloriemeterModels;
LayoutInflater inflater;



public  CaloriemeterAdapter(Context context,List<CaloriemeterModel> caloriemeterModels){
    this.context=context;
    this.caloriemeterModels=caloriemeterModels;
    inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return caloriemeterModels.size();
}

@Override
public Object getItem(int position) {
    return caloriemeterModels.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public static class ViewHolder{

    public TextView textView_item;
    public TextView textView_calorie;
    public TextView textView_qty;
    public TextView textView_total;


}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;


    if (convertView==null){
        convertView=inflater.inflate(R.layout.adapter_caloriemeter,null);
        holder=new ViewHolder();
        holder.textView_item=(TextView)convertView.findViewById(R.id.tv_item);
        holder.textView_calorie=(TextView)convertView.findViewById(R.id.tv_calorie);
        holder.textView_qty=(TextView)convertView.findViewById(R.id.tv_qty);
        holder.textView_total=(TextView)convertView.findViewById(R.id.tv_total);
        convertView.setTag(holder);

    }
    else
        holder=(ViewHolder)convertView.getTag();

    holder.textView_item.setText(caloriemeterModels.get(position).getItem());
    holder.textView_calorie.setText(String.valueOf(caloriemeterModels.get(position).getCalorie()));
    holder.textView_qty.setText(String.valueOf(caloriemeterModels.get(position).getQuantity()));
    holder.textView_total.setText(String.valueOf((caloriemeterModels.get(position).getCalorie())*(caloriemeterModels.get(position).getQuantity())));



    return null;
}
}

2 个答案:

答案 0 :(得分:2)

在适配器中使用此代码。您没有返回任何视图,而是返回空值。记住实现自定义列表时总是在getView中返回视图。

 public static class ViewHolder{

    public TextView textView_item;
    public TextView textView_calorie;
    public TextView textView_qty;
    public TextView textView_total;


}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;


    if (convertView==null){
        convertView=inflater.inflate(R.layout.adapter_caloriemeter,null);
        holder=new ViewHolder();
        holder.textView_item=(TextView)convertView.findViewById(R.id.tv_item);
        holder.textView_calorie=(TextView)convertView.findViewById(R.id.tv_calorie);
        holder.textView_qty=(TextView)convertView.findViewById(R.id.tv_qty);
        holder.textView_total=(TextView)convertView.findViewById(R.id.tv_total);
        convertView.setTag(holder);

    }
    else
        holder=(ViewHolder)convertView.getTag();

    holder.textView_item.setText(caloriemeterModels.get(position).getItem());
    holder.textView_calorie.setText(String.valueOf(caloriemeterModels.get(position).getCalorie()));
    holder.textView_qty.setText(String.valueOf(caloriemeterModels.get(position).getQuantity()));
    holder.textView_total.setText(String.valueOf((caloriemeterModels.get(position).getCalorie())*(caloriemeterModels.get(position).getQuantity())));



    return convertView;
}

答案 1 :(得分:1)

您必须在getView()的{​​{1}}内返回您的观点,您正在那里CaloriemeterAdapter

null