Question

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

Before clicking the accept textview

after clicking accept in first row

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" />

No correct solution

OTHER TIPS

You have to write some tracking position functionality which remember the position which you have changed and make sure that only text of position changed .

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top