2012-03-20 4 views
0

J'essaie d'afficher des données dans un ListView. J'ai créé ma propre carte mais elle ne semble pas fonctionner.Custom ListAdapter fonctionne maintenant

J'ai mis un point d'arrêt à l'intérieur de la méthode "getView (..)" mais il ne l'a jamais atteint. Je manque probablement quelque chose de simple mais je n'arrive pas à le comprendre.

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> 

Répondre

3

Vous ne pouvez pas retourner 0 dans la méthode getCount() parce que cela va dire à votre adaptateur que vous n'avez pas encore éléments l'adaptateur:

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

Un grand merci. Travaux! – theblitz

0

Votre adaptateur semble être basé sur vos scores que vous list..so faire quelque chose comme ceci:

@Override 
public int getCount() { 
    return scores.size(); 
} 


@Override 
public Object getItem(int position) { 
    scores.get(position); 
} 


@Override 
public long getItemId(int position) { 
    return position; 
} 

BTW: Les variables membres (playername & playerScore) de la classe d'adaptateur ne sont probablement pas neccessay ?!

Questions connexes