public class Fragment1 extends ListFragment {
private static final String ALBUM_LIST_URL ="http://localhost:8080/seoul2011.xml";
private static final String TAG = "AlbumsActivity";
private ListView listView;
private Vector<Album> albums = new Vector<Album>();
private Handler handler;
private ProgressBar loadingProgress;
private Hashtable<String, Bitmap> bitmapCache = new Hashtable<String, Bitmap>();
private Bitmap brokenImage = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(getListAdapter());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater= getActivity().getLayoutInflater();
View row=inflater.inflate(R.layout.activity_albums, container, false);
handler = new Handler();
listView.setAdapter(adapter);
setListAdapter(adapter);
loadingProgress = (ProgressBar)row.findViewById(R.id.loadingProgress);
new Thread(new Runnable() {
@Override
public void run() {
downloadXml();
}
}).start();
return row;
}
protected void reloadAlbumsList() {
// listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
loadingProgress.setVisibility(View.INVISIBLE);
}
public class AlbumView extends LinearLayout {
private TextView albumTitle;
private TextView artistName;
private ImageView albumImage;
private ProgressBar progressBar;
AlbumView(Context context) {
super(context);
LayoutInflater inflator = getActivity().getLayoutInflater();
inflator.inflate(R.layout.album_listitem, this, true);
albumTitle = (TextView)findViewById(R.id.albumTitle);
artistName = (TextView)findViewById(R.id.artistName);
albumImage = (ImageView)findViewById(R.id.albumImage);
progressBar = (ProgressBar)findViewById(R.id.imageProgress);
}
public void setAlbum(final Album album, final int position) {
albumTitle.setText(album.albumTitle);
artistName.setText(album.artistName);
albumImage.setImageResource(R.drawable.ic_launcher);
Bitmap bitmap = bitmapCache.get(album.imageUrl);
if (bitmap != null) {
albumImage.setImageBitmap(bitmap);
return;
}
progressBar.setVisibility(View.VISIBLE);
new Thread(new Runnable() {
@Override
public void run() {
try {
final String imageUrl = album.imageUrl;
URL url = new URL("imageUrl);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(is);
handler.post(new Runnable() {
@Override
public void run() {
bitmapCache.put(imageUrl, bitmap);
updateImage(position, bitmap);
progressBar.setVisibility(View.INVISIBLE);
}
});
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
Bitmap bitmap = getBrokenImage();
updateImage(position, bitmap);
progressBar.setVisibility(View.INVISIBLE);
}
});
}
}
}).start();
}
}
private Bitmap getBrokenImage() {
if (this.brokenImage != null) {
return this.brokenImage;
}
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.brokenchain);
this.brokenImage = bitmap;
return bitmap;
}
private void updateImage(int position, Bitmap bitmap) {
int first = listView.getFirstVisiblePosition();
if (position < first) {
return;
}
int last = listView.getLastVisiblePosition();
if (position > last) {
return;
}
AlbumView albumView = (AlbumView) listView.getChildAt(position - first);
albumView.albumImage.setImageBitmap(bitmap);
}
private BaseAdapter adapter = new BaseAdapter() {
Context mContext;
@Override
public int getCount() {
return albums.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Album album = albums.get(position);
AlbumView albumView = null;
if (convertView != null) {
albumView = (AlbumView)convertView;
} else {
albumView = new AlbumView(mContext);
}
albumView.setAlbum(album, position);
return albumView;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
};
private void downloadXml() {
try {
URL url = new URL("ALBUM_LIST_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
// conn.addRequestProperty(field, newValue)
int resCode = conn.getResponseCode();
if (resCode != HttpURLConnection.HTTP_OK) {
Log.d(TAG, "Response Code = " + resCode);
return;
}
InputStream is = conn.getInputStream();
XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
XmlPullParser xpp = xppf.newPullParser();
xpp.setInput(is, "utf-8");
int event = xpp.getEventType();
albums.removeAllElements();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_TAG:
if ("RECORD".equals(xpp.getName())) {
Album album = new Album(xpp);
albums.add(album);
}
}
event = xpp.next();
//Log.d(TAG, "XML Event:" + event + " Name: " + xpp.getName());
}
Log.v(TAG, "Parsed " + albums.size() + " albums");
handler.post(new Runnable() {
@Override
public void run() {
reloadAlbumsList();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public class Album {
public String artistName;
public String albumTitle;
public String imageUrl;
Album(XmlPullParser xpp) {
try {
while (true) {
int event = xpp.next();
if (event == XmlPullParser.START_TAG) {
String tag = xpp.getName();
if ("kind".equals(tag)) {
xpp.next();
this.artistName = xpp.getText();
} else if ("storeName".equals(tag)) {
xpp.next();
this.albumTitle = xpp.getText();
} else if ("imgUrlOne".equals(tag)) {
xpp.next();
this.imageUrl = xpp.getText();
}
} else if (event == XmlPullParser.END_TAG) {
String tag = xpp.getName();
if ("RECORD".equals(tag)) {
break;
}
}
}
} catch (Exception e) {
}
}
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="8dp" android:paddingRight="8dp"> <ListView android:id="@+id/android:list" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:drawSelectorOnTop="false"/> <TextView android:id="@id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:text="No data"/> </LinearLayout> 12-03 14:22:56.398: W/dalvikvm(24224): threadid=1: thread exiting with uncaught exception (group=0x40c101f8) 12-03 14:22:56.398: E/AndroidRuntime(24224): FATAL EXCEPTION: main 12-03 14:22:56.398: E/AndroidRuntime(24224): java.lang.NullPointerException 12-03 14:22:56.398: E/AndroidRuntime(24224): at com.example.plz.Fragment1.reloadAlbumsList(Fragment1.java:67) 12-03 14:22:56.398: E/AndroidRuntime(24224): at com.example.plz.Fragment1$3.run(Fragment1.java:215) 12-03 14:22:56.398: E/AndroidRuntime(24224): at android.os.Handler.handleCallback(Handler.java:605) 12-03 14:22:56.398: E/AndroidRuntime(24224): at android.os.Handler.dispatchMessage(Handler.java:92) 12-03 14:22:56.398: E/AndroidRuntime(24224): at android.os.Looper.loop(Looper.java:137) 12-03 14:22:56.398: E/AndroidRuntime(24224): at android.app.ActivityThread.main(ActivityThread.java:4512) 12-03 14:22:56.398: E/AndroidRuntime(24224): at java.lang.reflect.Method.invokeNative(Native Method) 12-03 14:22:56.398: E/AndroidRuntime(24224): at java.lang.reflect.Method.invoke(Method.java:511) 12-03 14:22:56.398: E/AndroidRuntime(24224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) |