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
|
package de.danoeh.antennapod.fragment.gpodnet;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.danoeh.antennapod.activity.gpoddernet.GpodnetTagActivity;
import de.danoeh.antennapod.gpoddernet.GpodnetService;
import de.danoeh.antennapod.gpoddernet.GpodnetServiceException;
import de.danoeh.antennapod.gpoddernet.model.GpodnetTag;
import java.util.ArrayList;
import java.util.List;
public class TagListFragment extends ListFragment {
private static final String TAG = "TagListFragment";
private static final int COUNT = 50;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setRetainInstance(true);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedTag = (String) getListAdapter().getItem(position);
Intent intent = new Intent(getActivity(), GpodnetTagActivity.class);
intent.putExtra(GpodnetTagActivity.ARG_TAGNAME, selectedTag);
startActivity(intent);
}
});
loadData();
}
private void loadData() {
AsyncTask<Void, Void, List<GpodnetTag>> task = new AsyncTask<Void, Void, List<GpodnetTag>>() {
private Exception exception;
@Override
protected List<GpodnetTag> doInBackground(Void... params) {
GpodnetService service = new GpodnetService();
try {
return service.getTopTags(COUNT);
} catch (GpodnetServiceException e) {
e.printStackTrace();
exception = e;
return null;
} finally {
service.shutdown();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
setListShown(false);
}
@Override
protected void onPostExecute(List<GpodnetTag> gpodnetTags) {
super.onPostExecute(gpodnetTags);
final Context context = getActivity();
if (context != null) {
if (gpodnetTags != null) {
List<String> tagNames = new ArrayList<String>();
for (GpodnetTag tag : gpodnetTags) {
tagNames.add(tag.getName());
}
setListAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, tagNames));
} else if (exception != null) {
TextView txtvError = new TextView(getActivity());
txtvError.setText(exception.getMessage());
getListView().setEmptyView(txtvError);
}
setListShown(true);
}
}
};
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
task.execute();
}
}
}
|