简体   繁体   中英

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". But when i click it on first listview item, another textview listview items below are changing. 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,

first you must define you int[] like below and set to zero every index.

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

something like this in getview:

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

and after else statement in getView :

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

and in 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!!

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