【问题标题】:Add button to put extra in Intent and Remove button to empty all添加按钮以将额外内容放入 Intent 并删除按钮以清空所有内容
【发布时间】:2015-12-04 15:34:22
【问题描述】:

我的 listView 适配器类中有添加和删除按钮,如下所示。我想通过单击“删除”按钮来删除单击“添加”按钮时添加的所有内容。问题是我尝试了getIntent.removeExtra();,但它不起作用。

有什么想法吗?

public class CustomRestaurantMenuAdapter extends ArrayAdapter<Restaurant> {
private List<Restaurant> items;

public CustomRestaurantMenuAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}
public CustomRestaurantMenuAdapter(Context context, int resource, List<Restaurant> items) {
    super(context, resource, items);
    this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.menu_item_list, null);
    }
    Restaurant p = getItem(position);
    if (p != null) {
        TextView foodId = (TextView) v.findViewById(R.id._id);
        final TextView foodName = (TextView) v.findViewById(R.id.name);
        TextView foodDescription = (TextView) v.findViewById(R.id.description);
        final TextView foodPrice = (TextView) v.findViewById(R.id.price);
      final  Button  addButton = (Button) v.findViewById(R.id.addButton);
       final Button  removeButton = (Button) v.findViewById(R.id.removeButton);

        if (foodId != null) {
            foodId.setText("" + p.getId());
        }
        if (foodName != null) {
            foodName.setText("" + p.getName());
        }
        if (foodDescription != null) {
            foodDescription.setText(p.getDescription());
        }
        if (foodPrice != null) {
            foodPrice.setText(p.getPrice());
        }

        removeButton.setVisibility(View.INVISIBLE);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String sName = foodName.getText().toString();
                String sPrice = foodPrice.getText().toString();
                Intent orderDetails = new Intent(getContext(), OrderActivity.class);
                orderDetails.putExtra("name", sName);
                orderDetails.putExtra("price", sPrice);
                Log.d("NAME  ", sName);
                Log.d("PRICE  ", sPrice);
                removeButton.setVisibility(View.VISIBLE);
            }
        });

        removeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //remove what is added to intent here or undo the add action
                removeButton.setVisibility(View.INVISIBLE);
            }
        });

    }
    return v;
}
 }

【问题讨论】:

  • 你在哪里使用getIntent.removeExtra()
  • 你在用 orderDetails 意图做什么?你的代码中没有任何 startactivity
  • 我厌倦了在 removeButton 中使用它,但我得到“无法解析方法 getIntent()”@ρяσѕρєя K
  • 发布您的完整课程

标签: android listview button android-intent


【解决方案1】:

您可以像这样重新初始化您的意图,而不是一一删除所有额外内容:

removeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // Re-init your intent
            orderDetails = new Intent(getContext(), OrderActivity.class);
        }
    });

【讨论】:

    【解决方案2】:

    你可以为你的 Intent 定义一个类成员,然后当你想摆脱额外的东西时只需调用yourIntent = new Intent();。这将是最简单的方法,因为您正在从多个内部类操作您的 Intent。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 2023-01-22
      • 2011-08-23
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 2014-11-27
      相关资源
      最近更新 更多