This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
/
StatusAdapter.java
104 lines (78 loc) · 2.64 KB
/
StatusAdapter.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.fredbrunel.android.twitter;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;
import jtwitter.TwitterEntry;
import jtwitter.TwitterResponse;
public class StatusAdapter implements ListAdapter {
private final TwitterResponse statuses;
private final Map<Integer,View> views = new HashMap<Integer,View>();
public StatusAdapter(Context context, TwitterResponse statuses) {
this.statuses = statuses;
for (int i = 0; i < statuses.getNumberOfItems(); i++)
views.put(i, makeUserStatusView(context, statuses.getItemAt(i)));
}
public boolean areAllItemsEnabled() {
return true;
}
public boolean isEnabled(int position) {
return true;
}
public boolean hasStableIds() {
return true;
}
public int getItemViewType(int position) {
return 0;
}
public int getViewTypeCount() {
return 1;
}
public boolean isEmpty() {
return getCount() == 0;
}
public int getCount() {
return statuses.getNumberOfItems();
}
public Object getItem(int position) {
return statuses.getItemAt(position);
}
public long getItemId(int position) {
return statuses.getItemAt(position).getId();
}
public View getView(int position, View convertView, ViewGroup parent) {
return views.get(position);
}
public void registerDataSetObserver(DataSetObserver observer) {
}
public void unregisterDataSetObserver(DataSetObserver observer) {
}
// Create the view for each user status
// [FIXME] Should be put as an XML file (possible?)
private View makeUserStatusView(Context context, TwitterEntry entry) {
ImageView iv = new ImageView(context);
Bitmap photo = BitmapCache.getInstance().get(entry.getUser().getProfileImageURL());
iv.setImageBitmap(photo);
iv.setScaleType(ScaleType.CENTER);
iv.setPadding(0, 4, 4, 0);
iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(context);
tv.setText(entry.getUser().getName() + ": " + entry.getText());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setPadding(4, 2, 4, 2);
layout.addView(iv);
layout.addView(tv);
return layout;
}
}