我想在我的应用中创建一个产品列表,左边是图片,右边是维度下拉列表。用户会为每个产品选择一个尺寸,然后在下一个活动中,他们会看到订单的总价格和他们想要的产品(如购物车)。
我和自定义ListView和数组适配器摔跤了两天,但是当我解决滚动时回收行的问题时,出现了另一个问题就像数量框重置为“0”一样走出屏幕的可见区域。
我认为ListView不是产品展示的正确解决方案,每行都有数量框和下拉窗口。
我应该在LinearLayouts中动态创建行并标记每个下拉列表和数量框,以便我可以在结帐时获取它们的值吗?
什么是正确的解决方案?
谢谢!
我提取下拉列表和数量框值的方式:
displayOrder.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int numOfPhotos = PhotosListAdapter.getCount();
for (int i = 0; i < numOfPhotos; i++) {
//System.out.println(PhotosListAdapter.getItemId(i));
View view = PhotosListView.getChildAt(i);
Spinner spinner = (Spinner) view.findViewById(R.id.photo_format);
EditText tv = (EditText) view.findViewById(R.id.quantity);
System.out.println(tv.getText() + " " + spinner.getSelectedItem().toString());
}
我的自定义ArrayAdapter:
Public class PhotosListViewAdapter extends ArrayAdapter<ImageItemsSetter> {
DeleteImageListener dListener;
private Context context;
private int layoutResourceId;
private ArrayList<ImageItemsSetter> data = new ArrayList<ImageItemsSetter>();
//standard constructor
public PhotosListViewAdapter(Context context, int layoutResourceId, ArrayList<ImageItemsSetter> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
static class ViewHolder {
public ImageView Img;
public Spinner spinner;
public Button delete;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
ImageItemsSetter image = data.get(position);
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.Img = (ImageView) row.findViewById(R.id.Img);
holder.spinner = (Spinner) row.findViewById(R.id.photo_format);
holder.delete = (Button) row.findViewById(R.id.deleteImage);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.Img.setImageBitmap(image.getTheImage());
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context,
R.array.formats_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spinner.setAdapter(adapter);
//The delete button
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (dListener != null) {
dListener.onDeletePressed(position);
}
}
});
return row;
}
//Interface to send selected image's position for deletion
public void setDeleteImageListener(DeleteImageListener listener) {
this.dListener = listener;
}
public static interface DeleteImageListener {
public void onDeletePressed(int position);
}
答案 0 :(得分:0)
Hy试试这个:
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageItemsSetter image = data.get(position);
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
ImageView Img = (ImageView) row.findViewById(R.id.Img);
Spinner spinner = (Spinner) row.findViewById(R.id.photo_format);
Button delete = (Button) row.findViewById(R.id.deleteImage);
Img.setImageBitmap(image.getTheImage());
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context,
R.array.formats_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//The delete button
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (dListener != null) {
dListener.onDeletePressed(position);
}
}
});
return row;
}