Android Listview可点击的textview

时间:2013-12-02 20:17:15

标签: java android listview textview

我已经使用list_row_layout.xml填充了Listview(其中填充了json可序列化类),我有可点击的textview和onclick将文本从“Accept”更改为“Accepted”。但是当我在第一个listview项目上单击它时,下面的另一个textview listview项目正在发生变化。 这里有一些照片可以更好地描述你

before clicking the accept textview

After clicking the fist accept textview

这是适配器类

   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;

    }
   }

1 个答案:

答案 0 :(得分:3)

请记住,可以通过convertView回收视图。

在onClick方法中,您将批准文本设置为“已接受”,但是当视图被回收时,您永远不会将其设置为“接受”

实际上,您需要更新(某些内容)列表以响应点击,并根据该值切换接受/接受值,而不是简单地更新屏幕上当前可见的内容。

- 回答“如何”问题(如下所述) -

向ViewHolder添加新字段

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

更改onClick方法:

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

创建convertView后(如有必要)

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

一旦它工作,你应该考虑删除Log.d()行以减少LogCat中的噪音。