-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeedAdapter.java
More file actions
93 lines (70 loc) · 3.37 KB
/
Copy pathFeedAdapter.java
File metadata and controls
93 lines (70 loc) · 3.37 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.example.xmlreader;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class FeedAdapter extends ArrayAdapter {
private static final String TAG = "FeedAdapter";
//we need to get layout inflater and layout resource from the constructor
//final objects should either be initialized when declared or inside the constructor
private final LayoutInflater layoutInflater;
private final int layoutResource;
private List<FeedEntry> applications;
public FeedAdapter(@NonNull Context context, int resource, List<FeedEntry> applications) {
//context : for getting layout inflater
//resource : for getting id of the resource file, here list_record.xml
super(context, resource);
this.layoutInflater=LayoutInflater.from(context);
layoutResource=resource;
this.applications=applications;
}
//We must override getCount() and getView() method
//getCount() method tells ListView about the total number of items in the listView so it can position the scroll bar
//getView() methods is called when listView asks the adapter to provide more View objects to display them
//Note that we do not call these methods, they are called by the listView widget when required
//For scroll bar
@Override
public int getCount() {
return applications.size();
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
/*
View view=layoutInflater.inflate(layoutResource, parent, false);
TextView tvName=view.findViewById(R.id.tvName);
TextView tvArtist=view.findViewById(R.id.tvArtist);
TextView tvSummary=view.findViewById(R.id.tvSummary);
FeedEntry currentApp=applications.get(position);
tvName.setText(currentApp.getName());
tvArtist.setText(currentApp.getArtist());
tvSummary.setText(currentApp.getSummary());
Log.d(TAG, "getView: " + currentApp.getName());
return view;
*/
//When we keep scrolling down in listView, it recycles the views that were already displayed
//These views are made available to us to reuse in the "convertView" argument
//ConvertView is null if there is no view that has been recycled
//If we are using the convertView, which means we are getting a view that was inflated before and we
//do not need to inflate again. This saves memory and makes app more responsive.
if(convertView == null){
convertView = layoutInflater.inflate(layoutResource,parent,false);
}
TextView tvName=convertView.findViewById(R.id.tvName);
TextView tvArtist=convertView.findViewById(R.id.tvArtist);
TextView tvSummary=convertView.findViewById(R.id.tvSummary);
FeedEntry currentApp = applications.get(position);
tvName.setText(currentApp.getName());
tvArtist.setText(currentApp.getArtist());
tvSummary.setText(currentApp.getSummary());
//Log.d(TAG, "getView: tvName : " + currentApp.getName());
return convertView;
}
}