简体   繁体   English

Android Listview可点击的TextView冲突

[英]Android Listview clickable textview conflict

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". 我已经用List_row_layout.xml(用json serializable类填充)填充了一个Listview,我具有可单击的textview和onclick,将文本从“ Accept”更改为“ Accepted”。 But when i click it on first listview item, another textview listview items below are changing. 但是,当我在第一个listview项目上单击它时,下面的另一个textview listview项目正在更改。 Here's some photos to descibe you better 这是一些可以更好地描述您的照片

在单击接受文本视图之前

单击第一行中的接受后

Activity class 活动课

     feedListView.setAdapter(new CustomListAdapter(this, feedList));

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.title);
        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 = listData.get(position);

    holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
    holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));

    holder.approve.setTag(newsItem);



    return convertView;
}

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

textview code 文字检视程式码

            <TextView
            android:id="@id/approveTV"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:background="@drawable/pressed"
            android:gravity="fill"
            android:text="Accept"
            android:clickable="true"
            android:textColor="#0D98BA"
            android:textSize="17sp" />

您必须编写一些跟踪位置功能,以记住更改的位置并确保仅更改位置文本。

you must set id to each row, and save it in one array,like integer and when you click on one row you must change the value of that, and in changing text or anything you must check the value of that row,if 0 then set default and if 1 then so what you want: my idea is create int[] with length of that is size of your list, 您必须为每一行设置id,并将其保存在一个数组中,例如整数,当您单击一行时,必须更改该行的值,并且在更改文本或任何内容时必须检查该行的值,如果为0则设置默认值,如果设置为1那么您想要的是:我的想法是创建int[] ,其长度是列表的大小,

first you must define you int[] like below and set to zero every index. 首先,您必须像下面那样定义int []并将每个索引设置为零。

    int[] selected = new int[listData.getsize()]

something like this in getview: 在getview中是这样的:

    holder.approve.setId(Html.fromHtml(newsItem.getId());

and after else statement in getView : getView else语句之后:

  if (selected[position] == 0)
  {
     // set default
  }
  else
  {
   // any changing that you want 
  }

and in onClick: 并在onClick中:

    selected[holder.approve.getId()] = 1;
    // any change that you want

Try the below code:- 试试下面的代码:

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

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


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

    FeedItem newsItem = listData.get(position);

    holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
    holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));



    holder.approve.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View argView)
                {
                    holder.approve.setText("accepted");
                }
            }
        );



    return convertView;
}

Thanks!! 谢谢!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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