如何在baseadapter中单击列表视图项时更新文本

时间:2015-02-25 05:48:40

标签: android listview baseadapter

我是android的新手,我遇到了这个问题,我有一个会计算喜欢的文本,onclick会像在文本视图中设置总喜欢的那样。我写的代码是为了增加holder.likestatus点击之类的。请帮助我....我正在进行为期3天的bt没有得到确切的答案。

这是baseadapter

    public View getView(final int position, View convertView, final ViewGroup parent) {
            // Declare Variables
          //  TextView rank;
          //  TextView country;
            TextView likestat = null;
              if(convertView==null){
                  LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                  convertView = inflater.inflate(R.layout.feed_item, null);
                  holder=new ViewHolder();
                  pos=getItemViewType(position);
                  holder.name = (TextView) convertView.findViewById(R.id.name);

                  holder.status=(TextView)convertView.findViewById(R.id.txtStatusMsg);
                  holder.userimage=(ImageView)convertView.findViewById(R.id.profilePic);
                  holder.postimage=(ImageView)convertView.findViewById(R.id.feedImage1);
                  holder.like=(TextView)convertView.findViewById(R.id.like);
                //  likestat=(TextView)convertView.findViewById(R.id.likecount);
                  holder.likestatus=(TextView)convertView.findViewById(R.id.likecount);
                  holder.comment=(TextView)convertView.findViewById(R.id.comment);
                  holder.commentstatus=(TextView)convertView.findViewById(R.id.commentcount);
                  holder.pos=position;

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

       holder.comment.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                feedid=data.get(position).get("TempFeed_id");
                openDialog();
                new Comment().execute();
            }
        });
       holder.postimage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                  feedimage=data.get(position).get("TempPost_Image");
                  feedimagediaload();
            }
        });

         /*   View itemView = inflater.inflate(R.layout.viewitem, parent, false);*/
            // Get the position
            resultp = data.get(position);
            String count=resultp.get("TempLike");
            holder.likestatus.setText(resultp.get("TempLike"));
            holder.comment.setText("Comment"+" "+resultp.get("TempCommnt"));
            holder.commentstatus.setText(resultp.get("TempCommnt"));
            // Locate the TextViews in listview_item.xml
            holder.name.setText(resultp.get("TempFirstName"));

            String status=resultp.get("Temp_Status");

           holder.like.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    feedid=data.get(position).get("TempFeed_id");
                    String pos=data.get(position).get("TempLike");
                    int c=Integer.parseInt(pos);
                       c++;
                       pos=c+"";

                     holder.likestatus.setText(pos);
                     ****// no increment on the like
                     //what code i have to written here****

                    new likepost().execute();
                    //here i call execute method to like the post



                }
            });

2 个答案:

答案 0 :(得分:1)

  

如何在单击baseadapter中的列表视图项目时更新文本

默认情况下TextView无法点击,因此请务必使用xml中的TextView属性设置android:clickable="true"点击

  

点击like:

增加holder.likestatus

更新likestatus TextView,如like按钮点击:

holder.like.setOnClickListener(new OnClickListener() {

@Override
   public void onClick(View v) {
   //...your code here....        

   ViewGroup viewGroup=(ViewGroup)v.getParent();
   // access likecount TextView from feed_item layout
   TextView txtLikeStatus=(TextView)viewGroup.findViewById(R.id.likecount);
   txtLikeStatus.setText(String.valueOf(pos));

   }
});

答案 1 :(得分:0)

点击更新,如果这不起作用,请告诉我,从下面的代码打印日志值。

holder.like.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                feedid=data.get(position).get("TempFeed_id");
                String likeCount=data.get(position).get("TempLike");

                Log.i("likeCount", likeCount);

                int counter=Integer.valueOf(likeCount)+1;                

                Log.i("counter", counter);

                 holder.likestatus.setText(counter+"");

                new likepost().execute();
                //here i call execute method to like the post



            }
        });