getItemAtPosition()在列表视图中不返回值 [英] getItemAtPosition() not returning value in listview

查看:235
本文介绍了getItemAtPosition()在列表视图中不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的基本适配器类,以用图像和文本填充列表视图. 类的代码如下:

I have created a custom Base Adapter class, to populate a list view with image and text. The code of class is as below :

public class ViewAdapter extends BaseAdapter {

private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 
private ArrayList<String> items;

public ViewAdapter(Activity a, ArrayList<String> items,String[] d) {
    this.items = items;     
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.length;
}

public Object getItem(int position) {
    return position;
}

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

public static class ViewHolder{
    public TextView text;
    public ImageView image;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        vi = inflater.inflate(R.layout.sectionview, null);
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.txtLogoName);;
        holder.image=(ImageView)vi.findViewById(R.id.imgBrandLogo);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    holder.text.setText(items.get(position));
    holder.image.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, holder.image);
    return vi;    
}

}

到目前为止,列表工作正常,但是当我使用下面的代码获取特定位置的项目值时,它返回的是列表视图中项目的位置,而不是值.

so far the list is working smoothly, but when I use the below code to get the value of the item at particular poisition, but instead of value, it is returning the item position of the list view.

即我正在使用的代码是

public void onItemClick(AdapterView<?> parent, View V, int position, long arg3) {

                System.out.println("****SELECTED VALUES " Parent.getItemAtPosition(position));

    });

即使使用getItemIdAtPosition(position)也会返回该值.

Even using getItemIdAtPosition(position) is also returning the value.

对此需要紧急帮助,适配器类中是否需要更改任何内容?

Need urgent help on this, is there any thing needed to be changed in the adapter class??

推荐答案

我将方法getItem(int position)更改为

I changed the method getItem(int position) to

public Object getItem(int position) {
    return items.get(position);
}

现在我将其称为:

System.out.println("****SELECTED VALUES " + secAdap.getItem(position));

所以现在可以正常工作了.

and so its working fine now.

这篇关于getItemAtPosition()在列表视图中不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆