forked from AndroidFromFrankfurt/linuxnews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewsAdapter.java
75 lines (63 loc) · 2.42 KB
/
NewsAdapter.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
package org.androidfromfrankfurt.archnews;
import java.net.URL;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import at.theengine.android.simple_rss2_android.RSSItem;
class NewsAdapter extends ArrayAdapter<RSSItem> {
private ArrayList<RSSItem> items;
private Context ctx;
private int layout;
public NewsAdapter(Context context, int layout, ArrayList<RSSItem> items) {
super(context, layout, items);
this.items = items;
this.ctx = context;
this.layout = layout;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(layout, null);
}
RSSItem o = items.get(position);
if (o != null) {
TextView tvTitle = ((TextView)v.findViewById(R.id.tv_title));
TextView tvPubDate = ((TextView)v.findViewById(R.id.tv_pubdate));
WebView tvDescription = ((WebView)v.findViewById(R.id.wv_description));
if (tvTitle != null) {
tvTitle.setText(o.getTitle());
final URL url = o.getLink();
tvTitle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ctx.startActivity(new Intent(Intent.ACTION_VIEW).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setData(Uri.parse(url.toString())));
}
});
}
if (tvPubDate != null) {
try {
// too stupid to turn the RSS date into a local date - just delete some chars and it'll be fine ;)
tvPubDate.setText(o.getDate().substring(5, o.getDate().length()-15));
}
catch(Exception e) {
tvPubDate.setText("");
}
}
if (tvDescription != null) {
tvDescription.loadDataWithBaseURL(null, o.getDescription(), "text/html", "UTF-8", null);
tvDescription.setBackgroundColor(0x00000000);
}
}
return v;
}
}