Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / NewsListAdapter.java
1 /* NewsListAdapter.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp.fragments;
17 import android.content.Context;
18 import android.view.LayoutInflater;
19 import android.view.View;
20 import android.view.ViewGroup;
21 import android.widget.ArrayAdapter;
22 import android.widget.ImageView;
23 import android.widget.TextView;
24
25 import java.util.ArrayList;
26
27 import de.dhbwloe.campusapp.R;
28
29 /**
30  * Created by pk910 on 27.02.2016.
31  */
32 public class NewsListAdapter  extends ArrayAdapter<NewsListItem> {
33     private Context context;
34     private int layoutResourceId;
35     private ArrayList<NewsListItem> data = new ArrayList<NewsListItem>();
36
37     public NewsListAdapter(Context context, int layoutResourceId, ArrayList<NewsListItem> data) {
38         super(context, layoutResourceId, data);
39         this.layoutResourceId = layoutResourceId;
40         this.context = context;
41         this.data = data;
42     }
43
44     @Override
45     public View getView(int position, View convertView, ViewGroup parent) {
46         View row = convertView;
47         RecordHolder holder = null;
48
49         if (row == null) {
50             LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
51             row = inflater.inflate(layoutResourceId, parent, false);
52
53             holder = new RecordHolder(row);
54             row.setTag(holder);
55         } else {
56             holder = (RecordHolder) row.getTag();
57         }
58
59         final NewsListAdapter that = this;
60         final NewsListItem item = data.get(position);
61
62         holder.newsTitle.setText(item.getFormatedDate() + ": " + item.getTitle());
63         holder.newsDescription.setText(item.getDescription());
64         if(item.isDhbwNews()) {
65             holder.newsDhbwLogo.setVisibility(View.VISIBLE);
66             holder.newsStuvLogo.setVisibility(View.GONE);
67         } else {
68             holder.newsDhbwLogo.setVisibility(View.GONE);
69             holder.newsStuvLogo.setVisibility(View.VISIBLE);
70         }
71
72         return row;
73     }
74
75     static class RecordHolder {
76         TextView newsTitle;
77         TextView newsDescription;
78         ImageView newsDhbwLogo;
79         ImageView newsStuvLogo;
80
81         public RecordHolder(View view) {
82             this.newsTitle = (TextView) view.findViewById(R.id.newsTitleView);
83             this.newsDescription = (TextView) view.findViewById(R.id.newsDescriptionView);
84
85             newsDhbwLogo = (ImageView) view.findViewById(R.id.dhbwLogo);
86             newsStuvLogo = (ImageView) view.findViewById(R.id.stuvLogo);
87         }
88     }
89 }