Nate
Nate

Reputation: 412

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>

Upvotes: 0

Views: 495

Answers (2)

Kay
Kay

Reputation: 350

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.

Upvotes: 1

Prasad Pawar
Prasad Pawar

Reputation: 1701

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();
}

Upvotes: 0

Related Questions