繁体   English   中英

如何在 android 中保存赞按钮 state?

[英]How to save like button state in android?

我在 android 应用程序中使用 jd-alexander/LikeButton https://github.com/jd-alexander/LikeButton而不是普通按钮。 该代码在启用和禁用开关时工作正常。 但是我想保存like按钮的state。 假设我启用点赞按钮并交换列表,背景代码将运行良好,但点赞按钮 state 将更改为不喜欢。

每次我交换列表时,赞按钮 state 都会变得不受欢迎。 有什么办法可以保存点赞按钮 State?

活动代码:

public class CollectorListAdapter extends ArrayAdapter<Collector> {

    private static final String TAG = "CollectorListAdapter";
    private Context mContext;
    private int mResource;

    public CollectorListAdapter(Context context, int resource, ArrayList<Collector> objects) {

        super(context, resource, objects);
        mContext = context;
        mResource = resource;
    }

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


        //Get the Shop information
        String Shopname = getItem(position).getName();
        String Specialoffers = getItem(position).getSpecialoffers();
        int Price = getItem(position).getPrice();
        final Double startLatitude = getItem(position).getLatitude();
        final Double startLongitude = getItem(position).getLongitude();
        final String user_id = String.valueOf(getItem(position).getUserid());
        final String shop_id = String.valueOf(getItem(position).getShopid());
        final String product_id = String.valueOf(getItem(position).getProductid());

        //create the view result for showing the animation
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mResource, parent, false);
        TextView sname = (TextView) convertView.findViewById(R.id.textView);
        TextView tvname = (TextView) convertView.findViewById(R.id.textView7);
        TextView Location = (TextView) convertView.findViewById(R.id.textView9);
        TextView tvdescription = (TextView) convertView.findViewById(R.id.textView10);

        sname.setText(Shopname);
        tvdescription.setText(Specialoffers);
        tvname.setText(CurrencyFormatting(Integer.toString(Price)) + " EGP");
        Location.setText(format(results[0]) + " km");

        LikeButton heart;

        heart = convertView.findViewById(R.id.favBtn);

        heart.setOnLikeListener(new OnLikeListener() {

            // Add Data to the Saved Shop Table by like
            @Override
            public void liked(LikeButton likeButton) {

                StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.URL_SAVED_SHOPS, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, "Register Response: " + response.toString());
                        try {
                            JSONObject jObj = new JSONObject(response);
                            boolean error = jObj.getBoolean("error");
                            if (!error) {
                            } else {
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e(TAG, "Registration Error: " + error.getMessage());
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() {
                        // Posting params to register url
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("user_id", user_id);
                        params.put("shop_id", shop_id);
                        params.put("product_id", product_id);
                        return params;
                    }
                };
                Volley.newRequestQueue(getContext()).add(strReq);
                Toast.makeText(getContext(),
                        "Shop Saved Successfully", Toast.LENGTH_LONG).show();
            }

            // Delete Data to the Saved Shop Table by Unlike
            @Override
            public void unLiked(LikeButton likeButton) {

                StringRequest strReq10 = new StringRequest(Request.Method.POST, AppConfig.URL_Delete_SAVED_SHOPS, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, "Register Response: " + response.toString());
                        try {
                            JSONObject jObj = new JSONObject(response);
                            boolean error = jObj.getBoolean("error");
                            if (!error) {
                            } else {
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e(TAG, "Registration Error: " + error.getMessage());
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() {
                        // Posting params to register url
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("user_id", user_id);
                        params.put("shop_id", shop_id);
                        params.put("product_id", product_id);
                        return params;
                    }
                };
                Volley.newRequestQueue(getContext()).add(strReq10);
                Toast.makeText(getContext(),
                        "Shop Saved Deleted Successfully", Toast.LENGTH_LONG).show();
            }
        });

        return convertView;
    }
}

收集器 Class:

public class Collector implements java.io.Serializable {
    private String specialoffers, name;
    private Double latitude, longitude;
    private int price,userid,shopid,productid;

    public Collector() {

    }

    //Sorting by Price method
    public static Comparator<Collector> PriceSort = new Comparator<Collector>() {

        public int compare(Collector s1, Collector s2) {

            int rollno1 = s1.getPrice();
            int rollno2 = s2.getPrice();

            /*For ascending order*/
            return rollno1 - rollno2;

            /*For descending order*/
            //rollno2-rollno1;
        }
    };

    //Sorting by Distance method
    public static Comparator<Collector> DistanceSort = new Comparator<Collector>() {

        public int compare(Collector s1, Collector s2) {

            float[] results1 = new float[3];
            Location.distanceBetween(
                    LocationActivity.currentLocation.getLatitude(),
                    LocationActivity.currentLocation.getLongitude(),s1.getLatitude(),
                    s1.getLongitude(),
                    results1);

            float[] results2 = new float[3];
            Location.distanceBetween(
                    LocationActivity.currentLocation.getLatitude(),
                    LocationActivity.currentLocation.getLongitude(),s2.getLatitude(),
                    s2.getLongitude(),
                    results2);

            /*For ascending order*/
            return Float.compare(results1[0], results2[0]);

            /*For descending order*/
            //rollno2-rollno1;
        }
    };


    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public int getShopid() {
        return shopid;
    }

    public void setShopid(int shopid) {
        this.shopid = shopid;
    }

    public int getProductid() {
        return productid;
    }

    public void setProductid(int productid) {
        this.productid = productid;
    }

    public String toString() {
        return ("Shop Name:" + getName() +
                " Price : " + getPrice() +
                " SpecialOffers : " + getSpecialoffers() +
                " latitude : " + getLatitude()) +
                " longitude : " + getLongitude();
    }

    public String getSpecialoffers() {
        return specialoffers;
    }

    public void setSpecialoffers(String specialoffers) {
        this.specialoffers = specialoffers;
    }

    public String getName() {
        return name;
    }

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

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}

这是ArrayAdapter中的正常行为,整个列表会再次重新创建,因此您会丢失按钮的 state。 您应该将按钮的 boolean 保存在本地列表变量中。 Collector class 中添加一个类似isLiked = true/false 的字段,并在每次用户单击喜欢/不喜欢按钮时更新特定 position 的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM