views:

39

answers:

2

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??

A: 

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.

Romain Guy
A: 

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.

kaibuki