简体   繁体   中英

Android Listview clickable textview

I have made an Listview populated with list_row_layout.xml(which is populated with json serializable class), i have clickable textview and onclick changing text from "Accept" to "Accepted". But when i click it on first listview item, another textview listview items below are changing. Here's some photos to descibe you better

在单击接受文本视图之前

单击拳头后接受textview

this is the adapter class

   public class CustomListAdapter extends BaseAdapter {

    private ArrayList<FeedItem> listData;

    private LayoutInflater layoutInflater;

    private Context mContext;

    public CustomListAdapter(Context context, ArrayList<FeedItem> listData) {
            this.listData = listData;
            layoutInflater = LayoutInflater.from(context);
            mContext = context;
    }

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

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

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


    public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                     convertView = layoutInflater.inflate(R.layout.list_row_layout, null);

                    holder = new ViewHolder();
                    holder.headlineView = (TextView)convertView.findViewById(R.id.sex);
                    holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
                    holder.approve = (TextView) convertView.findViewById(R.id.approveTV);



                     holder.approve.setOnClickListener(new OnClickListener()
                    {
                        @Override

                 public void onClick(View argView)
                  {
                holder.approve.setText("Accepted");
               }
                    }
                );

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

            FeedItem newsItem = (FeedItem) listData.get(position);
            holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
            holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));




            return convertView;
    }



    static class ViewHolder {
           TextView approve;
            TextView headlineView;
            TextView reportedDateView;
            ImageView imageView;

    }
   }

Remember that views can be recycled via convertView.

In your onClick method you set the approve text to "Accepted" but when the view is recycled, you never set it back to "Accept"

Actually you need to update (something in) the list in response to an click and have the Accept/Accepted value toggle based on that value rather than simply updating what is currently visible on the screen.

-- to answer the "how" question (asked below)--

Add a new field to ViewHolder

static class ViewHolder {
        TextView approve;
        TextView headlineView;
        TextView reportedDateView;
        ImageView imageView;
        FeedItem newsItem;
}

Change the onClick method:

           public void onClick(View argView)
           {
              // note that holder no longer needs to be final in the parent class
              // because it is not used here.
              View parent = (View)argView.getParent();
              ViewHolder clickedHolder = (ViewHolder)parent.getTag();
              clickedHolder .newsItem.setAccepted(true);    /// a new method
              clickedHolder .approve.setText ("Accepted");
              Log.d(TAG, "Accepted item #" + position);
           }

After you have convertView created (if necessary)

        FeedItem newsItem = (FeedItem) listData.get(position);
        holder.newsItem = newsItem; // populate the new field.
        holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
        holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
        if(newsItem.isAccepted ()){  // another new method!
           holder.approve.setText ("Accepted");
           Log.d(TAG, "Set text to Accepted for item #" + position);
        }else{
           holder.approve.setText("Accept");
           Log.d(TAG, "Set text to Accept for item #" + position);
        } 

Once it is working you should consider removing the Log.d() lines to cut down on the noise in LogCat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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