如何在android中的下一个活动中获取用户正在检查的项目的名称?

时间:2017-12-06 05:14:34

标签: java android listview

我正在使用Android应用,用户将选择食物数量并转到结帐屏幕,购买与swiggypublic class Food implements Parcelable,Serializable { String Foodname; int CartQuantity = 0; double FoodPrice; public Food(String Foodname, int cartQuantity, double FoodPrice) { this.Foodname = Foodname; this.CartQuantity = cartQuantity; this.FoodPrice = FoodPrice; } protected Food(Parcel in) { Foodname = in.readString(); CartQuantity = in.readInt(); FoodPrice = in.readDouble(); } public static final Creator<Food> CREATOR = new Creator<Food>() { @Override public Food createFromParcel(Parcel in) { return new Food(in); } @Override public Food[] newArray(int size) { return new Food[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.Foodname); dest.writeInt(this.CartQuantity); dest.writeDouble(this.FoodPrice); } } 相似的内容。

但我无法使用数量获取所选项目的内容。它显示包名称而不是项目名称。

代码 -

public class ListAdapter extends BaseAdapter {
    public ArrayList<Food> listproducts;
    private Context context;

    public ListAdapter(Context context,ArrayList<Food> listproducts){
        this.context=context;
        this.listproducts=listproducts;
    }
    @Override
    public int getCount() {
        return listproducts.size();
    }

    @Override
    public Food getItem(int position) {
        return listproducts.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row;
        final ListViewHolder listViewHolder;
        if(convertView == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.list_item,parent,false);
            listViewHolder = new ListViewHolder();
            listViewHolder.foodName = row.findViewById(R.id.lblListItem);
            listViewHolder.foodPrice = row.findViewById(R.id.lblListItem1);
            listViewHolder.cartQuantity = row.findViewById(R.id.integer_number);
            listViewHolder.increment = row.findViewById(R.id.increase);
            listViewHolder.decrement = row.findViewById(R.id.decrease);
            row.setTag(listViewHolder);
        }
        else
        {
            row=convertView;
            listViewHolder= (ListViewHolder) row.getTag();
        }
        final Food food=getItem(position);

        listViewHolder.foodName.setText(food.Foodname);
        listViewHolder.foodPrice.setText(food.FoodPrice+" $");
        listViewHolder.cartQuantity.setText(food.CartQuantity+"");
        listViewHolder.increment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                updateQuantity(position,listViewHolder.cartQuantity,1);
            }
        });
        //listViewHolder.edTextQuantity.setText("0");
        listViewHolder.decrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateQuantity(position,listViewHolder.cartQuantity,-1);

            }
        });

        return row;
    }

    private void updateQuantity(int position, TextView edTextQuantity, int value) {

        Food food = getItem(position);
        if(value > 0&&food.CartQuantity<10)
        {
            food.CartQuantity = food.CartQuantity + 1;
        }
        else
        {
            if(food.CartQuantity > 0)
            {
                food.CartQuantity = food.CartQuantity - 1;
            }

        }
        edTextQuantity.setText(food.CartQuantity+"");
    }
}

ListAdapter.class -

enum Box
{
    HEIGHT,
    WIDTH,
    DEPTH
}

public static void UseEnum()
{
    int height = Box.HEIGHT.GetEnumValue<int>();
    int width = Box.WIDTH.GetEnumValue<int>();
    int depth = Box.DEPTH.GetEnumValue<int>();
}

public static T GetEnumValue<T>(this object e) => (T)e;

这是我尝试过的,这是我从这段代码中获得的。

用户将选择要订购的项目的活动

image

用户显示所选项目的活动

image2

它不显示项目名称,价格和数量,而是显示包名称。 如何通过更改代码来获取所需的值?

3 个答案:

答案 0 :(得分:1)

以下是您的代码的更改:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

static int count=0;
private Context _context;

HashMap<String,Food> selectedItems;

public List<String> listDataHeader; // header titles

// child data in format of header title, child title
public HashMap<String, ArrayList<Food>> _listDataChild;

CheckOutItems checkOutItems;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
                             HashMap<String, ArrayList<Food>> listChildData) {
    this._context = context;
    this.listDataHeader = listDataHeader;
    this._listDataChild = listChildData;

    selectedItems = new HashMap<>();

    checkOutItems = (CheckOutItems)context;

}

@Override
public Food getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

Button button;
@Override
public View getChildView(final int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    View row;
    final ListViewHolder listViewHolder;
    if(convertView == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.list_item,parent,false);

        listViewHolder = new ListViewHolder();
        listViewHolder.foodName = row.findViewById(R.id.lblListItem);
        listViewHolder.foodPrice = row.findViewById(R.id.lblListItem1);
        listViewHolder.cartQuantity = row.findViewById(R.id.integer_number);
        listViewHolder.increment = row.findViewById(R.id.increase);
        listViewHolder.decrement = row.findViewById(R.id.decrease);
        listViewHolder.addButton = row.findViewById(R.id.addbtn);
        row.setTag(listViewHolder);
    }
    else
    {
        row=convertView;
        listViewHolder= (ListViewHolder) row.getTag();
    }
    final Food food=getChild(groupPosition,childPosition);

    listViewHolder.foodName.setText(food.Foodname);
    listViewHolder.foodPrice.setText(food.FoodPrice+" $");
    listViewHolder.cartQuantity.setText(food.CartQuantity+"");
    listViewHolder.increment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            updateQuantity(groupPosition,childPosition,listViewHolder.cartQuantity,1);
        }
    });
    //listViewHolder.edTextQuantity.setText("0");
    listViewHolder.decrement.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            updateQuantity(groupPosition,childPosition,listViewHolder.cartQuantity,-1);

        }
    });

    listViewHolder.addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectedItems.put(food.Foodname,food);
            checkOutItems.sendItems(selectedItems);
        }
    });

    return row;
}

private void updateQuantity(int group,int position, TextView edTextQuantity, int value) {

    Food food = getChild(group,position);
    if(value > 0&&food.CartQuantity<10)
    {
        food.CartQuantity = food.CartQuantity + 1;
        count++;


    }
    else
    {
        if(food.CartQuantity > 0)
        {
            food.CartQuantity = food.CartQuantity - 1;
            count--;

        }

    }
    edTextQuantity.setText(food.CartQuantity+"");
}


@Override
public int getChildrenCount(int groupPosition) {
    return _listDataChild.get(listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    return this.listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this.listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

interface CheckOutItems
{
    public void sendItems(HashMap<String,Food> selectedItems);
}
 }

以下是整个项目的Google云端硬盘链接:
https://drive.google.com/open?id=15utf5kuJJHUXDR1bOLeb7DAIDJ54RwOW

答案 1 :(得分:1)

我从您的问题中了解到您想要获得产品 在您的下一个活动中选择食物适配器。

首先在适配器中创建一个方法:

public ArrayList<Food> getSelectedProducts(){
   ArrayList<Food> selected = new ArrayList<>();
   for(Food food:listProducts){
      if(food.getCartQuantity() > 0){
        selected.add(food);
       }
   }
     return selected; 
 }

现在获取当前活动中的所选项目:

ArrayList<Food> products = adapter.getSelectedProducts();

然后将产品放入您用于导航到下一个活动的目标中:

Intent intent = new Intent("your intent");
intent.putExtra("data", (Serializable)products);
startActivity(intent);

在您的下一个活动中,选择的项目为:

ArrayList<Food> selectedProducts = getIntent().getSerializableExtra("data");

那就是它!

答案 2 :(得分:0)

您必须使用setText的对象引用。像

这样的东西

食物f = getFood(); //做了一些逻辑

//设置如下文字。

textView.setText(F); //错误的

尝试使用类似的东西。

textView.setText(f.FoodName)); //从对象获取正确的属性以显示确切的名称。