在android中的textview上没有设置数据

时间:2015-05-13 11:03:06

标签: android listview

我无法在自定义列表视图中的setTextTextView。要设置TextView,用户必须滚动列表。代码中可能存在什么问题?

我的适配器类

public class BeveragesItemAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
private final Integer[] price;
TextView qtyCounter;
ImageView decreaseQty;
ImageView increaseQty;
int qtyValues;
ArrayList<Integer> qtyValue = new ArrayList<Integer>();


public BeveragesItemAdapter(Activity context, String[] itemname, Integer[] imgid, Integer[] price, ArrayList<Integer> qtyValue) {
    super(context, R.layout.list_beverages_item, itemname);

    this.context = context;
    this.itemname = itemname;
    this.imgid = imgid;
    this.price = price;
    this.qtyValue = qtyValue;

    for(int i=0;i<itemname.length;i++){
        qtyValue.add(i, 0);
    }
}

public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_beverages_item, null, true);

    ImageView imageView = (ImageView) rowView.findViewById(R.id.drinks);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.drinkTitle);
    TextView extratxt = (TextView) rowView.findViewById(R.id.drinkQuantity);
    ImageView imageView1 = (ImageView) rowView.findViewById(R.id.rupee);
    TextView extratxt1 = (TextView) rowView.findViewById(R.id.drinkPrice);
    qtyCounter = (TextView) rowView.findViewById(R.id.drinkCounter);
    decreaseQty = (ImageView) rowView.findViewById(R.id.decreaseCounter);
    increaseQty = (ImageView) rowView.findViewById(R.id.increaseCounter);

    decreaseQty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(qtyValue.get(position) == 0) {
                    qtyValues=0;
                    qtyValue.set(position,qtyValues);
                }
                else{
                    qtyValues = qtyValue.get(position) - 1;
                    qtyValue.set(position,qtyValues);
                }

            }
    });
    increaseQty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(qtyValue.get(position)<10) {
                    qtyValues = qtyValue.get(position) + 1;
                    qtyValue.set(position, qtyValues);
                }
            }
    });

    imageView.setImageResource(imgid[position]);
    txtTitle.setText(itemname[position]);
    extratxt.setText("50ml | 100ml | 1L " );
    imageView1.setImageResource(R.drawable.rupee);
    extratxt1.setText(price[position] + "");
    qtyCounter.setText(qtyValue.get(position).toString()+"");
    return rowView;

}

;
}

致电班级

public class EnergyDrinkFragment extends Fragment {


public EnergyDrinkFragment() {
    // Required empty public constructor
}

ListView energyDrinkList;
String[] itemname = {
        "Gatorade",
        "Glucon-D",
        "Red Bull",
        "Monster",
        "Gatorade",
        "Glucon-D"
};

Integer[] imgid = {
        R.drawable.gatorade,
        R.drawable.glucon_d,
        R.drawable.redbull,
        R.drawable.monster,
        R.drawable.gatorade1,
        R.drawable.glucon_d1

};

Integer[] price = {
        30,
        40,
        20,
        15,
        50,
        80
};

ArrayList<Integer> qtyValue = new ArrayList();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    if (container == null) {
        return null;
    }
    BeveragesItemAdapter beveragesItemAdapter = new         BeveragesItemAdapter(getActivity(), itemname, imgid, price,qtyValue);

    // Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_energy_drinks, container, false);
    energyDrinkList = (ListView) view.findViewById(R.id.energyDrink);


    energyDrinkList.setAdapter(beveragesItemAdapter);
    beveragesItemAdapter.notifyDataSetChanged();

    return view;
}


}

2 个答案:

答案 0 :(得分:1)

更改getView方法并添加viewholder

public View getView(final int position,View view,ViewGroup parent){
        ViewHolder holder;
        if(view==null){

        LayoutInflater inflater=context.getLayoutInflater();
        view=inflater.inflate(R.layout.list_beverages_item,null,true);
        holder=new ViewHolder();
        holder.imageView=(ImageView)rowView.findViewById(R.id.drinks);
        holder.txtTitle=(TextView)rowView.findViewById(R.id.drinkTitle);
        holder.extratxt=(TextView)rowView.findViewById(R.id.drinkQuantity);
        holder.imageView1=(ImageView)rowView.findViewById(R.id.rupee);
        holder.extratxt1=(TextView)rowView.findViewById(R.id.drinkPrice);
        holder.qtyCounter=(TextView)rowView.findViewById(R.id.drinkCounter);
        holder.decreaseQty=(ImageView)rowView.findViewById(R.id.decreaseCounter);
        holder.increaseQty=(ImageView)rowView.findViewById(R.id.increaseCounter);

        view.setTag(holder);
        }else{
        holder=(ViewHolder)view.getTag();
        }


        holder.decreaseQty.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
        if(qtyValue.get(position)==0){
        qtyValues=0;
        qtyValue.set(position,qtyValues);
        }
        else{
        qtyValues=qtyValue.get(position)-1;
        qtyValue.set(position,qtyValues);
        }

        }
        });
        holder.increaseQty.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
        if(qtyValue.get(position)<10){
        qtyValues=qtyValue.get(position)+1;
        qtyValue.set(position,qtyValues);
        }
        }
        });

        holder.imageView.setImageResource(imgid[position]);
        holder.txtTitle.setText(itemname[position]);
        holder.extratxt.setText("50ml | 100ml | 1L ");
        holder.imageView1.setImageResource(R.drawable.rupee);
        holder.extratxt1.setText(price[position]+"");
        holder.qtyCounter.setText(qtyValue.get(position).toString()+"");
        return view;

        }

class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
    TextView extratxt;
    ImageView imageView1;
    TextView extratxt1;
    TextView qtyCounter;
    ImageView decreaseQty;
    ImageView increaseQty;
}

答案 1 :(得分:0)

你应该在这里实现视图持有者方法是你的视频管:https://youtu.be/wDBM6wVEO70