Android listview repeating some elements in a clickitem click element

This is my screenshot

This is 1 list item (contains 1 image and 3 image buttons and an identifier). My ListView has 50 ListItem.

in this case, when the user clicks on the button (first button), updates the value and sets the text to this specific position (this works). But it repeats a value similar to this (when you click on the first one, like the button-> Position 0, then it repeats the value for the 4th, 7th, etc. Position.

How to solve the problem of repeating the given text for each 4th element?

Please help me. Thanks at Advance.

My code is, / ** Adapter class * /

public class Adapter1 extends BaseAdapter {
        ImageView imgUnlike[] = null, imgLike[] = null,
                imgComments[] = null;
        TextView txtLikeUnlike[] = null, txtComments[] = null,

        public ArrayList<HashMap<String, String>> arr = null;
        Context context = null;
        LayoutInflater layoutInflater = null;
        HashMap<String, String> getData = new HashMap<String, String>();
        String urlLike = null, urlCountLike = null, urlUnlike = null;
        String strCountLike = null, strCountCommnets = null;

        public Adapter1(Context context,
                ArrayList<HashMap<String, String>> arr) {

            this.context = context;
            this.arr = arr;
            layoutInflater = LayoutInflater.from(context);
            this.imgUnlike = new ImageView[arr.size()];
            this.imgLike = new ImageView[arr.size()];
            this.imgComments = new ImageView[arr.size()];
            this.txtLikeUnlike = new TextView[arr.size()];
        }

        @Override
        public int getCount() {
            return arr.size();
        }

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

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

        @SuppressLint("InflateParams")
        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View row = null;
            if (convertView == null) {
                LayoutInflater inflater = ((Activity) context)
                        .getLayoutInflater();
                row = inflater.inflate(R.layout.list_item, parent,
                        false);
            } else {
                row = convertView;
            }
            /** Initialize Widgets */
            /** Imageview */

            imgUnlike[position] = (ImageView) row
                    .findViewById(R.id.imgUnlike);

            imgLike[position] = (ImageView) row
                    .findViewById(R.id.imgLike);


            /** TextView */
            txtLikeUnlike[position] = (TextView) row
                    .findViewById(R.id.txtLikeUnlike);

            getData = arr.get(position);





            txtLikeUnlike[position].setText(getData
                    .get(Fragment1.TAG_TOTAL_LIKE_COUNT));



            imgUnlike[position]
                    .setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            imgUnlike[position]
                                    .setVisibility(View.INVISIBLE);
                            imgLike[position]
                                    .setVisibility(View.VISIBLE);

                            strPostId = arr.get(position).get(
                                    Fragment1.TAG_POST_ID);

                            urlLike = Urls.BASE_URL
                                    + "createpostlike.php?post_id=" + strPostId
                                    + "&user_id=" + myDetail.getUserId();

                            getCurrentPosition = position;
                            new sendLikeData().execute();
                        }
                    });

            imgLike[position].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    imgLike[position].setVisibility(View.INVISIBLE);
                    imgUnlike[position].setVisibility(View.VISIBLE);

                    urlGetAllLike = Urls.BASE_URL + "getlike.php";

                    getCurrentPosition = position;
                    new getAllLikeData().execute();
                }
            });


            return row;
        }




public class sendLikeData extends AsyncTask<Void, Void, Void> {
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... arg0) {
                ServiceHandler sh = new ServiceHandler();
                String jsonStr = sh
                        .makeServiceCall(urlLike, ServiceHandler.GET);

                return null;
            }

            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                urlCountLike = Urls.BASE_URL + "getalllike.php?post_id="
                        + strPostId;
                new getCountLikeData().execute();
            };
        }


/** Count the Total Like for Selected Items. */
        private class getCountLikeData extends AsyncTask<Void, Void, Void> {
            JSONObject jsonobject;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... arg0) {

                dataList = new ArrayList<HashMap<String, String>>();
                // Retrieve JSON Objects from the given URL address
                jsonobject = JSONFunctions.getJSONfromURL(urlCountLike);
                try {
                    // Locate the array name in JSON
                    jsonarray = jsonobject.getJSONArray("data");

                    for (int i = jsonarray.length() - 1; i < jsonarray.length(); i++) {

                        jsonobject = jsonarray.getJSONObject(i);
                        // Retrive JSON Objects
                        strCountLike = jsonobject.getString("Total");
                    }

                } catch (JSONException e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

                Adapter1.notifyDataSetChanged();

                txtLikeUnlike[getCurrentPosition].setText(strCountLike);

            }
        }
+4
source share
2 answers

, , :

public class Adapter1 extends BaseAdapter {


    public ArrayList<HashMap<String, String>> arr = null;
    Context context = null;
    LayoutInflater layoutInflater = null;
    HashMap<String, String> getData = new HashMap<String, String>();
    String urlLike = null, urlCountLike = null, urlUnlike = null;
    String strCountLike = null, strCountCommnets = null;

    public Adapter1(Context context,
            ArrayList<HashMap<String, String>> arr) {

        this.context = context;
        this.arr = arr;
        layoutInflater = LayoutInflater.from(context);
        this.imgUnlike = new ImageView[arr.size()];
        this.imgLike = new ImageView[arr.size()];
        this.imgComments = new ImageView[arr.size()];
        this.txtLikeUnlike = new TextView[arr.size()];
    }

    @Override
    public int getCount() {
        return arr.size();
    }

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

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

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

        final ViewHolder holder;

        View row = null;
        if (convertView == null) {

            LayoutInflater inflater = ((Activity) context)
                    .getLayoutInflater();
            row = inflater.inflate(R.layout.list_item, parent,
                    false);

            holder = new ViewHolder();

            holder.imgUnlike[position] = (ImageView) row
                    .findViewById(R.id.imgUnlike);

            holder.imgLike[position] = (ImageView) row
                    .findViewById(R.id.imgLike);


            /** TextView */
            holder.txtLikeUnlike[position] = (TextView) row
                    .findViewById(R.id.txtLikeUnlike);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /** Initialize Widgets */
        /** Imageview */


        getData = arr.get(position);




        holder.txtLikeUnlike[position].setText(getData
                .get(Fragment1.TAG_TOTAL_LIKE_COUNT));



        holder.imgUnlike[position]
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        holder.imgUnlike[position]
                                .setVisibility(View.INVISIBLE);
                        holder.imgLike[position]
                                .setVisibility(View.VISIBLE);

                        holder.strPostId = arr.get(position).get(
                                Fragment1.TAG_POST_ID);

                        urlLike = Urls.BASE_URL
                                + "createpostlike.php?post_id=" + strPostId
                                + "&user_id=" + myDetail.getUserId();

                        getCurrentPosition = position;
                        new sendLikeData().execute();
                    }
                });

        holder.imgLike[position].setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                holder.imgLike[position].setVisibility(View.INVISIBLE);
                holder.imgUnlike[position].setVisibility(View.VISIBLE);

                urlGetAllLike = Urls.BASE_URL + "getlike.php";

                getCurrentPosition = position;
                new getAllLikeData().execute();
            }
        });


        return row;
    }

    public class ViewHolder {
        ImageView imgUnlike[] = null, imgLike[] = null,
                imgComments[] = null;
        TextView txtLikeUnlike[] = null, txtComments[] = null;
    }
}
-1

-,

public class LikeModel{

private String likes ;

public void setLikes(String likes){
this.likes = likes ;
}

public String getLikes(){
return likes ;
}

}

, , like , Gson, bean .

public class Adapter1 extends BaseAdapter {


        public Adapter1(Context context,
                ArrayList<HashMap<String, String>> arr) {

           // your own implementation or reference check here
        }

        @Override
        public int getCount() {
            return arr.size();
        }

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

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

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View row = null;
            if (convertView == null) {
               ayoutInflater inflater = ((Activity) context)
                    .getLayoutInflater();
            row = inflater.inflate(R.layout.list_item, parent,
                    false);

            holder = new ViewHolder();

            holder.YOUR_LIKE_TEXT_VIEW = (TextView)convertView.findViewById(R.id.your_id);
            // Same way refer other view(like image view etc)

           convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            LikeModel model = likeModelArray.get(position);

            holder.YOUR_LIKE_TEXT_VIEW.setText(model.getLikes());

            holder.YOUR_LIKE_IMAGE.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {


                    urlGetAllLike = Urls.BASE_URL + "getlike.php";

                    getCurrentPosition = position;
                    new getAllLikeData().execute(PASS POISTION HERE);
                }
            });


            return row;
        }

Async Task

public class sendLikeData extends AsyncTask<Void, Void, Void> {
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... arg0) {
                ServiceHandler sh = new ServiceHandler();
                String jsonStr = sh
                        .makeServiceCall(urlLike, ServiceHandler.GET);

                return null;
            }

            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                urlCountLike = Urls.BASE_URL + "getalllike.php?post_id="
                        + strPostId;
                new getCountLikeData().execute();
            };
        }


/** Count the Total Like for Selected Items. */
        private class getCountLikeData extends AsyncTask<Void, Void, Void> {


            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... arg0) {

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);



                LikeModel model = likeModelArray.get(position);
                model.setLikes(SET COUNTER VALUE HERE);
                notifyDataSetChanged();

                // 

            }
        }
-1

Source: https://habr.com/ru/post/1567976/


All Articles