如何在适配器列表视图中将String值插入String

时间:2017-04-07 18:22:00

标签: java android string android-arrayadapter

插入将整数值返回到数组适配器列表的方法时出错。它表示在对象中无法应用。

我的错误:

现在我无法得到方法及其价值

我无法访问getQuantity方法。

代码:

public class FoodItemAdapter extends ArrayAdapter<FoodItem> {

    private int qty  ;

    public FoodItemAdapter(Activity context, ArrayList<FoodItem> androidFlavors) {
        // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
        // the second argument is used when the ArrayAdapter is populating a single TextView.
        // Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
        // going to use this second argument, so it can be any value. Here, we used 0.
        super(context, 0, androidFlavors);
    }

    /**
     * Provides a view for an AdapterView (ListView, GridView, etc.)
     *
     * @param position    The position in the list of data that should be displayed in the
     *                    list item view.
     * @param convertView The recycled view to populate.
     * @param parent      The parent ViewGroup that is used for inflation.
     * @return The View for the position in the AdapterView.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }


        FoodItem currentFoodItem = getItem(position);


        TextView nameTextView = (TextView) listItemView.findViewById(R.id.item_name);

        nameTextView.setText(currentFoodItem.getItemName());


        TextView priceTextView = (TextView) listItemView.findViewById(R.id.item_price);


        priceTextView.setText(currentFoodItem.getItemPrice());



        TextView quantityTextView = (TextView) listItemView.findViewById(R.id.quantity_text_view);


        quantityTextView.setText(currentFoodItem.getQuantity());

        ImageView imgbutton = (ImageView) listItemView.findViewById(R.id.cart_minus_img);
        imgbutton.setOnClickListener(new View.OnClickListener()   {
            public void onClick(View v)  {

                    qty = qty -1;

            }
        });
        ImageView imgbutton2 = (ImageView) listItemView.findViewById(R.id.cart_minus_img);
        imgbutton2.setOnClickListener(new View.OnClickListener()   {
            public void onClick(View v)  {

                qty = qty  + 1;

            }
        });



        // Return the whole list item layout (containing 2 TextViews and an ImageView)
        // so that it can be shown in the ListView
        return listItemView;
    }

    public String getQuantity()
    {
        String strI = String.valueOf(qty);
        return strI;

    }
}

1 个答案:

答案 0 :(得分:0)

使用此代码将文本设置为视图。

quantityTextView.setText(String.valueOf(displayQuantity()));

String.valueOf()方法返回传递参数的字符串表示形式。

像:

System.out.println("Return Value : " + String.valueOf(1.4));

打印:返回值:1.4

System.out.println("Return Value : " + String.valueOf(true));

打印:返回值:true