在android中使用基本适配器时,Inflater不适合我

时间:2012-07-31 04:38:59

标签: android android-layout android-inflate

我正在尝试给布局充气,但这对我不起作用。 我在列表视图中使用了一个基础播放器。

int can1 = Integer.parseInt(c.getString(c.getColumnIndex("InCan")));
if (can1 < 1) {
    // ...
} else {
    View view;
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.receipt, null);
    TextView textView = (TextView) view.findViewById(R.id.rcan);
    textView.setVisibility(View.GONE);
    TextView coke3 = (TextView) view.findViewById(R.id.rcoke);
    coke3.setVisibility(View.GONE);

can[i] = " Coke In Can: "+c.getString(c.getColumnIndex("InCan"));
}

你能指出我的问题是什么吗?

这是我在列表视图中使用的Adapter类。 希望你帮助我。

public class receiptadapter extends BaseAdapter {
private Context context;
private final String[] productname;
private final String[] fixins;
private final String[] original;
private final String[] hot;
private final String[] quantity;
private final String[] price;
private final String[] total;
private final String[] salad;
private final String[] coleslaw;
private final String[] potato;
private final String[] coke;
private final String[] ccoke;
private final String[] water;
private final String[] can;

public receiptadapter(Context context, String[] productname, String[] fixins,
        String[] original, String[] hot, String[] quantity,String[] price, 
        String[] total, String[] salad, String[] coleslaw, String[] potato, 
        String[] coke, String[] ccoke, String[] water, String[] can){

    this.context= context;
    this.productname = productname;
    this.fixins = fixins;
    this.original = original;
    this.hot = hot;
    this.quantity = quantity;
    this.price = price;
    this.total = total;
    this.salad = salad;
    this.coleslaw = coleslaw;
    this.potato = potato;
    this.coke = coke;
    this.ccoke = ccoke;
    this.water = water;
    this.can = can;
}   
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    viewHolder holder = new viewHolder();

    if(convertView == null ){
        convertView = inflater.inflate(R.layout.receipt, null);

        holder.pname = (TextView) convertView.findViewById(R.id.rpname);
        holder.fix = (TextView) convertView.findViewById(R.id.rfixins);
        holder.orig = (TextView) convertView.findViewById(R.id.rorig);
        holder.hot1 = (TextView) convertView.findViewById(R.id.rhot);
        holder.qty = (TextView) convertView.findViewById(R.id.rquantity);
        holder.price1 = (TextView) convertView.findViewById(R.id.rprice);
        holder.sub = (TextView) convertView.findViewById(R.id.rsub);
        holder.sal = (TextView) convertView.findViewById(R.id.rsal);
        holder.col = (TextView) convertView.findViewById(R.id.rcol);
        holder.pot = (TextView) convertView.findViewById(R.id.rpot);
        holder.coke1 = (TextView) convertView.findViewById(R.id.rcoke);
        holder.coke2 = (TextView) convertView.findViewById(R.id.rccoke);
        holder.water1 = (TextView) convertView.findViewById(R.id.rwater);
        holder.can1 = (TextView) convertView.findViewById(R.id.rcan);
        convertView.setTag(holder);


    }else{
        holder = (viewHolder) convertView.getTag();
    }

    holder.pname.setText(productname[position]);
    holder.fix.setText(fixins[position]);
    holder.orig.setText(original[position]);
    holder.hot1.setText(hot[position]);
    holder.qty.setText("  Quantity: "+quantity[position]);
    holder.price1.setText("  Price: PHP "+price[position]);
    holder.sub.setText("  Sub - Total: PHP "+total[position]);
    holder.sal.setText(salad[position]);
    holder.col.setText(coleslaw[position]);
    holder.pot.setText(potato[position]);
    holder.coke1.setText(coke[position]);
    holder.coke2.setText(ccoke[position]);
    holder.water1.setText(water[position]);
    holder.can1.setText(can[position]);

    return convertView;
}

public int getCount() {
    // TODO Auto-generated method stub
    return productname.length;
}

public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

class viewHolder{
    TextView pname;
    TextView fix;
    TextView orig;
    TextView hot1;
    TextView price1;
    TextView qty;
    TextView sub;
    TextView sal;
    TextView col;
    TextView pot;
    TextView coke1;
    TextView coke2;
    TextView water1;
    TextView can1;
}

}

2 个答案:

答案 0 :(得分:0)

试试这段代码

 LayoutInflater inflater = null;
 inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
 view = inflater.inflate(R.layout.other, null);

答案 1 :(得分:0)

使用以下代码代替您的代码。

public class receiptadapter extends BaseAdapter {
    private Context context;
    private final String[] productname;
    private final String[] fixins;
    private final String[] original;
    private final String[] hot;
    private final String[] quantity;
    private final String[] price;
    private final String[] total;
    private final String[] salad;
    private final String[] coleslaw;
    private final String[] potato;
    private final String[] coke;
    private final String[] ccoke;
    private final String[] water;
    private final String[] can;
    private LayoutInflater mLinflater;

    public receiptadapter(Context context, String[] productname,
        String[] fixins, String[] original, String[] hot,
        String[] quantity, String[] price, String[] total, String[] salad,
        String[] coleslaw, String[] potato, String[] coke, String[] ccoke,
        String[] water, String[] can) {

        this.context = context;
        this.productname = productname;
        this.fixins = fixins;
        this.original = original;
        this.hot = hot;
        this.quantity = quantity;
        this.price = price;
        this.total = total;
        this.salad = salad;
        this.coleslaw = coleslaw;
        this.potato = potato;
        this.coke = coke;
        this.ccoke = ccoke;
        this.water = water;
        this.can = can;
        this.mLinflater = LayoutInflater.from(context);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        viewHolder holder = new viewHolder();

        if (convertView == null) {
            convertView = mLinflater.inflate(R.layout.receipt, null);
            holder.pname = (TextView) convertView.findViewById(R.id.rpname);
            holder.fix = (TextView) convertView.findViewById(R.id.rfixins);
            holder.orig = (TextView) convertView.findViewById(R.id.rorig);
            holder.hot1 = (TextView) convertView.findViewById(R.id.rhot);
            holder.qty = (TextView) convertView.findViewById(R.id.rquantity);
            holder.price1 = (TextView) convertView.findViewById(R.id.rprice);
            holder.sub = (TextView) convertView.findViewById(R.id.rsub);
            holder.sal = (TextView) convertView.findViewById(R.id.rsal);
            holder.col = (TextView) convertView.findViewById(R.id.rcol);
            holder.pot = (TextView) convertView.findViewById(R.id.rpot);
            holder.coke1 = (TextView) convertView.findViewById(R.id.rcoke);
            holder.coke2 = (TextView) convertView.findViewById(R.id.rccoke);
            holder.water1 = (TextView) convertView.findViewById(R.id.rwater);
            holder.can1 = (TextView) convertView.findViewById(R.id.rcan);
            convertView.setTag(holder);
        } else {
            holder = (viewHolder) convertView.getTag();
        }

        holder.pname.setText(productname[position]);
        holder.fix.setText(fixins[position]);
        holder.orig.setText(original[position]);
        holder.hot1.setText(hot[position]);
        holder.qty.setText("  Quantity: " + quantity[position]);
        holder.price1.setText("  Price: PHP " + price[position]);
        holder.sub.setText("  Sub - Total: PHP " + total[position]);
        holder.sal.setText(salad[position]);
        holder.col.setText(coleslaw[position]);
        holder.pot.setText(potato[position]);
        holder.coke1.setText(coke[position]);
        holder.coke2.setText(ccoke[position]);
        holder.water1.setText(water[position]);
        holder.can1.setText(can[position]);

        return convertView;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return productname.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    class viewHolder {
        TextView pname;
        TextView fix;
        TextView orig;
        TextView hot1;
        TextView price1;
        TextView qty;
        TextView sub;
        TextView sal;
        TextView col;
        TextView pot;
        TextView coke1;
        TextView coke2;
        TextView water1;
        TextView can1;
    }
}