Android Listview可点击的textview冲突

时间:2013-12-03 12:42:56

标签: java android listview android-listview

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

Before clicking the accept textview

after clicking accept in first row

活动类

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

适配器类

    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代码

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你必须为每一行设置id,并将其保存在一个数组中,如整数,当你点击一行时你必须改变它的值,并且在更改文本或任何内容时你必须检查该行的值,如果0然后设置默认值,那么1那么你想要的是:我的想法是创建int[],其长度是列表的大小,

首先你必须像下面那样定义int []并将每个索引设置为零。

    int[] selected = new int[listData.getsize()]
在getview中

这样的事情:

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

以及else中的getView语句:

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

并在onClick上:

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

答案 2 :(得分:0)

请尝试以下代码: -

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

谢谢!