ListView displaying nothing with custom adapter - android

As the title states, I'm having some troubles getting my custom listview to work properly. The app displays nothing on the list, and gives just a blank white screen. I tested my data with a simple list I already have setup, and that worked just fine. I'm hoping someone can see something I haven't. Thanks.
History.java
public class History {
public String score;
public String gametype;
public int icon;
public History() {
super();
}
public History(String score, String gametype, int icon) {
super();
this.score = score;
this.gametype = gametype;
this.icon = icon;
}
}
HistoryAdapter.java
public class HistoryAdapter extends ArrayAdapter<History> {
Context context;
int layoutResId;
History data[] = null;
public HistoryAdapter(Context context, int layoutResId, History[] data) {
super(context, layoutResId, data);
this.layoutResId = layoutResId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HistoryHolder holder = null;
if(convertView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResId, parent, false);
holder = new HistoryHolder();
holder.imageIcon = (ImageView)convertView.findViewById(R.id.icon);
holder.textTitle = (TextView)convertView.findViewById(R.id.gameType);
holder.textScore = (TextView)convertView.findViewById(R.id.score);
convertView.setTag(holder);
}
else
{
holder = (HistoryHolder)convertView.getTag();
}
History history = data[position];
holder.textScore.setText(history.score);
holder.textTitle.setText(history.gametype);
holder.imageIcon.setImageResource(history.icon);
return convertView;
}
static class HistoryHolder
{
ImageView imageIcon;
TextView textTitle;
TextView textScore;
}
}
Implementation
History[] historyData = new History[games.length()];
for(int i = 0; i < games.length(); i++) {
JSONObject c = games.getJSONObject(i);
JSONObject gameStats = games.getJSONObject(i).getJSONObject(TAG_STATS);
type[i] = c.getString(TAG_TYPE);
champId[i] = c.getString("championId");
cs[i] = gameStats.getString("minionsKilled");
kills[i] = gameStats.getString("championsKilled");
deaths[i] = gameStats.getString("numDeaths");
assists[i] = gameStats.getString("assists");
win[i] = gameStats.getString("win");
if(win[i].equals("true"))
win[i] = "Victory";
else
win[i] = "Defeat";
if(type[i].equals("RANKED_SOLO_5x5"))
type[i] = "Ranked (Solo)";
if(type[i].equals("CAP_5x5"))
type[i] = "TeamBuilder";
if(type[i].equals("NORMAL"))
type[i] = "Unranked";
score[i] = kills[i] +"/" + deaths[i] + "/" + assists[i];
historyData[i] = new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image
}
if(historyData == null) {
historyData[0] = new History("No game found", "N/A", R.drawable.ic_launcher);
Log.i("Data", "" + historyData);
}
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,
historyData);
list.setAdapter(adapter);
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:background="#111111">
</ListView>
list_item.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#111111"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/score"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="0/0/0 KDA"
android:textSize="12sp" />
<TextView
android:id="#+id/gameType"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/score"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>

The code you've included seems fine, so the only point where it can fail is in the loop where you create the historyData array.
For some reason you might not be processing it correctly, for instance, some JSON attributes not being defined sometimes.
You can use a try block and catch a JSONException to see what's wrong, and also add several Log.d() sentences to know where's the culprit.
For instance:
try {
cs[i] = gameStats.getString("minionsKilled");
}
catch (JSONException) { e.printStackTrace(); }

I guess you are using wrong XML
Change
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,historyData);
to
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_item,historyData);
and make sure that your historyData is not null

Related

Listview adapter doesnt show textview

I got a ListView Adapter. In this adapter , i fill it with some data .My problem is that it shows my date only after i scroll down and don't understand what's the problem , any ideas??
public class EventsAdapter extends ArrayAdapter<Article> {
EventsAdapter adapter = this;
Context context;
int layoutResourceId;
ArrayList<Article> cartItems = new ArrayList<Article>();
Date time;
public EventsAdapter(Context context, int layoutResourceId,
ArrayList<Article> galleries) {
super(context, layoutResourceId, galleries);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.cartItems = galleries;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Article eventItem = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.event_item_row, parent, false);
}
Typeface cFont2 = Typeface.createFromAsset(context.getAssets(), "fonts/berthold_baskerville_bold-webfont.ttf");
final RecordHolder holder = new RecordHolder();
holder.eventImage = (ImageView) convertView.findViewById(R.id.event_image);
holder.eventTitle = (TextView) convertView.findViewById(R.id.event_title);
holder.eventTitleDescription = (TextView) convertView.findViewById(R.id.event_title_description);
holder.eventCountries = (TextView) convertView.findViewById(R.id.event_countries);
holder.eventRegions = (TextView) convertView.findViewById(R.id.event_regions);
holder.eventCategory = (TextView) convertView.findViewById(R.id.event_category);
holder.eventType = (TextView) convertView.findViewById(R.id.event_type);
holder.eventDate = (TextView) convertView.findViewById(R.id.event_date);
holder.salary = (TextView) convertView.findViewById(R.id.job_salary);
holder.eventTitle.setTypeface(cFont2);
holder.salary.setVisibility(View.GONE);
holder.eventImage.setVisibility(View.GONE);
if (!eventItem.getImageURL().equals("")) {
holder.eventImage.setVisibility(View.VISIBLE);
Picasso.with(context)
.load(eventItem.getImageURL())
.resize(250, 175)
.into(holder.eventImage);
}
holder.eventTitle.setText(eventItem.getName());
if (eventItem.getCountry() == null) {
holder.eventCountries.setText(context.getString(R.string.all_countries));
} else {
holder.eventCountries.setText(eventItem.getCountry().getName());
}
if (eventItem.getRegion() == null) {
holder.eventRegions.setText(context.getString(R.string.all_regions));
} else {
holder.eventRegions.setText(eventItem.getRegion().getName());
}
boolean startDate = false;
boolean endDate = false;
String endDateString = "";
String startDateString = "";
for (int i = 0; i < eventItem.getExtraFields().size(); i++) {
if (eventItem.getExtraFields().get(i).getName().equals("EVENTENDDATE") && !eventItem.getExtraFields().get(i).getValue().getValue().equals("")) {
endDate = true;
endDateString = new SimpleDateFormat("dd/MM/yyyy").format(getDate(eventItem.getExtraFields().get(i).getValue().getValue()));
}
}
for (int i = 0; i < eventItem.getExtraFields().size(); i++) {
if (eventItem.getExtraFields().get(i).getName().equals("EVENTSTARTDATE") && !eventItem.getExtraFields().get(i).getValue().getValue().equals("")) {
startDate = true;
startDateString = new SimpleDateFormat("dd/MM/yyyy").format(getDate(eventItem.getExtraFields().get(i).getValue().getValue()));
}
}
if (startDate && endDate) {
holder.eventDate.setText(startDateString + " - " + endDateString);
holder.eventDate.setVisibility(View.VISIBLE);
} else if (startDate) {
holder.eventDate.setText(startDateString);
holder.eventDate.setVisibility(View.VISIBLE);
} else {
holder.eventDate.setVisibility(View.VISIBLE);
}
for (int i = 0; i < eventItem.getExtraFields().size(); i++) {
if (eventItem.getExtraFields().get(i).getName().equals("EVENTORGANISER")) {
holder.eventTitleDescription.setText(eventItem.getExtraFields().get(i).getValue().getValue());
} else if (eventItem.getExtraFields().get(i).getName().equals("EVENTTYPE")) {
holder.eventType.setVisibility(View.VISIBLE);
holder.eventType.setText(eventItem.getExtraFields().get(i).getValue().getValue());
}
}
holder.eventCategory.setText(eventItem.getCategories().get(0).getName());
return convertView;
}
private Date getDate(String date) {
String json = date;
String timeString = json.substring(json.indexOf("(") + 1, json.indexOf(")"));
String[] timeSegments = timeString.split("\\+");
// May have to handle negative timezones
int timeZoneOffSet = Integer.valueOf(timeSegments[1]) * 36000; // (("0100" / 100) * 3600 * 1000)
long millis = Long.valueOf(timeSegments[0]);
time = new Date(millis + timeZoneOffSet);
return time;
}
static class RecordHolder {
TextView salary;
ImageView eventImage;
TextView eventTitle;
TextView eventTitleDescription;
TextView eventCountries;
TextView eventRegions;
TextView eventCategory;
TextView eventType;
TextView eventDate;
}
}
Have i done something wrong or it is an android visual bug ??
UPDATED:
event_item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ebf5fb"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:visibility="gone"
android:id="#+id/event_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
<TextView
android:id="#+id/event_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#043c5b"
android:textStyle="bold"
android:singleLine="true"
android:textSize="17sp"
android:paddingBottom="10dp"/>
<TextView
android:id="#+id/event_title_description"
android:layout_width="wrap_content"
android:singleLine="true"
android:layout_height="wrap_content"
android:textColor="#7c7f7e"/>
<TextView
android:id="#+id/event_countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7c7f7e"/>
<TextView
android:id="#+id/event_regions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7c7f7e"/>
<TextView
android:id="#+id/event_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7c7f7e"/>
<TextView
android:visibility="gone"
android:id="#+id/event_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7c7f7e"/>
<TextView
android:visibility="gone"
android:id="#+id/job_salary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7c7f7e"
android:text="#string/salary"/>
<TextView
android:visibility="gone"
android:id="#+id/event_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7c7f7e"/>
</LinearLayout>
Got it ,Please update your null checking .
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
convertView = null;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.from(activity).inflate(R.layout.community_common_tab_layout, null);
holder.DashBoard_Tab_List_Root=(LinearLayout)convertView.findViewById(R.id.Community_Common_Root);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText( dataArray.get(position).countryName);
holder.language.setText( dataArray.get(position).language);
holder.Capital.setText( dataArray.get(position).capital);
return convertView;
}
public static class ViewHolder {
public LinearLayout DashBoard_Tab_List_Root;
public TextView Tv_Badge_View;
}
}

how to show delivered messge in chatadapter

This is my Mainactivity class Code using this code am able to display message sent and failure.
#Override
public void executionResult(int result) {
// TODO Auto-generated method stub
final int delivered=result;
runOnUiThread(new Runnable() {
public void run() {
if(delivered==1)
{
Toast.makeText(getApplicationContext(), "sent", 1000).show();
}
else
{
Toast.makeText(getApplicationContext(), "failure", 1000).show();
}
}
});
}
While I have to display it in adapter I have created field for delivered in text view, but unable to display in adapter when I sent message successfully. Below is code for adapter and xml for that adapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/even_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<com.lociiapp.utils.RoundedImageView
android:id="#+id/odd_bubble"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_margin="5dip"
android:src="#drawable/index"
android:visibility="visible" />
<com.lociiapp.utils.RoundedImageView
android:id="#+id/user_img"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"
android:layout_margin="5dip"
android:src="#drawable/index"
android:visibility="visible" />
<LinearLayout
android:id="#+id/shareRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_toLeftOf="#+id/user_img"
android:layout_toRightOf="#+id/odd_bubble"
android:background="#drawable/rectangle"
android:gravity="right"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="30dp"
android:textColor="#636363"
android:textSize="20sp" />
<TextView
android:id="#+id/delivered"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="#+id/leftimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/shareRow"
android:layout_alignTop="#+id/user_img"
android:layout_marginRight="-11.5dip"
android:src="#drawable/callout_right" />
<ImageView
android:id="#+id/ rightangale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/odd_bubble"
android:layout_marginLeft="19dip"
android:layout_toRightOf="#+id/odd_bubble"
android:src="#drawable/callout_left" />
</RelativeLayout>
delivered textview is the id of adepter where I have to display sent and failure message if 1 is got:
public class ChatAdapter extends ArrayAdapter<Chat> {
private final Context context;
private final ArrayList<Chat> values;
ImageLoader imageloader;
Datamodel dm;
public ChatAdapter(Context context, ArrayList<Chat> values) {
super(context, R.layout.list_row_layout_even, values);
this.context = context;
this.values = values;
imageloader = new ImageLoader(context);
}
public void addMessage(Chat chat) {
values.add(chat);
this.notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row_layout_odd,
parent, false);
RelativeLayout root = (RelativeLayout) convertView
.findViewById(R.id.even_container);
TextView tv = (TextView) convertView.findViewById(R.id.text);
TextView delivred = (TextView) convertView.findViewById(R.id.delivered);
RoundedImageView userImg = (RoundedImageView) convertView
.findViewById(R.id.user_img);
RoundedImageView oddImg = (RoundedImageView) convertView
.findViewById(R.id.odd_bubble);
ImageView leftimage = (ImageView) convertView
.findViewById(R.id.leftimage);
ImageView rightimage = (ImageView) convertView
.findViewById(R.id.rightangale);
Typeface fontArial = Typeface.createFromAsset(context.getAssets(),
"fonts/ARIAL.TTF");
SharedPreferences prefs = context.getSharedPreferences(
AppConstants.LOGIN_PREFS, Context.MODE_PRIVATE);
String K = prefs.getString("Member_id", "");
Chat chat = values.get(position);
String t = prefs.getString("PREFS_NAME_reccvierChatadpter", "");
tv.setText(chat.getMessage());
tv.setTypeface(fontArial);
AQuery aq = new AQuery(context);
if (chat.getSenderID().equals(prefs.getString("Member_id", ""))) {
root.setBackgroundColor(Color.parseColor("#ffffff"));
tv.setTextColor(Color.parseColor("#636363"));
userImg.setVisibility(View.VISIBLE);
rightimage.setVisibility(View.GONE);
leftimage.setVisibility(View.VISIBLE);
aq.id(userImg).image(
"http://api.lociiapp.com/TransientStorage/" + K + ".jpg");
oddImg.setVisibility(View.GONE);
} else {
root.setBackgroundColor(Color.parseColor("#f5f6f1"));
tv.setTextColor(Color.parseColor("#bdbdbd"));
aq.id(oddImg).image(
"http://api.lociiapp.com/TransientStorage/" + t + ".jpg");
userImg.setVisibility(View.GONE);
oddImg.setVisibility(View.VISIBLE);
leftimage.setVisibility(View.GONE);
rightimage.setVisibility(View.VISIBLE);
}
return convertView;
}
}
Here is the code of adapter. Please help and suggest me where we have to change I am not able to fix this issue.
Try to use Handler instead of runOnUiThread. Have you Tested that you are getting the Result in "executionResult(int result)"?

Nothing showing on screen with custom listview adapter

As the title states, I'm having some troubles getting my custom listview adapter to work properly. The app displays nothing on the list, and gives just a blank white screen. I tested my data with a simple list I already have setup, and that worked just fine.
History.java
public class History {
public String score;
public String gametype;
public int icon;
public History() {
super();
}
public History(String score, String gametype, int icon) {
super();
this.score = score;
this.gametype = gametype;
this.icon = icon;
}
}
HistoryAdapter.java
public class HistoryAdapter extends ArrayAdapter<History> {
Context context;
int layoutResId;
History data[] = null;
public HistoryAdapter(Context context, int layoutResId, History[] data) {
super(context, layoutResId, data);
this.layoutResId = layoutResId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HistoryHolder holder = null;
if(convertView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResId, parent, false);
holder = new HistoryHolder();
holder.imageIcon = (ImageView)convertView.findViewById(R.id.icon);
holder.textTitle = (TextView)convertView.findViewById(R.id.gameType);
holder.textScore = (TextView)convertView.findViewById(R.id.score);
convertView.setTag(holder);
}
else
{
holder = (HistoryHolder)convertView.getTag();
}
History history = data[position];
holder.textScore.setText(history.score);
holder.textTitle.setText(history.gametype);
holder.imageIcon.setImageResource(history.icon);
return convertView;
}
static class HistoryHolder
{
ImageView imageIcon;
TextView textTitle;
TextView textScore;
}
}
Implementation
for(int i = 0; i < games.length(); i++) {
JSONObject c = games.getJSONObject(i);
JSONObject gameStats = games.getJSONObject(i).getJSONObject(TAG_STATS);
type[i] = c.getString(TAG_TYPE);
champId[i] = c.getString("championId");
cs[i] = gameStats.getString("minionsKilled");
kills[i] = gameStats.getString("championsKilled");
deaths[i] = gameStats.getString("numDeaths");
assists[i] = gameStats.getString("assists");
win[i] = gameStats.getString("win");
if(win[i].equals("true"))
win[i] = "Victory";
else
win[i] = "Defeat";
if(type[i].equals("RANKED_SOLO_5x5"))
type[i] = "Ranked (Solo)";
if(type[i].equals("CAP_5x5"))
type[i] = "TeamBuilder";
if(type[i].equals("NORMAL"))
type[i] = "Unranked";
score[i] = kills[i] +"/" + deaths[i] + "/" + assists[i];
historyData[i] = new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image
}
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,
historyData);
list.setAdapter(adapter);
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:background="#111111">
</ListView>
list_item.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#111111"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/score"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="0/0/0 KDA"
android:textSize="12sp" />
<TextView
android:id="#+id/gameType"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/score"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>
I think you should replace ArrayAdapter by BaseAdapter and implement all required method e.g. getItemId(), getItem(), getCount(), getView().
It should work fine!.
Below is example code of mine, dont care about what is MusicModel.
protected class MusicCustomAdapter extends BaseAdapter {
private Activity context;
private List<MusicModel> musicModelList;
private SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
public MusicCustomAdapter(Activity context, List<MusicModel> musicModelList) {
this.context = context;
this.musicModelList = musicModelList;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public Object getItem(int position) {
return this.musicModelList.get(position);
}
#Override
public int getCount() {
return this.musicModelList.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = context.getLayoutInflater().inflate(R.layout.song_item, null);
}
MusicModel musicModel = (MusicModel) getItem(position);
TextView tvMusicName = (TextView) view.findViewById(R.id.tv_song_name);
tvMusicName.setText(musicModel.getName());
TextView tvArtist = (TextView) view.findViewById(R.id.tv_artist);
tvArtist.setText(musicModel.getArtist());
TextView tvDuration = (TextView) view.findViewById(R.id.tv_duration);
tvDuration.setText(sdf.format(musicModel.getDuration()));
ImageView imgThumbnail = (ImageView) view.findViewById(R.id.img_thumbnail);
imgThumbnail.setImageDrawable(musicModel.getThumbnail());
return view;
}
}
Hope this help.
You need to override getCount() in your custom adapter.
It will return the number of entries in your ListView.
#Override
public int getCount() {
return myArrayList.size();
}

Using Picasso library with ListView

What I'm trying to do is adapt my custom ListView adapter to use images fetched from the web by the Picasso library. I believe I have my adapter changed in order to accept an image from Picasso, but I am unsure how to change my implementation to accept it using a ListView. I believe I have to access holder.imageIcon, but I am not sure how to get it up and running. My code is as follows.
History.java
public class History {
public String score;
public String gametype;
public Picasso icon;
public History() {
super();
}
public History(String score, String gametype, Picasso icon) {
super();
this.score = score;
this.gametype = gametype;
this.icon = icon;
}
}
HistoryAdapter.java
public class HistoryAdapter extends ArrayAdapter<History> {
Context context;
int layoutResId;
History data[] = null;
public HistoryAdapter(Context context, int layoutResId, History[] data) {
super(context, layoutResId, data);
this.layoutResId = layoutResId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HistoryHolder holder = null;
if(convertView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResId, parent, false);
holder = new HistoryHolder();
holder.imageIcon = (ImageView)convertView.findViewById(R.id.icon);
holder.textTitle = (TextView)convertView.findViewById(R.id.gameType);
holder.textScore = (TextView)convertView.findViewById(R.id.score);
convertView.setTag(holder);
}
else
{
holder = (HistoryHolder)convertView.getTag();
}
History history = data[position];
holder.textScore.setText(history.score);
holder.textTitle.setText(history.gametype);
holder.imageIcon.setImageResource(history.icon);
return convertView;
}
static class HistoryHolder
{
ImageView imageIcon;
TextView textTitle;
TextView textScore;
}
}
Implementation
History[] historyData = new History[games.length()];
for(int i = 0; i < games.length(); i++) {
JSONObject c = games.getJSONObject(i);
JSONObject gameStats = games.getJSONObject(i).getJSONObject(TAG_STATS);
type[i] = c.getString(TAG_TYPE);
champId[i] = c.getString("championId");
cs[i] = gameStats.getString("minionsKilled");
kills[i] = gameStats.getString("championsKilled");
deaths[i] = gameStats.getString("numDeaths");
assists[i] = gameStats.getString("assists");
win[i] = gameStats.getString("win");
if(win[i].equals("true"))
win[i] = "Victory";
else
win[i] = "Defeat";
if(type[i].equals("RANKED_SOLO_5x5"))
type[i] = "Ranked (Solo)";
if(type[i].equals("CAP_5x5"))
type[i] = "TeamBuilder";
if(type[i].equals("NORMAL"))
type[i] = "Unranked";
score[i] = kills[i] +"/" + deaths[i] + "/" + assists[i];
historyData[i] = new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image
}
if(historyData == null) {
historyData[0] = new History("No game found", "N/A", R.drawable.ic_launcher); // Use Picasso placeholder
Log.i("Data", "" + historyData);
}
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,
historyData);
list.setAdapter(adapter);
list_item.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#111111"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/score"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="0/0/0 KDA"
android:textSize="12sp" />
<TextView
android:id="#+id/gameType"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/score"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>
There are 2 things you need to change:
1) History.icon should be the String url of the icon, not a Picasso object. You can also use a File, Uri, or int, but a String url is probably what you want.
2) Modify your Adapter's getView() method to load the icon using Picasso (see the last line before getView() returns the convertView):
public class HistoryAdapter extends ArrayAdapter<History> {
Context context;
int layoutResId;
History data[] = null;
public HistoryAdapter(Context context, int layoutResId, History[] data) {
super(context, layoutResId, data);
this.layoutResId = layoutResId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HistoryHolder holder = null;
if(convertView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResId, parent, false);
holder = new HistoryHolder();
holder.imageIcon = (ImageView)convertView.findViewById(R.id.icon);
holder.textTitle = (TextView)convertView.findViewById(R.id.gameType);
holder.textScore = (TextView)convertView.findViewById(R.id.score);
convertView.setTag(holder);
}
else
{
holder = (HistoryHolder)convertView.getTag();
}
History history = data[position];
holder.textScore.setText(history.score);
holder.textTitle.setText(history.gametype);
Picasso.with(this.context).load(history.icon).into(holder.imageIcon)
return convertView;
}
static class HistoryHolder
{
ImageView imageIcon;
TextView textTitle;
TextView textScore;
}
}

Having an issue with a custom listview adapter

As the title states, I'm having some troubles getting my custom listView adapter to work. The app doesn't crash, but it also displays nothing on the list. I tested my data with a simple list I already have setup, and that worked just fine.
History.java
public class History {
public String score;
public String gametype;
public int icon;
public History() {
super();
}
public History(String score, String gametype, int icon) {
super();
this.score = score;
this.gametype = gametype;
this.icon = icon;
}
}
HistoryAdapter.java
public class HistoryAdapter extends ArrayAdapter<History> {
Context context;
int layoutResId;
History data[] = null;
public HistoryAdapter(Context context, int layoutResId, History[] data) {
super(context, layoutResId, data);
this.layoutResId = layoutResId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HistoryHolder holder = null;
if(convertView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResId, parent, false);
holder = new HistoryHolder();
holder.imageIcon = (ImageView)convertView.findViewById(R.id.icon);
holder.textTitle = (TextView)convertView.findViewById(R.id.gameType);
holder.textScore = (TextView)convertView.findViewById(R.id.score);
convertView.setTag(holder);
}
else
{
holder = (HistoryHolder)convertView.getTag();
}
History history = data[position];
holder.textScore.setText(history.score);
holder.textTitle.setText(history.gametype);
holder.imageIcon.setImageResource(history.icon);
return convertView;
}
static class HistoryHolder
{
ImageView imageIcon;
TextView textTitle;
TextView textScore;
}
}
Implementation
for(int i = 0; i < games.length(); i++) {
JSONObject c = games.getJSONObject(i);
JSONObject gameStats = games.getJSONObject(i).getJSONObject(TAG_STATS);
type[i] = c.getString(TAG_TYPE);
champId[i] = c.getString("championId");
cs[i] = gameStats.getString("minionsKilled");
kills[i] = gameStats.getString("championsKilled");
deaths[i] = gameStats.getString("numDeaths");
assists[i] = gameStats.getString("assists");
win[i] = gameStats.getString("win");
if(win[i].equals("true"))
win[i] = "Victory";
else
win[i] = "Defeat";
if(type[i].equals("RANKED_SOLO_5x5"))
type[i] = "Ranked (Solo)";
if(type[i].equals("CAP_5x5"))
type[i] = "TeamBuilder";
if(type[i].equals("NORMAL"))
type[i] = "Unranked";
score[i] = kills[i] +"/" + deaths[i] + "/" + assists[i];
historyData[i] = new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image
}
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,
historyData);
list.setAdapter(adapter);
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:background="#111111">
</ListView>
list_item.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#111111"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/score"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="0/0/0 KDA"
android:textSize="12sp" />
<TextView
android:id="#+id/gameType"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/score"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>
You are creating a History object with this line:
new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image
This just calls the constructor and creates an object for the garbage collector to take away again.
You are not actually adding this History object to the historyData array.
Before the loop, you should declare the historyData array as:
History[] historyData = new History[games.length();
And in the loop, you should assign the created object to the array
historyData[i] = new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image

Categories

Resources