2015-05-13 65 views
0

我無法在自定義列表視圖中使用TextViewsetText。要設置TextView,用戶必須滾動列表。代碼中可能存在什麼問題?數據未在android的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; 
} 


} 
+0

在getview中使用viewholder方法並設置你的數據.. – Android

+0

@AnkitKumar我如何使用viewholder,因爲我是新的不知道 –

+0

http://stackoverflow.com/questions/30212208/inflater-button-click-listener-not-工作/ 30212311#30212311你可以反駁這個例子。 – Android

回答

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; 
} 
相關問題