Question

I am created a view of current week using GridView. I use a BaseAdapter for generating the GridView.

this is how i give values to the GridView

        ArrayList<WeekCellItem> weekCellItems = getData(scheduleListdto);
    gridview1.setAdapter(new WeekAdapter(getApplicationContext(),weekCellItems));

And I need the CurrentItem at postion when i clicked on each cell on the GridView.

I wrote the onItemClickListener for the GridView

gridview1.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Object o= gridview1.getItemAtPosition(arg2);
            Log.e("", "hhh");
        }
    });

I get the object 'o' as null.

(EDIT)

my weekAdapter

public class WeekAdapter extends BaseAdapter
{
Context mycontext;
ArrayList<String>weekHeads;
ArrayList<WeekCellItem> weekCellItems;
LayoutInflater inflater;
public WeekAdapter(Context c, ArrayList<WeekCellItem> items, ArrayList<String> obj)
{
    weekCellItems=items;
    weekHeads=obj;
    mycontext=c;
    inflater = LayoutInflater.from(c);
}
public int getCount() {
    // TODO Auto-generated method stub
    return weekCellItems.size();
}

public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(int pos, View convertview, ViewGroup parent) {
    // TODO Auto-generated method stub
    viewHolder holder = new viewHolder();
    if(convertview==null)
    {
        convertview = inflater.inflate(R.layout.weekly_layout, null);
        holder.tv = (TextView) convertview.findViewById(R.id.textweeklylayout);
        convertview.setTag(holder);
    }
    else
        holder=(viewHolder)convertview.getTag();
    return convertview;
}
private static class viewHolder
{
    TextView tv;
}}
Was it helpful?

Solution

You need to implement the adapter's getItem() method. gridview.getItemAtPosition() in turns call adapter's getItem()-method.

Your WeekAdapter's getItem() returns null. That's the problem. It should be:

public Object getItem(int arg0) {
    return weekCellItems.get(arg0); 
}

OTHER TIPS

Add following code.

public Object getItem(int arg0) {
    if (weekCellItems == null || weekCellItems.size() == 0)
        return null;

    return weekCellItems.get(arg0); 
}

Why : You are implementing your custom Adapter, so you need to modify getItem() as your need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top