0

I have a Listview that contains an spinner in it. I am using the baseadapter class to fill the listview and the spinner. How do I pass the Vector I have to each spinner in the Listview. The Vector for the spinner is the same for all the items in the listview.

The workflow is the listview starts empty, but the vector for the spinner is populated and will not change. Then the user will add image(s) to the listview and might select from the spinner adapter.

The problem I am having is the Spinner never seems to get the data, it justs lists "Please Select" and each time I focus on the spinner it adds two more "Please Select" to it.

I also need to know how to get the selected value of the spinner from the activity that is using the two adapters.

Here is the Baseadapter for the Listview.

public class InvoicePicAdapter extends BaseAdapter {

 private Context context;
 private ArrayList<InvoicePicItem> invoicepics;
 private VectorItemsOnInvoice itemsOnInvoice;

 public InvoicePicAdapter(Context context, ArrayList<InvoicePicItem> invoicepics,VectorItemsOnInvoice items ){
     this.context=context;
     this.invoicepics=invoicepics;
     this.itemsOnInvoice=items;
 }

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

@Override
public InvoicePicItem getItem(int position) {
    return invoicepics.get(position);
}

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

protected void deleteImage(int position) {
   this.invoicepics.remove(position);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = (View) inflater.inflate(
                R.layout.listview_item_invoice_pic, null);
    }

    ImageView imgPartPic=(ImageView)convertView.findViewById(R.id.imgPart);

    imgPartPic.setTag(Integer.valueOf(position));
    imgPartPic.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) { 
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage("Delete this Picture?");
            builder.setCancelable(true);
            builder.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    int position = (Integer) v.getTag();
                    deleteImage(position);
                    notifyDataSetChanged();
                    dialog.cancel();
                }
            });
            builder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert = builder.create();
            alert.show();

            return true;
            }
        });

    android.view.ViewGroup.LayoutParams layoutParams = imgPartPic.getLayoutParams();
    layoutParams.width = 400;
    layoutParams.height = 300;
    imgPartPic.setLayoutParams(layoutParams);
    imgPartPic.setScaleType(ScaleType.FIT_XY);

    imgPartPic.setImageBitmap(invoicepics.get(position).getImage());

    Spinner spinItemsOnInvoice = (Spinner)convertView.findViewById(R.id.spinnerItemsOnInvoice);
    InvoicePicSpinAdapter spinnerAdapter = new InvoicePicSpinAdapter(context,
            android.R.layout.simple_spinner_item,itemsOnInvoice);
    spinItemsOnInvoice.setAdapter(spinnerAdapter);
    return convertView;
}
}

Then this is the code for the Spinner BaseAdapter.

public class InvoicePicSpinAdapter extends ArrayAdapter<ItemsOnInvoice>{
private VectorItemsOnInvoice values;
private Context context;

public InvoicePicSpinAdapter(Context context, int simpleSpinnerItem,
        VectorItemsOnInvoice response) {
    super(context, simpleSpinnerItem, response);
    ItemsOnInvoice pleaseSelect  = new ItemsOnInvoice();
    pleaseSelect.inventoryID=0;
    response.add(0, pleaseSelect);
    this.values= response;
    this.context = context;
}

public void updateItemsOnInvoice(VectorItemsOnInvoice items){
    ItemsOnInvoice pleaseSelect  = new ItemsOnInvoice();
    pleaseSelect.inventoryID=0;
    items.add(0, pleaseSelect);
    this.values= items;
}

public int getCount(){
   return values.size();
}

public ItemsOnInvoice getItem(int position){
   return values.elementAt(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    TextView label = new TextView(context);
    label.setTextColor(Color.BLACK);

    if (values.elementAt(position).inventoryID==0){
        label.setText("Please Select");
    } else {
        if (values.elementAt(position).isSPIG==0){
            label.setText("G"+String.valueOf(values.elementAt(position).groupNumber));
        } else {
            label.setText("R"+String.valueOf(values.elementAt(position).inventoryID));
        }
    }
    return label;
}

@Override
public View getDropDownView(int position, View convertView,
        ViewGroup parent) {
    TextView label = new TextView(context);

    if (position==0){
        label.setTextColor(Color.GRAY);
        label.setTextSize(22);
        label.setText("Please Select");
    } else {
        label.setTextColor(Color.BLACK);
        label.setTextSize(28);
        if (values.elementAt(position).inventoryID==0){
            label.setText("Please Select");
        } else {
            if (values.elementAt(position).isSPIG==0){
                label.setText("G"+String.valueOf(values.elementAt(position).groupNumber));
            } else {
                label.setText("R"+String.valueOf(values.elementAt(position).inventoryID));
            }
        }
    }       
    return label;
}
}
4

2 回答 2

0

更新内容时是否在适配器上调用 notifyDataSetChanged()?

于 2013-09-30T13:10:32.647 回答
0

第二个适配器的返回值无效,在加载到屏幕的每个新视图组上都会调用适配器-> getview() 方法,即操作系统“知道”如何回收视图以获取更多信息,请参见视频81-87应该很好地解释整个主题

于 2013-09-30T13:29:47.157 回答