-2

我在进入适配器并在 textview 中显示的地方有响应,但问题是它在 listview 中打印了两次该项目。我检查了响应。它也只有一项。但是当我在调试模式下检查时,循环会在适配器中打印两次。我找不到解决方案。例如:在一个列表中,如果它有 10 个项目。(A、B、C 等)它显示为 20 个项目(A、A、B、B、C、C 等)请帮助我。

 public class ListAdaptersTest extends BaseAdapter  {
ArrayList<Persons> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
ImageLoader loader;
 Persons movie;
  Activity activity;
  public static final String NAME="name";
    public static final String image="image";
    ArrayList<Persons> data=new ArrayList<Persons>();


public ListAdaptersTest(Activity context, int resource, ArrayList<Persons> movies) {
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    data=movies;
    activity=context;

      loader=new ImageLoader(context.getApplicationContext());

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // convert view = design
    View v = convertView;
    try{
    if (v == null) {
        holder = new ViewHolder();
        v = vi.inflate(Resource, null);
        holder.tvName = (TextView) v.findViewById(R.id.tvname);
    v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
     movie = null;
        movie = data.get(position);
        final   String name=movie.getName();
        holder.tvName.setText(name);
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
    return v;

}

static class ViewHolder {
    public TextView tvName;
}

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


@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return data.get(position);
}

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

}

在此处输入图像描述

当我检查响应时,它显示为

E/REsponse(4989):     Response{"id":27136,"cast_id":3,"order":0,"credit_id":"52fe4e769251416c7515736b","profile_path":"\/rrN6d7WR.jpg","name":"Katharine Isabelle","character":"Tamara"}

10-29 02:01:56.934: E/Final(4989): Finaltrue E/REsponse(4989): Response{"id":21320,"cast_id":4,"order":1,"credit_id":"52fe4e769251416c7515736f ","profile_path":"/d9YmZ.jpg","name":"Danielle Harris","character":"Amy"} E/Final(4989): Finaltrue E/REsponse(4989): Response{"id" :27775,"cast_id":5,"order":2,"credit_id":"52fe4e769251416c75157373","profile_path":"/gNeWQz9oqF.jpg","name":"Chelan Simmons","character":"Kayla" } E/Final(4989): Finaltrue 10-29 02:01:56.937: E/REsponse(4989): Response{"id":61903,"cast_id":6,"order":3,"credit_id":" 53da2fb40e0a2652f000200d","profile_path":"/jMUbn5I63NDlqM650.jpg","name":"Glenn Thomas Jacobs","character":"Jacob Goodnight"} 10-29 02:01:56.937: E/Final(4989): Finaltrue

4

2 回答 2

0

尝试在您的公共 View getView 方法中注释掉这行代码

if (v == null) {

由于回收问题,这可能是问题的根源。

于 2014-10-28T20:39:19.067 回答
0

我建议您知道,我认为您的问题由两个原因引起,首先您应该使用数组适配器而不是基本适配器,其次您应该看到我们如何调用适配器以及在您的活动中创建人员列表。

于 2014-10-28T21:01:19.647 回答