ListView按钮位置重复

时间:2013-12-08 19:27:38

标签: android listview position

我有一个用BaseAdapter填充的listview。在listview中我有2个textviews和2个按钮,我想要点击,我确实设置了它并且工作正常但问题是当我点击一个textview时,下面的第4个,第8个列表项也被点击了,我发现他们有打印位置与吐司相同的位置。

Position on Toast

我怎样才能让这些职位成为++并且每个职位都是独一无二的?

适配器类

  public class CustomListAdapter extends BaseAdapter  
 {

private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;

protected ListView feedListView;


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

}




public void addItem(final FeedItem item) {
    listData.add(item);
    notifyDataSetChanged();
}

@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( final int position, View convertView, ViewGroup parent)
{
 final ViewHolder holder;
 View row=convertView;
    if (row == null)
    {
     convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
     holder = new ViewHolder();
     holder.headlineView = (TextView)convertView.findViewById(R.id.name);
     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 = (FeedItem) listData.get(position);
    holder.approve.setFocusable(true);
    holder.approve.setClickable(true);
    holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
    holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));



    holder.approve.setTag(position); 
    holder.approve.setOnClickListener(new OnClickListener() {           
      @Override
      public void onClick(View v) 
      {

        Toast.makeText(mContext, String.valueOf(v.getTag()), Toast.LENGTH_LONG).show();
      holder.approve.setTag(v);
      holder.approve.setText("Approved");



      }    
   });

    return convertView;
}

static class ViewHolder
{

    TextView approve;
    TextView headlineView;
    TextView reportedDateView;
    ImageView imageView;
    FeedItem newsItem;

}

}

3 个答案:

答案 0 :(得分:1)

Listview回收视图。

How ListView's recycling mechanism works

更改getView

public View getView( final int position, View convertView, ViewGroup parent)
{
 ViewHolder holder;
    if (convertView == null)
    {
     convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
     holder = new ViewHolder();
     holder.headlineView = (TextView)convertView.findViewById(R.id.name);
     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 = (FeedItem) listData.get(position);
    holder.approve.setFocusable(true);
    holder.approve.setClickable(true);
    holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
    holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
    holder.approve.setTag(position); 
    holder.approve.setOnClickListener(new OnClickListener() {           
      @Override
      public void onClick(View v) 
      {
        Toast.makeText(mContext, String.valueOf(v.getTag()), Toast.LENGTH_LONG).show();
      }    
   });

    return convertView;
}

答案 1 :(得分:1)

当您重复使用

视图时
else{
        holder = (ViewHolder) convertView.getTag();
    }

所以多个textView使用相同的监听器。所以把你的onclickListener放在if条件之外。

if (convertView == null)
    {
     convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
     holder = new ViewHolder();
     holder.headlineView = (TextView)convertView.findViewById(R.id.name);
     holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
     holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
     convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();

    }

    holder.approve.setOnClickListener(new OnClickListener() {           

         @Override
        public void onClick(View v) 
         {
         Toast.makeText(mContext, String.valueOf(position), Toast.LENGTH_LONG).show();
          }    
        });

答案 2 :(得分:0)

在列表适配器中添加此代码:

    @Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return questionsClassArrayList_.size();
}

@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return position;
}