【问题标题】:android listview repeating some items on listitem click eventandroid listview在listitem点击事件上重复一些项目
【发布时间】:2014-12-17 11:34:15
【问题描述】:

这是我的应用程序屏幕截图,

这是 1 个列表项(包含 1 个 imageview 和 3 个 imagebutton 和 id)。我的 ListView 中有 50 个 ListItem 就像这样。

在此当用户单击喜欢按钮(第一个按钮)时,值会更新并将文本设置到该特定位置(这是有效的)但它会重复这样的值(当单击第一个喜欢按钮时 -> 位置 0 然后它重复第 4、7 等位置的值。

如何解决每 4 项重复设置文本的问题?

请指导我。 提前致谢。

我的代码是, /** 适配器类 */

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);

            }
        }

【问题讨论】:

标签: android listview


【解决方案1】:

我认为您的适配器类将类似于下面使用 viewholder:

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;
    }
}

【讨论】:

  • @Reena,创建异步任务的构造函数,并在调用异步任务时将持有者传递给它。
  • 告诉我投反对票的原因。
  • holder.imgUnlike[位置] = (ImageView) row.findViewById(R.id.imgUnlike);你在 null convertView 检查中有这个,所以你在持有人的标签中只设置了位置 0。然后,您可以使用除 0 之外的任何其他位置访问它 holder.txtLikeUnlike[position] = (TextView) row .findViewById(R.id.txtLikeUnlike);这将为您提供其他参考的结果。认为你理解了
  • @SuhailMehta 请发送给我使用 ImageView 数组的 View Holder 示例代码。
  • @SuhailMehta 我正在使用 ViewHolder 然后再次面临同样的问题
【解决方案2】:

这是在特定 cmets 处添加所需代码的伪代码

public class LikeModel{

private String likes ;

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

public String getLikes(){
return likes ;
}

}

现在,当您从服务器获取数据时,只需在此处设置 likes 值并将模型传递给适配器,或者您可以使用 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;
        }

然后是异步任务

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();

                // 

            }
        }

【讨论】:

  • 感谢给我演示
  • 明天进一步讨论
猜你喜欢
  • 2013-04-12
  • 1970-01-01
  • 2017-06-28
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
相关资源
最近更新 更多