Search code examples
androidlistviewlistadaptercustom-adapter

Custom ListAdapter not working


I am trying to show some data in a ListView. I created my own Adapter but it doesn't seem to be working.

I put a break point inside the method "getView(..)" but it never reached it.

I am probably missing something simple but can't figure it out.

package mpg.scoreControl;

import java.util.ArrayList;

import mpg.playerControl.MPGPlayer;
import mpg.playerControl.MPGPlayerControl;
import multiplayerGameControl.pkg.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SlidingDrawer;
import android.widget.ListView;
import android.widget.TextView;

public class MPGGameScore {

    ArrayList<MPGGameScoreEntry> scores;
    protected class MPGGameScoreEntry{
        public String playerName;
        public int playerScore;
        public MPGGameScoreEntry(String playerName, int playerScore) {
            this.playerName = playerName;
            this.playerScore = playerScore;
        }
    }

    private class GameScoreAdaptor  extends BaseAdapter{


     private LayoutInflater mInflater;
     public GameScoreAdaptor(Context context) {
//        searchArrayList = results;
          mInflater = LayoutInflater.from(context);
         }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          if (convertView == null) {
           convertView = mInflater.inflate(R.layout.playerscoresrow, null);
           holder = new ViewHolder();
           holder.tvwPlayerName = (TextView) convertView.findViewById(R.id.tvwPlayerName);
           holder.tvwPlayerScore = (TextView) convertView.findViewById(R.id.tvwPlayerScore);

           convertView.setTag(holder);
          } else {
           holder = (ViewHolder) convertView.getTag();
          }

          holder.tvwPlayerName.setText(scores.get(position).playerName);
          holder.tvwPlayerScore.setText(scores.get(position).playerScore);

          return convertView;
         }
    }

    static class ViewHolder {
          TextView tvwPlayerName;
          TextView tvwPlayerScore;
    }

    public void showCurrentScores(final  Activity context, SlidingDrawer sd){
        ListView lvwScores = (ListView) sd.findViewById(R.id.lvwScores);


        // Build arraylist with scores.


        scores = new ArrayList<MPGGameScoreEntry>();

        // Now fill it up with rows

        for (MPGPlayer player: MPGPlayerControl.getInstance().players)
            scores.add(new MPGGameScoreEntry(player.playerName,player.playerDetails.playerScore.getScoreInt()));

        lvwScores.setAdapter(new GameScoreAdaptor(context));

    }
}

score_drawer:

<?xml version="1.0" encoding="utf-8"?>

<SlidingDrawer android:id="@+id/slidingDrawer" android:handle="@+id/drawerHandle"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:content="@+id/contentLayout" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_alignParentBottom="true" 
                android:visibility="visible">
     <ImageView android:id="@+id/drawerHandle"
                android:src="@drawable/blue_arrow_up_flat"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
    </ImageView>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/contentLayout" android:gravity="center"
                  android:layout_width="fill_parent" android:layout_height="wrap_content">

      <ListView
       android:id="@+id/lvwScores" 
       android:layout_width="fill_parent" android:layout_height="wrap_content"  
       android:divider="#FFFFFF" android:dividerHeight="1dip" 
       android:layout_weight="1"  android:layout_marginBottom="60dip"
       android:footerDividersEnabled="true" android:headerDividersEnabled ="true"/>
    </LinearLayout>
</SlidingDrawer>

playerscorerow:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">

  <TextView android:id="@+id/tvwPlayerName" android:layout_weight="1" android:gravity="left"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"/>
  <TextView android:id="@+id/tvwPlayerScore" android:layout_weight="1" android:gravity="right"
    android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="12sp"
    android:layout_alignParentRight="true"/>
</RelativeLayout>

Solution

  • You can't return 0 in the getCount() method because this will tell your adapter that you don't have any elements in the adapter:

          @Override
            public int getCount() {
               return  24 ; // I just put a number here,if you plan to use the scores      ArrayList as the data 
    // of the adapter you should return here scores.size();     
        }