单击Edittext时,在Listview项目位置中出现问题

时间:2015-05-27 08:50:30

标签: android listview adapter

enter image description here

我正在使用ListView适配器,并且在单击EditText时尝试使按钮可见。它适用于一个项目,但在我尝试为多个项目执行此操作时无效。当我在第一个位置点击EditText时,那时我的其他位置项按钮不会显示。

public class CartAdapter extends BaseAdapter {

    Context context;
    ViewHolder holder;
    public ArrayList<CartList> arr_cart;
    private AsyncHttpClient client;
    private RequestParams params;
    private TextView tv_PayableAmount;
    double dNationalShippingCost, dInternationalShippingCost, dSellingPrice,
            dQuantity, TotalCost, dgetPayableTotal, dGrandTotal;
    String totalString = null, sShippingCost = null, sNationalShippingCost,
            sInternationalShippingCost, sQuantity, sSellingPrice,
            sgetPayableTotal, sGrandTotal;
    int count = 0;
    Double d = null;

    SessionManager sessionManager;
    HashMap<String, String> userData;

    private static LayoutInflater inflater = null;

    public CartAdapter(Context context, ArrayList<CartList> arr_cart,
            TextView tv_PayableAmount) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.arr_cart = arr_cart;
        this.tv_PayableAmount = tv_PayableAmount;
        sessionManager = new SessionManager(context);
        userData = sessionManager.getUserDetails();
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    private static class ViewHolder {
        private TextView tv_Title, tv_Price, tv_SellingPrice,
                tv_ShippingCharge, tv_TotalPrice, tv_Qty, tv_ShowSellingPrice,
                tv_ShowShippingCharge, tv_ShowTotalPrice;
        private EditText edt_Qty;
        private Button btn_Save;
        private ImageView iv_Cancel, iv_ProductImage;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arr_cart.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.cart_item, null);

            holder.iv_ProductImage = (ImageView) convertView
                    .findViewById(R.id.iv_ProductImage);
            holder.tv_Title = (TextView) convertView
                    .findViewById(R.id.tv_Title);
            holder.tv_Qty = (TextView) convertView.findViewById(R.id.tv_Qty);
            holder.edt_Qty = (EditText) convertView.findViewById(R.id.edt_Qty);
            holder.btn_Save = (Button) convertView.findViewById(R.id.btn_Save);
            holder.tv_Price = (TextView) convertView
                    .findViewById(R.id.tv_Price);
            holder.tv_ShowSellingPrice = (TextView) convertView
                    .findViewById(R.id.tv_ShowSellingPrice);
            holder.tv_SellingPrice = (TextView) convertView
                    .findViewById(R.id.tv_SellingPrice);
            holder.tv_ShowShippingCharge = (TextView) convertView
                    .findViewById(R.id.tv_ShowShippingCharge);
            holder.tv_ShippingCharge = (TextView) convertView
                    .findViewById(R.id.tv_ShippingCharge);
            holder.tv_ShowTotalPrice = (TextView) convertView
                    .findViewById(R.id.tv_ShowTotalPrice);
            holder.tv_TotalPrice = (TextView) convertView
                    .findViewById(R.id.tv_TotalPrice);
            holder.iv_Cancel = (ImageView) convertView
                    .findViewById(R.id.iv_Cancel);

            // holder.iv_Cancel.setTag(arr_cart.get(position));
            // holder.btn_Save.setTag(arr_cart.get(position));
            // holder.edt_Qty.setTag(arr_cart.get(position));

            setTypeface();
            convertView.setTag(holder);
        } else {
            // the getTag returns the viewHolder object set as a tag to the view
            holder = (ViewHolder) convertView.getTag();
        }

        String[] image = arr_cart.get(position).getImage().split(",");
        String firstImage = image[0];
        if (firstImage.equals("")) {

        } else {
            Picasso.with(this.context).load(firstImage)
                    .into(holder.iv_ProductImage);
        }
        holder.tv_Title.setText(arr_cart.get(position).getAddTitle());
        holder.edt_Qty.setText(arr_cart.get(position).getQuantity());
        holder.tv_Price.setText(arr_cart.get(position).getAddPrice());
        holder.tv_SellingPrice.setText(arr_cart.get(position)
                .getAddSellingPrice());

        makeTotal(position);
        holder.iv_Cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                deleteCartItem(position);
            }
        });
        holder.edt_Qty.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // updatequantity = holder.edt_Qty.getText().toString();
                holder.btn_Save.setVisibility(View.VISIBLE);
            }
        });
        holder.btn_Save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                updateQuantity(position);
                holder.btn_Save.setVisibility(View.GONE);
            }

        });

        return convertView;
    }

    private void makeTotal(int position) {
        // TODO Auto-generated method stub
        try {
            // get cost in String
            sSellingPrice = arr_cart.get(position).getAddSellingPrice();
            sNationalShippingCost = arr_cart.get(position)
                    .getAddNationalShippingCharge();
            sInternationalShippingCost = arr_cart.get(position)
                    .getAddWorldShippingCharge();
            sQuantity = holder.edt_Qty.getText().toString();
            // sQuantity = arr_cart.get(position).getQuantity();

            // convert in to double
            dSellingPrice = Double.parseDouble(sSellingPrice);
            dNationalShippingCost = Double.parseDouble(sNationalShippingCost);
            dInternationalShippingCost = Double
                    .parseDouble(sInternationalShippingCost);
            dQuantity = Double.parseDouble(sQuantity);

            // total Shipping Cost in double
            TotalCost = dQuantity
                    * (dInternationalShippingCost + dSellingPrice);

            // convert in to string
            sShippingCost = Double.toString(dInternationalShippingCost);
            totalString = Double.toString(TotalCost);

            holder.tv_ShippingCharge.setText(sShippingCost);
            holder.tv_TotalPrice.setText(totalString);

        } catch (NumberFormatException e) {
        }
    }

    private void setTypeface() {
        // TODO Auto-generated method stub
        holder.tv_Title.setTypeface(Global.SetFontRegular(context));
        holder.tv_Qty.setTypeface(Global.SetFontRegular(context));
        holder.edt_Qty.setTypeface(Global.SetFontRegular(context));
        holder.btn_Save.setTypeface(Global.SetFontRegular(context));
        holder.tv_Price.setTypeface(Global.SetFontRegular(context));
        holder.tv_SellingPrice.setTypeface(Global.SetFontRegular(context));
        holder.tv_ShowSellingPrice.setTypeface(Global.SetFontRegular(context));
        holder.tv_ShippingCharge.setTypeface(Global.SetFontRegular(context));
        holder.tv_ShowShippingCharge
                .setTypeface(Global.SetFontRegular(context));
        holder.tv_TotalPrice.setTypeface(Global.SetFontRegular(context));
        holder.tv_ShowTotalPrice.setTypeface(Global.SetFontRegular(context));
        holder.tv_Title.setTypeface(Global.SetFontRegular(context));
        holder.tv_Title.setTypeface(Global.SetFontRegular(context));
    }

    private void deleteCartItam(final int delete_position) {
        // TODO Auto-generated method stub
        client = new AsyncHttpClient();
        params = new RequestParams();

        client.getHttpClient()
                .getParams()
                .setParameter(CoreProtocolPNames.USER_AGENT,
                        Global.SERVER_HEADER);

        params.put("cartId", arr_cart.get(delete_position).getCartId());
        Log.e("params", "" + params);

        client.post(Global.REMOVE_PRODUCT_FROM_CART_URL, params,
                new JsonHttpResponseHandler() {

                    @Override
                    public void onStart() {
                        // TODO Auto-generated method stub
                        super.onStart();
                    }

                    @Override
                    public void onSuccess(int statusCode, Header[] headers,
                            JSONObject response) {
                        // TODO Auto-generated method stub
                        if (response != null) {
                            try {
                                if (response.getInt("ResponseCode") == 1
                                        && response.getBoolean("Result") == true) {
                                    // Log.e("OnSuccess", "OnSuccess : "
                                    // + response);
                                    makeTotal(delete_position);

                                    sgetPayableTotal = tv_PayableAmount
                                            .getText().toString();
                                    dgetPayableTotal = Double
                                            .parseDouble(sgetPayableTotal);
                                    // Log.e("total pay", "" + dgt);
                                    // Log.e("delete total", "" + TotalCost);
                                    dGrandTotal = dgetPayableTotal - TotalCost;
                                    // Log.e("after delete pos", "" + grnto);
                                    sGrandTotal = Double.toString(dGrandTotal);
                                    tv_PayableAmount.setText(sGrandTotal);

                                    arr_cart.remove(delete_position);
                                    arr_cart.trimToSize();
                                    CartAdapter.this.notifyDataSetChanged();
                                    Global.ShowTost(context,
                                            "Your Item is Deleted successfuly");

                                }
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                            String responseString, Throwable throwable) {
                        // TODO Auto-generated method stub
                        super.onFailure(statusCode, headers, responseString,
                                throwable);
                        Log.e("onFailure", "onFailure : " + responseString);
                    }

                    @Override
                    public void onFinish() {
                        // TODO Auto-generated method stub
                        super.onFinish();
                    }
                });
    }

    @SuppressWarnings("static-access")
    private void updateQuantity(final int update_position) {
        // TODO Auto-generated method stub
        client = new AsyncHttpClient();
        params = new RequestParams();

        client.getHttpClient()
                .getParams()
                .setParameter(CoreProtocolPNames.USER_AGENT,
                        Global.SERVER_HEADER);

        params.put("userId", userData.get(sessionManager.KEY_USERID));
        params.put("addId", arr_cart.get(update_position).getAddId());
        params.put("quantity", holder.edt_Qty.getText().toString());
        params.put("totalPrice", TotalCost);
        Log.e("params", "" + params);

        client.post(Global.INSERT_PRODUCT_IN_CART_URL, params,
                new JsonHttpResponseHandler() {

                    @Override
                    public void onStart() {
                        // TODO Auto-generated method stub
                        super.onStart();
                    }

                    @Override
                    public void onSuccess(int statusCode, Header[] headers,
                            JSONObject response) {
                        // TODO Auto-generated method stub
                        if (response != null) {
                            try {
                                if (response.getInt("ResponseCode") == 1
                                        && response.getBoolean("Result") == true) {
                                    // Log.e("OnSuccess", "OnSuccess : "
                                    // + response);
                                    // Log.e("olddddddddd", "" + TotalCost);
                                    makeTotal(update_position);
                                    sgetPayableTotal = tv_PayableAmount
                                            .getText().toString();
                                    dgetPayableTotal = Double
                                            .parseDouble(sgetPayableTotal);
                                    // Log.e("old totel", "" +
                                    // dgetPayableTotal);
                                    // Log.e("after update quantity", ""
                                    // + TotalCost);
                                    // Log.e("old delete total", ""
                                    // + arr_cart.get(update_position)
                                    // .getTotalPrice());
                                    if (count == 0) {
                                        updateQuantity(update_position);
                                        // Log.e("in loop old totel", ""
                                        // + dgetPayableTotal);
                                        // Log.e("in loop after update quantity",
                                        // "" + TotalCost);
                                        // Log.e("in loop old delete total", ""
                                        // + arr_cart.get(update_position)
                                        // .getTotalPrice());
                                        String str = arr_cart.get(
                                                update_position)
                                                .getTotalPrice();
                                        d = Double.parseDouble(str);
                                        // Log.e("dddd", "" + d);
                                        double add = dgetPayableTotal
                                                + TotalCost;
                                        // Log.e("add", "" + add);
                                        dGrandTotal = add - d;
                                        // Log.e("dGrandTotal", "" +
                                        // dGrandTotal);
                                        count++;
                                    } else {

                                    }

                                    sGrandTotal = Double.toString(dGrandTotal);
                                    tv_PayableAmount.setText(sGrandTotal);

                                    // CartAdapter.this.notifyDataSetChanged();
                                }
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                            String responseString, Throwable throwable) {
                        // TODO Auto-generated method stub
                        super.onFailure(statusCode, headers, responseString,
                                throwable);
                        Log.e("onFailure", "onFailure : " + responseString);
                    }

                    @Override
                    public void onFinish() {
                        // TODO Auto-generated method stub
                        super.onFinish();
                    }
                });
    }
}

1 个答案:

答案 0 :(得分:1)

这是您的问题的示例代码

ButtonList.java

package com.example.helppeople;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;

public class ButtonList extends ActionBarActivity {

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

    ListView mLists = (ListView) findViewById(R.id.listbutton);
    ArrayList<SampleList> mList = new ArrayList<SampleList>();

    mList.add(new SampleList("123"));
    mList.add(new SampleList("456"));
    mList.add(new SampleList("789"));
    mList.add(new SampleList("012"));

    ButtonAdapter mAdapter = new ButtonAdapter(getApplicationContext(),
            mList);

    mLists.setAdapter(mAdapter);

  }
}

listview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

ButtonAdapter.java

package com.example.helppeople;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class ButtonAdapter extends BaseAdapter {
private ArrayList<SampleList> mList;
private Context mContext;

/*
 * Creates the Adapter for the list
 */
public ButtonAdapter(Context context, ArrayList<SampleList> list) {
    mList = list;
    mContext = context;

}

/**
 * inflate view for each row
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    /* if the given channel row view is not being updated */
    if (v == null) {
        /* inflate layout */
        LayoutInflater vi = LayoutInflater.from(mContext);
        v = vi.inflate(R.layout.rowlist, null, false);
    }

    /* get the component pointers */
    final TextView etname = (TextView) v.findViewById(R.id.textView1);
    final Button button = (Button) v.findViewById(R.id.button1);

    etname.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            button.setVisibility(View.VISIBLE);
        }
    });

    return v;
}

@Override
public int getCount() {

    return mList.size();
}

@Override
public SampleList getItem(int position) {
    return mList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
   }

}

rowlist.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Button"
    android:visibility="gone" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignParentLeft="true"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/black" />

</RelativeLayout>

SampleList.java

package com.example.helppeople;

public class SampleList {
String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public SampleList(String name) {
    super();
    this.name = name;
}

}

这是工作代码,希望它可以帮助你