用适配器强制关闭

时间:2013-06-23 10:38:16

标签: android adapter

我有简单的适配器显示列表,在数组中包含数据并将其加载到列表中并将此列表传递给适配器。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View rowView=convertView;

    String[] data;
    LayoutInflater inflater = context.getLayoutInflater();
    if(rowView==null){
          rowView = inflater.inflate(R.layout.list_inflator, null);
          ViewHolder  viewHolder = new ViewHolder();
          viewHolder.dishName= (TextView)rowView.findViewById(R.id.textView1);
          viewHolder.dishPrice = (TextView)rowView.findViewById(R.id.textView2);
          viewHolder.add=(Button)rowView.findViewById(R.id.add);

          rowView.setTag(viewHolder);
    }
    data=dishes.get(position).split("-");
    Log.d("Data", data[0]+"///"+data[1]);
    ViewHolder holder = (ViewHolder) rowView.getTag();
    holder.add.setTag(R.string.position,position);

    if(data[0]!=null){
    holder.dishName.setText(data[0]);
    holder.add.setTag(R.string.dishname, data[0]);
    }
    if(data[1]!=null){
         holder.dishPrice.setText(data[1]);
         holder.add.setTag(R.string.dishprice, data[1]);
    }

    holder.add.setOnClickListener(context.addClick);
    return rowView;
}

1 个答案:

答案 0 :(得分:0)

在加载数据时得到的答案是错误的。

           case R.id.juice:

            for (int i = 0; i < Dolci.length; i++)
                dataList.add(Dolci[i]);

            break;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View rowView=convertView;

    String[] data;
    LayoutInflater inflater = context.getLayoutInflater();
    if(rowView==null){
          rowView = inflater.inflate(R.layout.list_inflator, null);
          ViewHolder  viewHolder = new ViewHolder();
          viewHolder.dishName= (TextView)rowView.findViewById(R.id.textView1);
          viewHolder.dishPrice = (TextView)rowView.findViewById(R.id.textView2);
          viewHolder.add=(Button)rowView.findViewById(R.id.add);

          rowView.setTag(viewHolder);
    }
    data=dishes.get(position).split("-");
    Log.d("Data", data[0]+"///"+data[1]);
     ViewHolder holder = (ViewHolder) rowView.getTag();
     holder.add.setTag(R.string.position,position);

     if(data[0]!=null){
     holder.dishName.setText(data[0]);
     holder.add.setTag(R.string.dishname, data[0]);
     }
     if(data[1]!=null){
         holder.dishPrice.setText(data[1]);
         holder.add.setTag(R.string.dishprice, data[1]);
     }


    return rowView;
}