Skip to content

Instantly share code, notes, and snippets.

@vibin
Created June 15, 2014 10:28
Show Gist options
  • Save vibin/35255dbc0d2447162680 to your computer and use it in GitHub Desktop.
Save vibin/35255dbc0d2447162680 to your computer and use it in GitHub Desktop.
Volatile
public class Fragment1 extends Fragment {
volatile ArrayList<FetchTask.BillyData> mData;
String[] billySong,result;
ListView lv;
View v;
MyCustomAdapter myCustomAdapter;
RequestQueue req;
BillyApplication billyapp;
FetchTask ft;
String uri, searchparam;
String rssurl = getResources().getStringArray(R.array.url)[0];
int mIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData = new ArrayList<FetchTask.BillyData>(24);
while (mData.size() < 24) {
mData.add(new FetchTask.BillyData());
}
billySong = new String[24];
billyapp= (BillyApplication) getActivity().getApplication();
req = billyapp.getRequestQueue();
ft = new FetchTask();
// Get Billboard XML
StringRequest stringreq = new StringRequest(Request.Method.GET, rssurl, billyComplete(), billyError());
// Cache for images
//imgload = new ImageLoader(req, ImageCacheManager.getInstance().getImageLoader());
req.add(stringreq);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(getClass().getName(), "Size of mdata in fragment1 "+mData.size());
v = inflater.inflate(R.layout.fragment_1, container, false);
lv = (ListView) v.findViewById(R.id.listView);
myCustomAdapter = new MyCustomAdapter(getActivity());
lv.setAdapter(myCustomAdapter);
mIndex = 0;
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
// Listeners for Billboard request
private Response.Listener<String> billyComplete() {
return new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
handleXML(response);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
};
}
private Response.ErrorListener billyError() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d(getClass().getName(), volleyError.toString());
}
};
}
/**
* Parses XML, populates billySong, spawns requests to iTunes
* @param response A String containing XML
* @throws IOException
* @throws XmlPullParserException
*/
private void handleXML(String response) throws IOException, XmlPullParserException {
billySong = ft.parseBillboard(response);
while (mIndex < billySong.length) {
searchparam = billySong[mIndex].replaceAll(" ", "+"); // Put Billboard song as iTunes search parameter
uri = "http://itunes.apple.com/search?term=" + searchparam + "&limit=1";
JsonObjectRequest jsonreq = new JsonObjectRequest(Request.Method.GET, uri, null, itunesComplete(), itunesError());
req.add(jsonreq);
mIndex++;
}
Log.d(getClass().getName(), "itunes requests complete!");
}
/**
* Parses JSONObject, populates {@code mData}, notifies the BaseAdapter
*/
private Response.Listener<JSONObject> itunesComplete() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
result = ft.parseItunes(jsonObject);
Log.d(getClass().getName(), "Match is" +Integer.parseInt(result[4]));
mData.get(Integer.parseInt(result[4])).setItunes(result[1], result[0], result[2], result[3]);
myCustomAdapter.notifyDataSetChanged();
} catch (JSONException e) {
Log.d(getClass().getName(), e + "mIndex is" + mIndex);
}
}
};
}
private Response.ErrorListener itunesError() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d(getClass().getName(), volleyError.toString());
}
};
}
}
class MyCustomAdapter extends BaseAdapter {
Context c;
ArrayList<FetchTask.BillyData> mData;
MyCustomAdapter(Context c) {
this.c = c;
Log.d(getClass().getName(), "This is the adapter constructor!");
Log.d(getClass().getName(), "size of mData in Adapter"+mData.size());
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int i) {
return mData.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
class MyViewHolder {
TextView album, artist, song;
NetworkImageView artwork;
MyViewHolder(View row) {
album = (TextView) row.findViewById(R.id.album);
artist = (TextView) row.findViewById(R.id.artist);
song = (TextView) row.findViewById(R.id.song);
song.setMaxLines(2);
album.setMaxLines(1);
song.setEllipsize(TextUtils.TruncateAt.END);
album.setEllipsize(TextUtils.TruncateAt.END);
artwork = (NetworkImageView) row.findViewById(R.id.artwork);
}
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View row = convertView;
MyViewHolder holder;
if (row == null) {
LayoutInflater lif = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = lif.inflate(R.layout.single_row, viewGroup, false);
holder = new MyViewHolder(row);
row.setTag(holder);
} else {
holder = (MyViewHolder) row.getTag();
}
FetchTask.BillyData temp = mData.get(i);
//Log.d(getClass().getName(), i + " " + temp.album + " " + temp.artist + " " + temp.song + " " + temp.artwork);
// Set everything
holder.album.setText(temp.album);
holder.artist.setText(temp.artist);
holder.song.setText(temp.song);
holder.artwork.setImageUrl(temp.artwork, ImageCacheManager.getInstance().getImageLoader());
return row;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment