[android]更改目的上一行但影响显示在下一行

发布时间: 2016/9/26 4:12:32
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

你好朋友我不能够理解如何在列表中的同一行中设置文本 view 我在那里有两个 button 与文本 view 中心,但当我正要递增或递减 effect 在下一行而不是我想要应用更改的行上显示Please check the Screen shot

CartAdapter.java

public class Cart_Adapter extends BaseAdapter {
    Context cartcontext;
    List<MobiData> cartlist;
    LayoutInflater inflater;
    cartlist cartdata;
    public ArrayList<Integer> quantity = new ArrayList<Integer>();
    CustomButtonListener customButtonListener;
    public Cart_Adapter(Context cartcontext, List<MobiData> cartlist) {
        this.cartcontext = cartcontext;
        this.cartlist = cartlist;
}
 @Override
    public int getCount() {
        return cartlist.size();
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }
@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        inflater = (LayoutInflater) cartcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            cartdata = new cartlist();
            convertView = inflater.inflate(R.layout.cart_row, parent, false);
            cartdata.decrement = (TextView) convertView.findViewById(R.id.decrement);
            cartdata.single = (TextView) convertView.findViewById(R.id.single);
            cartdata.single.setTag(position);
            cartdata.increment = (TextView) convertView.findViewById(R.id.increment);
            cartdata.increment.setTag(position);
            cartdata.cancel = (TextView) convertView.findViewById(R.id.cancel);
            cartdata.vcmedname = (TextView) convertView.findViewById(R.id.vcmedname);
            cartdata.vcmedprice = (TextView) convertView.findViewById(R.id.vcmedprice);
            Typeface carttext = Typeface.createFromAsset(cartcontext.getAssets(), "fonts/fontawesome.ttf");
            cartdata.decrement.setTypeface(carttext);
            cartdata.increment.setTypeface(carttext);
            cartdata.cancel.setTypeface(carttext);
            convertView.setTag(cartdata);
        } else {
            cartdata = (cartlist) convertView.getTag();
        }
        MobiData newcart = cartlist.get(position);
        cartdata.vcmedname.setText(newcart.getVcmedname());
        cartdata.vcmedprice.setText(newcart.getVcmedprice());
        cartdata.single.setText(newcart.getVcqty());
        final String cartids = newcart.getVcmedid();
        cartdata.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cartlist.remove(position);
                notifyDataSetChanged();
            }
        });
        cartdata.increment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View v) {
                if (customButtonListener !=null){
                    int plus = Integer.parseInt(cartdata.single.getText().toString());
                plus++;
                                    int plus = Integer.parseInt(cartdata.single.getText().toString());
                plus++;
                cartdata.single.setText(String.valueOf(plus));
                SharedPreferences viewpref = cartcontext.getSharedPreferences("datapref", Context.MODE_PRIVATE);
                String cartuid = viewpref.getString("uid", "");
                String carttempid = viewpref.getString("tempid", "");
                String incremnturl = "http://sampletemplates.net/mobichemist/json/cart_process.php?mid=" + cartids + "&userid=" + cartuid + "&tempid=" + carttempid;
                Log.d("Incremnturl", incremnturl);
                JsonArrayRequest incrementarray = new JsonArrayRequest(Request.Method.GET, incremnturl, (String) null, new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject incrobj = response.getJSONObject(i);
                                int plus = Integer.parseInt(cartdata.single.getText().toString());
                                plus++;

                               cartdata.single.setText(String.valueOf(plus));

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

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("Incrementurl", String.valueOf(error));
                    }
                });
                incrementarray.setRetryPolicy(new DefaultRetryPolicy(50000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                           AppController.getInstance().addToRequestQueue(incrementarray);


            }
        });
        cartdata.decrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 int i = Integer.parseInt(cartdata.single.getText().toString());
            i--;
            if (i <= 0) {
                Toast.makeText(Single_Cart_Page.this, "Minimum Quantity is 1", Toast.LENGTH_SHORT).show();
            } else {
                cartdata.single.setText(String.valueOf(i));
            }
            }
        });
        return convertView;
    }

    static class cartlist {
        TextView decrement, single, increment, cancel, vcmedname, vcmedprice;
    }

解决方法 1:

试试这个和添加其他代码。

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

    ViewHolder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.cart_row, parent, false);
        holder = new ViewHolder();
        holder.decrement = (TextView) convertView.findViewById(R.id.decrement);
        holder.single = (TextView) convertView.findViewById(R.id.single);
        holder.increment = (TextView) convertView.findViewById(R.id.increment);
        holder.cancel = (TextView) convertView.findViewById(R.id.cancel);
        holder.vcmedname = (TextView) convertView.findViewById(R.id.vcmedname);
        holder.vcmedprice = (TextView) convertView.findViewById(R.id.vcmedprice);

        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }
        Typeface carttext = Typeface.createFromAsset(cartcontext.getAssets(), "fonts/fontawesome.ttf");
        holder.decrement.setTypeface(carttext);
        holder.increment.setTypeface(carttext);
        holder.cancel.setTypeface(carttext);

    MobiData newcart = cartlist.get(position);
    holder.vcmedname.setText(newcart.getVcmedname());
    holder.vcmedprice.setText(newcart.getVcmedprice());
    holder.single.setText(newcart.getVcqty());
    final String cartids = newcart.getVcmedid();

    holder.cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cartlist.remove(position);
            notifyDataSetChanged();
        }
    });
    return convertView;
}

static class ViewHolder {

  private TextView decrement;
  private TextView single;
  private TextView increment;
  private TextView vcmedname;
  private TextView vcmedprice;

 }
赞助商