-
Notifications
You must be signed in to change notification settings - Fork 0
/
category_adapter.java
69 lines (56 loc) · 2.4 KB
/
category_adapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.foodapp.food.recipe.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.foodapp.food.recipe.helper.ImageHelper;
import com.foodapp.food.recipe.model.CategoryItem;
import com.foodapp.food.recipe.R;
import java.util.ArrayList;
public class category_adapter extends BaseAdapter
{
private LayoutInflater inflater;
private ArrayList<CategoryItem> categoryList;
private int layout; //layout is ID of the layout resource that getView() inflates to create the view.
private ImageHelper imageHelper = new ImageHelper();
public category_adapter(Context context, ArrayList<CategoryItem> categoryList, int layout)
{
this.inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.categoryList = categoryList;
this.layout = layout;
}
@Override //returns the total number of items to be displayed in a list
public int getCount()
{
return categoryList.size();
}
@Override // return the object at position in data set
public Object getItem(int position) {
return categoryList.get(position).get_category();
}
@Override // returns a long value of item position to the adapter.
public long getItemId(int position) {
return position;
}
@Override // called when the list item view is ready to be displayed
public View getView(int position, View convertView, ViewGroup parent)
{
//A convert view is a view that is currently not in the screen and hence can be recycled
//basically uses existing view
if(convertView==null)
{ //if it's not recycled, initialize some attributes
convertView=inflater.inflate(layout,parent,false);
}
// sets model of category item
CategoryItem categoryItem = categoryList.get(position);
ImageView icon= convertView.findViewById(R.id.categoryitem_img);
icon.setImageBitmap(imageHelper.getBitmapFromByteArray(categoryItem.get_mainImg()));
// goes to "fragment_category_item" layout and sets up the layout and populate daata
TextView name = convertView.findViewById(R.id.categoryitem_text);
name.setText(categoryItem.get_category());
return convertView;
}
}