getItemAtPosition() not returning value in listview

只谈情不闲聊 提交于 2019-12-07 16:57:46

问题


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.

i.e. the code I am using is

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

                System.out.println("****SELECTED VALUES " Parent.getItemAtPosition(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??


回答1:


I changed the method getItem(int position) to

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

and now I am calling it as :

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

and so its working fine now.




回答2:


In your adapter you wrote:

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

So this is working as expected since you are returning the position as the item.



来源:https://stackoverflow.com/questions/3965654/getitematposition-not-returning-value-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!