【发布时间】: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