gpt4 book ai didi

android - 如何在单击按钮时从 ListView 中删除特定行(Android)

转载 作者:行者123 更新时间:2023-11-29 15:45:38 26 4
gpt4 key购买 nike

我有自定义适配器类,在其中单击按钮我们对服务器进行异步调用。因此,在 onPostExecute() 方法中,我想删除单击按钮的特定行。这是我的适配器类

public class CartAdapter extends BaseAdapter {

private List<Products> cartList;

private LayoutInflater inflater;

Context context;

public static final String URLL ="http://192.168.1.3/wordpress/upmeapi/class-woocommerce.php?function=remove_cart_api";
RequestObject requestObject;


public CartAdapter(Context ctx,List<Products> list){
this.context = ctx;
this.cartList = list;
}
@Override
public int getCount() {
return cartList.size();
}

@Override
public Object getItem(int position) {
return cartList.get(position);
}

@Override
public long getItemId(int position) {
Products c = cartList.get(position);
long id = c.getProductId();
return id;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

View row = convertView;
CartHolder holder = null;
if (row == null){
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.cartview_helper,parent,false);
holder = new CartHolder(row);
row.setTag(holder);
}
else {
holder =(CartHolder)row.getTag();
}
Products c = cartList.get(position);
Picasso.Builder builder = new Picasso.Builder(context);
Picasso picasso = builder.build();
picasso.with(context).cancelRequest(holder.myImage);
picasso.load(c.getProductImage())
.placeholder(R.drawable.ic_plusone_tall_off_client)
.resize(100,100)
.into(holder.myImage);
/* Picasso.with(context)
.load(c.getProductImage())
.placeholder(R.drawable.ic_plusone_tall_off_client)
.resize(100, 75)
.into(holder.myImage);*/

holder.title.setText(c.getTitle());
String stringdouble= Double.toString(c.getPrice());
holder.price.setText(stringdouble);
holder.quantity.setText(String.valueOf(c.getProductQuantity()));
holder.totalPrice.setText(c.getTotalPrice());
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long productId = getItemId(position);
//holder.button.setTag(position);
try {
ProductHelper productHelper = new ProductHelper();
productHelper.setProductId(productId);
ObjectMapper objectMapper = new ObjectMapper();
String req = objectMapper.writeValueAsString(productHelper);

requestObject = ExceptionRequest.generateRequest(req);
requestObject.setUrl(URLL);
new RemovefromList().execute(requestObject);
}catch (Exception e) {
e.printStackTrace();
}
}
});



return row;
}

static class CartHolder {
ImageView myImage;

TextView title;

//TextView descriptions;

TextView price;

TextView quantity;

TextView totalPrice;

Button button;


public CartHolder(View v){
myImage =(ImageView)v.findViewById(R.id.imageView2);

title =(TextView)v.findViewById(R.id.carttitle);

// descriptions =(TextView)v.findViewById(R.id.product_description);

price =(TextView)v.findViewById(R.id.cart_price);

quantity =(TextView)v.findViewById(R.id.item_quantity);

totalPrice =(TextView)v.findViewById(R.id.sub_total);

button =(Button)v.findViewById(R.id.remove_cart);



}

}


private class RemovefromList extends AsyncTask<RequestObject, Void,JSONObject> {


@Override
protected JSONObject doInBackground(RequestObject... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// RequestObject requestObject = new RequestObject();

// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(arg0[0], ServiceHandler.POST);

JSONObject products = new JSONObject();

Log.d("Response: ", "> " + jsonStr);

if (jsonStr != null) {

try {
JSONObject jsonObj = new JSONObject(jsonStr);

products = jsonObj.getJSONObject("rsBody");
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}

return products;
}

@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
try {
if (result!=null){
String status = result.getString("status");


// cartList.remove();
// notifyDataSetChanged();
Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
}

} catch (JSONException e) {
e.printStackTrace();
}

return;

}
}



}

在 onPostExecute() 中,如果我获得状态成功,这意味着在服务器端该特定产品已被删除。但在我们这边我们仍然看到该产品。所以我想删除该行,还想更新 cartitem 的计数。任何帮助将不胜感激。

最佳答案

请检查一下

单击按钮时在异步任务构造函数中发送位置

private List<Products> cartList;
private LayoutInflater inflater;
Context context;

public static final String URLL ="http://192.168.1.3/wordpress/upmeapi/class-woocommerce.php?function=remove_cart_api";
RequestObject requestObject;


public CartAdapter(Context ctx,List<Products> list){
this.context = ctx;
this.cartList = list;
}
@Override
public int getCount() {
return cartList.size();
}

@Override
public Object getItem(int position) {
return cartList.get(position);
}

@Override
public long getItemId(int position) {
Products c = cartList.get(position);
long id = c.getProductId();
return id;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

View row = convertView;
CartHolder holder = null;
if (row == null){
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.cartview_helper,parent,false);
holder = new CartHolder(row);
row.setTag(holder);
}
else {
holder =(CartHolder)row.getTag();
}
Products c = cartList.get(position);
Picasso.Builder builder = new Picasso.Builder(context);
Picasso picasso = builder.build();
picasso.with(context).cancelRequest(holder.myImage);
picasso.load(c.getProductImage())
.placeholder(R.drawable.ic_plusone_tall_off_client)
.resize(100,100)
.into(holder.myImage);
/* Picasso.with(context)
.load(c.getProductImage())
.placeholder(R.drawable.ic_plusone_tall_off_client)
.resize(100, 75)
.into(holder.myImage);*/

holder.title.setText(c.getTitle());
String stringdouble= Double.toString(c.getPrice());
holder.price.setText(stringdouble);
holder.quantity.setText(String.valueOf(c.getProductQuantity()));
holder.totalPrice.setText(c.getTotalPrice());
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long productId = getItemId(position);
//holder.button.setTag(position);
try {
ProductHelper productHelper = new ProductHelper();
productHelper.setProductId(productId);
ObjectMapper objectMapper = new ObjectMapper();
String req = objectMapper.writeValueAsString(productHelper);

requestObject = ExceptionRequest.generateRequest(req);
requestObject.setUrl(URLL);
new RemovefromList(position).execute(requestObject);
}catch (Exception e) {
e.printStackTrace();
}
}
});



return row;
}

static class CartHolder {
ImageView myImage;

TextView title;

//TextView descriptions;

TextView price;

TextView quantity;

TextView totalPrice;

Button button;


public CartHolder(View v){
myImage =(ImageView)v.findViewById(R.id.imageView2);

title =(TextView)v.findViewById(R.id.carttitle);

// descriptions =(TextView)v.findViewById(R.id.product_description);

price =(TextView)v.findViewById(R.id.cart_price);

quantity =(TextView)v.findViewById(R.id.item_quantity);

totalPrice =(TextView)v.findViewById(R.id.sub_total);

button =(Button)v.findViewById(R.id.remove_cart);



}

}


private class RemovefromList extends AsyncTask<RequestObject, Void,JSONObject> {
int selectedPos;
public RemovefromList(int pos){
selectedPos = pos;
}

@Override
protected JSONObject doInBackground(RequestObject... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// RequestObject requestObject = new RequestObject();

// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(arg0[0], ServiceHandler.POST);

JSONObject products = new JSONObject();

Log.d("Response: ", "> " + jsonStr);

if (jsonStr != null) {

try {
JSONObject jsonObj = new JSONObject(jsonStr);

products = jsonObj.getJSONObject("rsBody");
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}

return products;
}

@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
try {
if (result!=null){
String status = result.getString("status");


cartList.remove(selectedPos);
notifyDataSetChanged();
Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
}

} catch (JSONException e) {
e.printStackTrace();
}

return;

}
}
}

关于android - 如何在单击按钮时从 ListView 中删除特定行(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33559712/

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