自定义ListAdapter不起作用

时间:2012-03-20 19:36:35

标签: android listview listadapter custom-adapter

我试图在ListView中显示一些数据。 我创建了自己的适配器,但它似乎没有工作。

我在方法“getView(..)”中设置了一个断点但它从未到达它。

我可能遗漏了一些简单的东西,但无法弄明白。

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>

2 个答案:

答案 0 :(得分:3)

您不能在getCount()方法中返回0,因为这会告诉您的适配器您在适配器中没有任何元素:

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

答案 1 :(得分:0)

您的适配器似乎基于您的分数列表。所以您执行以下操作:

@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:Adapter类的成员变量(playerName&amp; playerScore)可能不是必需的?!