gpt4 book ai didi

java - 从 getView 中的 onClickListener 内部更新 ArrayAdapter

转载 作者:行者123 更新时间:2023-12-02 04:21:03 24 4
gpt4 key购买 nike

我正在制作一个购物车应用程序,用户可以在其中添加和删除购物车中的商品。当用户查看他们的购物车时,会显示他们已添加的商品列表。该列表是使用 ArrayAdapter 创建的。每行都包含一个删除按钮,用户可以在其中从购物车中删除商品。我已成功地从购物车对象中删除该项目。但是,当用户单击删除按钮时更新屏幕时,我遇到了麻烦。我听说过适配器的 notificationdatasetchanged() 方法,但我不确定如何从 getView 中的 OnClickListener 内部实现该方法。这是代码:

CartScreen.java

package com.livilatenight.livilatenight;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SaveCallback;

public class CartScreen extends ActionBarActivity {

ListView items;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart_screen);

final ParseUser current = ParseUser.getCurrentUser();

try{
Cart userCart = (Cart) current.fetchIfNeeded().get("cart");
String[] itemNames = userCart.getItems();
String[] itemPrices = userCart.getPrices();
int[] quantities = userCart.getQuantities();
Integer[] itemPictures = userCart.getPictures();

CartListAdapter adapter = new CartListAdapter(CartScreen.this, itemNames,itemPrices,quantities,itemPictures);
items = (ListView) findViewById(R.id.listView);
items.setAdapter(adapter);
}catch(ParseException e){
Toast.makeText(CartScreen.this, e.toString(),Toast.LENGTH_LONG).show();
}

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_cart_screen, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

CartListAdapter.java

package com.livilatenight.livilatenight;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.ParseUser;

public class CartListAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] items;
private final String[] prices;
private final int[] quantities;
private final Integer[] pictures;

public CartListAdapter(Activity context, String[] itemnames, String[] prices, int[] quantities, Integer[] pictures) {
super(context, R.layout.item_list, itemnames);

this.context=context;
this.items=itemnames;
this.prices=prices;
this.quantities=quantities;
this.pictures=pictures;
}

public View getView(final int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.cart_item, null,true);

TextView itemName = (TextView) rowView.findViewById(R.id.tvItemName);
TextView itemPrice = (TextView) rowView.findViewById(R.id.tvPrice);
ImageView itemPicture = (ImageView) rowView.findViewById(R.id.ivItemPicture);
View blankView = (View) rowView.findViewById(R.id.blankView);
Button removeCart = (Button) rowView.findViewById(R.id.btnRemoveCart);

final Spinner spinner = (Spinner) rowView.findViewById(R.id.spinnerQty);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.spinner_numbers, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);


spinner.setSelection(quantities[position]-1);

itemName.setText(items[position]);
itemPrice.setText(prices[position]);
itemPicture.setImageResource(pictures[position]);

removeCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ParseUser current = ParseUser.getCurrentUser();
int quantity = spinner.getSelectedItemPosition()+1;

Cart userCart = (Cart) current.get("cart");
boolean success = userCart.removeFromCart(items[position]);

if(success){
Toast.makeText(getContext(), "Removed from Cart", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getContext(), "Item is already in your shopping cart", Toast.LENGTH_LONG).show();
}

current.saveInBackground();
}
});
return rowView;
};
}

我想在用户单击“removeCart”按钮时更新购物车列表。我怎样才能做到这一点?

更新

我做了一个解决方法,每次删除一个项目时我都会设置一个新的适配器。这不是我想做的,但它有效,所以我会保持原样。

最佳答案

试试这个

if (success) {
Toast.makeText(getContext(), "Removed from Cart", Toast.LENGTH_LONG).show();
final String s = items[position];
List<String> list = new ArrayList<String>(Arrays.asList(items));
list.remove(s);
items = list.toArray(array);
this.notifyDataSetChanged()
}

关于java - 从 getView 中的 onClickListener 内部更新 ArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32769790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com