How to inflate another layout inside getView() of gridview adapter in android?
Asked Answered
S

1

8

Weekly_Calendar_View I want to create weekly calendar view and inside each grid item (each day) there are may be several activities.Out of this I have created weekly calendar view using grid view but I want to add activities if there are any for particular date by dynamically checking db. Like same as in image. Below is my getView() code..

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.calendar_week_gridcell, parent, false);
    }

    txtRowTitle = (TextView) row.findViewById(R.id.txtDayTitle);

    LinearLayout root = (LinearLayout) row.findViewById(R.id.linear_root);

    String dayTitle = list.get(position);
    txtRowTitle.setText(dayTitle);
    if (position == currentWeekDay - 1)
        root.setBackgroundResource(R.drawable.calheader);
    if (!activityMap.isEmpty() && activityMap.containsKey(activityDateList.get(position))) {
        TextView item = new TextView(mContext);
        item.setText(activityMap.get(activityDateList.get(position)));
        item.setBackgroundColor(Color.GREEN);
        root.addView(item);
    }

    return row;
}

}

Here I am temporarily trying to add text view dynamically but I want to inflate here my custom activity_item layout and add it to grid cell.

Sweeney answered 13/7, 2013 at 10:59 Comment(2)
I have tried to bind activity items to list view for grid view cell but it's not working properly. I want to inflate here my custom activity_item and it to grid view cell. How I can do it?Sweeney
@blackbelt can you tell me how can inflate here my custom layout and add it to grid view item for number of activities available on that day.Sweeney
R
1

do you mean that you wish to add multiple rows in the area that is now being taken by the "Busy" space ? do you want it to take as much space as it needs, or should it be scrollable?

anyway, it seems you are in the right direction. you used a linearLayout to hold the "busy" area, right? so just use a for-loop and put their the data you wish to show.

of course, because the gridView recycles items, you would need to empty the linearLayout every time you reach the getView, but it should still work.

if you wish to use the layoutInflater in the for-loop, that's also possible:

 subRow = inflater.inflate(R.layout.sub_row, linearLayout, true);
Rhiamon answered 13/7, 2013 at 11:30 Comment(4)
Actually I want to add that "Busy" like multiple items for number of activities available on that particular day. So how to inflate this custom activity_item layout multiple times and add it to root Linear Layout.Sweeney
i'm not sure i understand. don't you mean that instead of "busy", you would have a list of items inside? if so, that's the answer i've already written. you also need to decide if it should be scrollable or not, and maybe the maximum number of items to show. it's now a design question. the technical details come after the design...Rhiamon
I will try your solution and let you know...thanks for your suggestion.Sweeney
@Sweeney I am working on similar kind of screen can u post some code if u found solution?Adaurd

© 2022 - 2024 — McMap. All rights reserved.