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

Java 如何获取用户在android下一个活动中检查的项目名称?,java,android,listview,Java,Android,Listview,我正在开发一个android应用程序,用户将选择食物数量并移动到结账屏幕,购买类似于foodpanda,swiggy的食物 但我无法使用数量获取所选项目的内容。它显示的是包名而不是项目名 代码- public class Food implements Parcelable,Serializable { String Foodname; int CartQuantity = 0; double FoodPrice; public Food(String Food

我正在开发一个android应用程序,用户将选择食物数量并移动到结账屏幕,购买类似于
foodpanda
swiggy
的食物

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

代码-

public 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);
    }
}
公共类食品实现可打包、可序列化{
字符串食物名;
数量=0;
双餐;
公共食品(字符串食品名称、整数卡数量、双食品价格){
this.Foodname=Foodname;
this.CartQuantity=CartQuantity;
this.FoodPrice=FoodPrice;
}
受保护食品(包裹内){
Foodname=in.readString();
CartQuantity=in.readInt();
FoodPrice=in.readDouble();
}
公共静态最终创建者=新创建者(){
@凌驾
公共食品包裹(包裹内){
归还新食物(在);
}
@凌驾
公共食品[]新数组(整数大小){
返回新食物[尺寸];
}
};
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
dest.writeString(this.Foodname);
目的地写入量(此数量);
目的地可写数据集(本文件);
}
}
ListAdapter.class-

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扩展了BaseAdapter{
公共阵列列表产品;
私人语境;
公共ListAdapter(上下文上下文,ArrayList listproducts){
this.context=context;
这个.listproducts=listproducts;
}
@凌驾
public int getCount(){
返回listproducts.size();
}
@凌驾
公共食品项目(内部位置){
返回列表产品。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
查看行;
最终列表视图持有者列表视图持有者;
if(convertView==null)
{
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
行=布局更平坦。充气(R.layout.list\u项,父项,false);
listViewHolder=新listViewHolder();
listViewHolder.foodName=row.findViewById(R.id.lblListItem);
listViewHolder.foodPrice=row.findViewById(R.id.lblListItem1);
listViewHolder.cartQuantity=row.findViewById(R.id.integer\U编号);
listViewHolder.increment=row.findViewById(R.id.increment);
listViewHolder.decrement=row.findViewById(R.id.decrease);
row.setTag(listViewHolder);
}
其他的
{
行=转换视图;
listViewHolder=(listViewHolder)行。getTag();
}
最终食物=获取项目(位置);
listViewHolder.foodName.setText(food.foodName);
listViewHolder.foodPrice.setText(food.foodPrice+“$”);
listViewHolder.cartQuantity.setText(food.cartQuantity+);
listViewHolder.increment.setOnClickListener(新视图.OnClickListener()中){
@凌驾
公共void onClick(视图v)
{
updateQuantity(位置,listViewHolder.cartQuantity,1);
}
});
//listViewHolder.edTextQuantity.setText(“0”);
listViewHolder.decrement.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
updateQuantity(位置,listViewHolder.cartQuantity,-1);
}
});
返回行;
}
私有void updatequality(int位置、TextView edTextQuantity、int值){
食品=获取项目(位置);
如果(值>0&&food.CartQuantity 0)
{
food.CartQuantity=food.CartQuantity-1;
}
}
edTextQuantity.setText(food.CartQuantity+);
}
}
这是我尝试过的,这是我从这段代码中得到的

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

显示用户所选项目的活动

它显示的不是商品名称、价格和数量,而是包装名称。
如何通过更改代码来获得所需的值?

您必须使用对象引用来设置文本。差不多

食物f=获取食物()//我做了一些逻辑分析

//如下所示设置文本

textView.setText(f)//错

试着用这样的东西


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

以下是对代码的更改:

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);
}
 }
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
静态整数计数=0;
私人语境(private Context)(私人语境);;
HashMap-selectedItems;
公共列表listDataHeader;//标题
//标题标题、子标题格式的子数据
公共HashMap_listDataChild;
签出项目签出项目;
公共ExpandableListAdapter(上下文、列表listDataHeader、,
HashMap listChildData){
这._context=context;
this.listDataHeader=listDataHeader;
这。_listDataChild=listChildData;
selectedItems=newhashmap();
checkOutItems=(checkOutItems)上下文;
}
@凌驾
公共食品getChild(int groupPosition、int ChildPosition){
返回此。\u listDataChild.get(this.listDataHeader.get(groupPosition)).get(childpositionon);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
按钮;
@凌驾
公共视图getChildView(最终int groupPosition,最终int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
查看行;
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");