在自定义ListView中重复值

时间:2015-01-18 20:11:02

标签: android android-listview baseadapter android-viewholder

我正在使用BaseAdapter使用自定义列表视图,但是我必须使用Viewholder来优化listView。

现在每次滚动列表视图时,每次都会显示5到6条记录。这是我正在使用的代码:

Context context;
LayoutInflater inflater;
MainActivity main;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> itemList;

static ImageLoader imageLoader;
Internholder holder;
String flight;
 View itemView ;
ImageView flag;
TextView flight_view,status_view;
TextView origin_item_view;
TextView expected_time_view,flight_item_view;
HashMap<String, String> resultp = new HashMap<String, String>();
List<String> values = new ArrayList<String>();

public khidomlistadapter(Context context,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;

    imageLoader = new ImageLoader(context);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // Declare Variables
    View v = convertView;
    Internholder holder;    
    if (v == null) {
    LayoutInflater vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    v = vi.inflate(R.layout.listview_item, null);
    holder = new Internholder();
    holder.flight_item_view = (TextView) v.findViewById(R.id.flight);
    holder.origin_item_view = (TextView) v.findViewById(R.id.origin);
    holder.expected_time_view = (TextView) v.findViewById(R.id.expected);
    holder.status_view =(TextView) v.findViewById(R.id.status);
    // Locate the ImageView in listview_item.xml
    holder.flag = (ImageView) v.findViewById(R.id.flag);
    v.setTag(holder);

    }
    else
    {
        holder = (Internholder) v.getTag();
    }
    // Get the position


    resultp = data.get(position);

     //Capture position and set results to the ImageView
     //Passes flag images URL into ImageLoader.class
    holder.flight_item_view.setText(resultp.get("flight"));
    holder.origin_item_view.setText(resultp.get("origin"));
    holder.expected_time_view.setText(resultp.get("expected_time"));
    holder.status_view.setText(resultp.get("status"));
    imageLoader.DisplayImage(resultp.get("flag"), holder.flag);



    return v;
}

有人能指出我做错了吗?

2 个答案:

答案 0 :(得分:0)

你可以像这样充气你的观点

v = inflater.inflate(R.layout.listview_item, parent, false);

可能会有所帮助。

答案 1 :(得分:0)

对于2个被覆盖的getter,你应该有适当的返回值。根据您的其余代码,这可能有效:

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}