在列表视图中滚动重复文本

时间:2016-03-12 20:02:02

标签: android listview android-fragments interface android-arrayadapter

我在列表视图中遇到问题,当我点击加号按钮增量时,但是当我滚动列表视图时,文本已被重置,增量值也在其他项目中更新,我正在使用getter和setter方法保存递增值的数据,我也使用接口来更新片段类中的文本,但它没有更新

Otc_Fragment.Java

public class Otc_Fragment extends Fragment implements CartNumber {
    ArrayList<MobiData> otcdatalist;
    ListView otc_list;
    Otc_Fragment_Adapter otcFragmentAdapter;
    TextView cart;
    static TextView cartnumber;
    RelativeLayout cartbox;

    public Otc_Fragment() {
    }

    public static Fragment getInstance(int position) {
        Otc_Fragment otcFragment = new Otc_Fragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        otcFragment.setArguments(bundle);
        return otcFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View otcview = inflater.inflate(R.layout.fragments_otc, container, false);
        otc_list = (ListView) otcview.findViewById(R.id.otc_list);
        cartnumber = (TextView) otcview.findViewById(R.id.cartnumber);
        cart = (TextView) otcview.findViewById(R.id.cart);
        cartbox= (RelativeLayout) otcview.findViewById(R.id.cartbox);
        Typeface cartface = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontawesome.ttf");
        cart.setTypeface(cartface);
        otcdatalist = new ArrayList<MobiData>();
        otcFragmentAdapter = new Otc_Fragment_Adapter(getActivity(), otcdatalist, this);
        otc_list.setAdapter(otcFragmentAdapter);
        catitems();
        return otcview;
    }

    private void catitems() {
        String medurl = getResources().getString(R.string.CommonUrl) + "medicines_catwise.php";
        final int position = getArguments().getInt("position", 0);
        JsonObjectRequest medobjreq = new JsonObjectRequest(Request.Method.GET, medurl, (String) null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONObject orderobj1 = response.getJSONObject("orders");
                    JSONArray orderarray = orderobj1.getJSONArray("menu1");
                    JSONObject orderobj = orderarray.getJSONObject(position);
                    JSONArray itemsarray = orderobj.getJSONArray("items");
                    for (int i = 0; i < itemsarray.length(); i++) {
                        JSONObject itemsobj = itemsarray.getJSONObject(i);
                        MobiData itemsdata = new MobiData();
                        itemsdata.setProductidotc(itemsobj.getString("id"));
                        itemsdata.setProductnameotc(itemsobj.getString("medicine_name"));
                        itemsdata.setProductpriceotc(itemsobj.getString("price"));
                        otcdatalist.add(itemsdata);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                otcFragmentAdapter.notifyDataSetChanged();

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

    @Override
    public void setcount(String count) {
        Toast.makeText(getActivity(), count, Toast.LENGTH_LONG).show();
        cartnumber.setText(count);
    }
}

Otc_Fragment_Adapter.Java

public class Otc_Fragment_Adapter extends ArrayAdapter<MobiData> {
    ArrayList<MobiData> otcproducrlist;
    CartNumber cartNumber;
    String tid;
    SharedPreferences preferences;
    SharedPreferences.Editor editorpref;

    public Otc_Fragment_Adapter(Context context, ArrayList<MobiData> otcproducrlist, CartNumber cartNumber) {
        super(context, R.layout.fragment_otc_row, otcproducrlist);
        this.otcproducrlist = otcproducrlist;
        this.cartNumber = cartNumber;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Productholder productholder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.fragment_otc_row, parent, false);
            productholder = new Productholder();
            productholder.productdecrement = (TextView) convertView.findViewById(R.id.productdecrement);
            productholder.productsingle = (TextView) convertView.findViewById(R.id.productsingle);
            productholder.productincrement = (TextView) convertView.findViewById(R.id.productincrement);
            productholder.productname = (TextView) convertView.findViewById(R.id.productname);
            productholder.product_price = (TextView) convertView.findViewById(R.id.product_price);
            convertView.setTag(productholder);
        } else {
            productholder = (Productholder) convertView.getTag();
        }
        Typeface otctext = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontawesome.ttf");
        productholder.productdecrement.setTypeface(otctext);
        productholder.productincrement.setTypeface(otctext);
        final MobiData productdata = otcproducrlist.get(position);
        productholder.productname.setText(productdata.getProductnameotc());
        productholder.product_price.setText(productdata.getProductpriceotc());
        productdata.setSinglelist(productholder.productsingle.getText().toString());
        productholder.productsingle.setText(productdata.getSinglelist());
        final String proids = productdata.getProductidotc();
        SharedPreferences tempvalue = getContext().getSharedPreferences("datapref", Context.MODE_PRIVATE);
        String tempid = tempvalue.getString("tempid", "");
        if (tempid != "") {
            tid = tempid;
        } else {
            int min = 65;
            int max = 8000;
            Random r = new Random();
            tid = String.valueOf(r.nextInt(max - min + 1) + min);
            preferences = getContext().getSharedPreferences("datapref", Context.MODE_PRIVATE);
            editorpref = preferences.edit();
            editorpref.putString("tempid", tid);
            editorpref.apply();

        }
        productholder.productincrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int plus = Integer.parseInt(productholder.productsingle.getText().toString());
                plus++;
                productholder.productsingle.setText(String.valueOf(plus));
                productdata.setSinglelist(String.valueOf(plus));
                SharedPreferences uid = getContext().getSharedPreferences("datapref", Context.MODE_PRIVATE);
                String userid = uid.getString("uid", "");
                String addcarturl = getContext().getResources().getString(R.string.CommonUrl) + "add_to_cart.php?userid=" + userid + "&mid=" + proids + "&quantity=" + String.valueOf(plus) + "&tempid=" + tid;
                Log.d("Addcart", addcarturl);
                JsonObjectRequest addreq = new JsonObjectRequest(Request.Method.GET, addcarturl, (String) null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            String count = response.getString("count");
                            cartNumber.setcount(count);
                        } catch (JSONException e) {
                            Log.d("OtcAddCart", String.valueOf(e));
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("AddCart", String.valueOf(error));
                    }
                });
                addreq.setRetryPolicy(new DefaultRetryPolicy(10000000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            }
        });
        productholder.productdecrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int minus = Integer.parseInt(productholder.productsingle.getText().toString());
                if (minus <= 0) {
                    Toast.makeText(getContext(), "Minimum Quantity is 1", Toast.LENGTH_SHORT).show();
                    productholder.productsingle.setText(String.valueOf(minus));
                    productdata.setSinglelist(String.valueOf(minus));
                } else {
                    minus--;
                    productholder.productsingle.setText(String.valueOf(minus));
                    productdata.setSinglelist(String.valueOf(minus));
                    SharedPreferences viewpref = getContext().getSharedPreferences("datapref", Context.MODE_PRIVATE);
                    String cartduid = viewpref.getString("uid", "");
                    String cartdtempid = viewpref.getString("tempid", "");
                    String decremnturl = getContext().getResources().getString(R.string.CommonUrl) + "del_cartitems.php?mid=" + proids + "&userid=" + cartduid + "&tempid=" + cartdtempid;
                    Log.d("Decremnturl", decremnturl);
                    JsonObjectRequest decrementreq = new JsonObjectRequest(Request.Method.GET, decremnturl, (String) null, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d("Response decremnet", String.valueOf(response));
                            try {
                                JSONArray decarray = response.getJSONArray("items");
                                Log.d("Cartdecrement", String.valueOf(decarray));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

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

        });
        return convertView;
    }

    static class Productholder {
        private TextView productname;
        private TextView product_price;
        private TextView productdecrement;
        private TextView productsingle;
        private TextView productincrement;
    }
}

CartNumber.java

public interface CartNumber {  
       public void setcount(String count);
}

0 个答案:

没有答案