Skip to content

Instantly share code, notes, and snippets.

@thedumbtechguy
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedumbtechguy/096b74139f01950c91c4 to your computer and use it in GitHub Desktop.
Save thedumbtechguy/096b74139f01950c91c4 to your computer and use it in GitHub Desktop.
Android ListView Adapter To Display Different Views
package com.whisppa.app.adapter;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.crashlytics.android.Crashlytics;
import com.whisppa.app.BaseActivity;
import com.whisppa.app.R;
import com.whisppa.app.View.PlayableSongListItem;
import com.whisppa.app.View.SongViewItem;
import com.whisppa.app.View.TwoLinedListItem;
import com.whisppa.app.model.Remote.SongModel;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
/**
* Created by frosty on 9/2/2014.
*/
public class SearchAdapter extends com.nhaarman.listviewanimations.ArrayAdapter<SearchAdapter.SearchItem> {
private final BaseActivity mContext;
public SearchAdapter(Activity context, ArrayList<SearchItem> values) {
super(values);
this.mContext = (BaseActivity) context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SearchItem item = get(position);
if (convertView == null || !convertView.getTag(R.string.title_activity_search).equals(item.Type)) {
switch (item.Type) {
case HEADER:
convertView = new TextView(mContext);
break;
case SONG:
convertView = new PlayableSongListItem(mContext, null);
break;
case ARTIST:
case ALBUM:
if(!(convertView instanceof TwoLinedListItem))
convertView = new TwoLinedListItem(mContext, null);
break;
}
}
switch (item.Type) {
case HEADER:
((TextView) convertView).setText((String) item.Data);
break;
case SONG:
try {
((SongViewItem) convertView).setSong(new SongModel((JSONObject) item.Data));
} catch (JSONException e) {
Crashlytics.logException(e);
}
break;
case ARTIST:
JSONObject artistData = (JSONObject) item.Data;
try {
((TwoLinedListItem) convertView).setData(artistData.getString("UID"), artistData.getString("name"), artistData.getString("record_label"), artistData.getString("album_art"));
} catch (JSONException e) {
Crashlytics.logException(e);
}
break;
case ALBUM:
JSONObject albumData = (JSONObject) item.Data;
try {
((TwoLinedListItem) convertView).setData(albumData.getString("UID"), albumData.getString("name"), albumData.getString("artist_name"), albumData.getString("album_art"));
} catch (JSONException e) {
Crashlytics.logException(e);
}
break;
}
convertView.setTag(R.string.title_activity_search, item.Type);
return convertView;
}
public static class SearchItem {
public final SearchItemType Type;
public final Object Data;
public SearchItem(SearchItemType type, Object data) {
Type = type;
Data = data;
}
}
public enum SearchItemType {
HEADER,
SONG,
ALBUM,
ARTIST;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment