ListView行无法膨胀

时间:2014-02-24 04:49:55

标签: android listview android-listview

我是Android的新手,我正在尝试为我的应用程序创建一个表。我使用listview和自定义适配器连续填充9个数据。我认为它应该工作,但是当我运行应用程序时没有显示listview元素。谢谢你的帮助

以下是我的适配器类的一部分,我认为它有一些问题: 的修改

public class PlayerAdapter extends BaseAdapter {
ArrayList<Player> PlayerList;
private Context mContext;

public PlayerAdapter(Context c,ArrayList<Player> List) {
    mContext = c;
    PlayerList=List;
}

public int getCount() {
        return PlayerList.size();
    }

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return PlayerList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return PlayerList.indexOf(getItem(position));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    ViewHolder viewHolder = null;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView == null) {

             rowView = inflater.inflate(R.layout.list_view_activity, parent, false);
             viewHolder = new ViewHolder();
             viewHolder.text1 = (TextView) rowView.findViewById(R.id.textView2);
             viewHolder.text2 = (TextView) rowView.findViewById(R.id.textView3);
             viewHolder.text3 = (TextView) rowView.findViewById(R.id.textView4);
             viewHolder.text4 = (TextView) rowView.findViewById(R.id.textView5);
             viewHolder.text5 = (TextView) rowView.findViewById(R.id.textView6);
             viewHolder.text6 = (TextView) rowView.findViewById(R.id.textView7);
             viewHolder.text7 = (TextView) rowView.findViewById(R.id.textView8);
             viewHolder.text8 = (TextView) rowView.findViewById(R.id.textView9);
             viewHolder.text9 = (TextView) rowView.findViewById(R.id.textView10);
             rowView.setTag(viewHolder);        
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
             viewHolder.text1.setText(PlayerList.get(position)._name);
             viewHolder.text2.setText(""+PlayerList.get(position)._play);
             viewHolder.text3.setText(""+PlayerList.get(position)._win);
             viewHolder.text4.setText(""+PlayerList.get(position)._draw);
             viewHolder.text5.setText(""+PlayerList.get(position)._lose);
             viewHolder.text6.setText(""+PlayerList.get(position)._GD);
             viewHolder.text7.setText(""+PlayerList.get(position)._GF);
             viewHolder.text8.setText(""+PlayerList.get(position)._GA);
             viewHolder.text9.setText(""+PlayerList.get(position)._PTS);
            // set text for other TextViews too if u want to set value for each TextView I only show for 1st one
            return rowView;
        }

    private class ViewHolder {
        TextView text1;
        TextView text2;
        TextView text3;
        TextView text4;
        TextView text5;
        TextView text6;
        TextView text7;
        TextView text8;
        TextView text9;
    }

}

这是我的主要计划:

ArrayList<Player> PlayerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    PlayerList = new ArrayList<Player>();
    PlayerList.add(new Player("BER"));
    PlayerList.add(new Player("DOM"));
    PlayerList.add(new Player("FER"));
    PlayerList.add(new Player("HEN"));
    PlayerList.add(new Player("GAR"));
    PlayerList.add(new Player("JER"));
    PlayerList.add(new Player("JON"));
    PlayerList.add(new Player("IRV"));
    PlayerList.add(new Player("LIM"));

    ListView listView = (ListView) findViewById(R.id.player_list);
    listView.setAdapter(new PlayerAdapter(this,PlayerList));
}

而且,这是玩家类:

ADDED

public class Player {
String _name;
int _play,_win,_draw,_lose,_GF,_GA,_GD,_PTS;

public Player(String name){
    _play=0;_win=0;_draw=0;_lose=0;_GF=0;_GA=0;_GD=0;_PTS=0;
    _name = name;
}

void play(int goal,int concede){
    _play++;_GF+=goal;_GA+=concede;_GD+=(goal-concede);
    if(goal>concede){
        _win++;
        _PTS+=3;
    }
    else if(goal<concede){
        _lose++;
    }
    else{
        _draw++;
        _PTS+=1;
    }

}

activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:background="#000000" android:layout_margin="1dip" android:id="@+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="PLAYER" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="@+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="P" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="@+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="W" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="@+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="L" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="@+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="D" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="@+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="GD" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="@+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="GF" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="@+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="GA" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="@+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="PTS" android:textColor="#fff"/> 

</LinearLayout>
<ListView
android:id="@+id/player_list" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" > 

5 个答案:

答案 0 :(得分:1)

删除for循环,get view方法将自行循环..

修改

您必须初始化所有10个文本视图,然后才会循环显示将应用文本..

所以在循环之外启动所有10个文本视图..

答案 1 :(得分:0)

 public View getView(int position, View convertView, ViewGroup parent) {
         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View rowView = inflater.inflate(R.layout.list_view_activity, parent, null);
         TextView textView;
        // **for(int i=0;i<9;i++){// Remove this for loop inside your code** 
             textView = (TextView) rowView.findViewById(tv[position]);
             textView.setText(playerArray[position]._name);
        // }
        return rowView;
    }

答案 2 :(得分:0)

View rowView = inflater.inflate(R.layout.list_view_activity, parent, false);

- &GT;

View rowView = inflater.inflate(R.layout.list_view_activity, null);

答案 3 :(得分:0)

为您提供给适配器的数组中的每个getView()调用

row。因此,您需要在getView()中应用循环。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View rowView = inflater.inflate(R.layout.list_view_activity, parent, false);
         TextView textView;

             textView = (TextView) rowView.findViewById(tv[i]);
             textView.setText(playerArray[position]._name);

        return rowView;
    }

*注意:此外,您只需制作row-layout,就像您的情况一样,您似乎希望使用单个textview显示每一行。因此,您的R.layout.list_view_activity应仅包含textView。 假设R.layout.list_view_activity中的textView标识为description。因此需要进行以下更改。

textView = (TextView) rowView.findViewById();

答案 4 :(得分:0)

不必声明 int[] tv

int[] tv = {R.id.textView2,R.id.textView3,R.id.textView4,R.id.textView5,R.id.textView6
                ,R.id.textView7,R.id.textView8,R.id.textView9,R.id.textView10};

for loop

for(int i=0;i<9;i++){
             textView = (TextView) rowView.findViewById(tv[i]);
             textView.setText(playerArray[position]._name);
     }

你必须做的改变

1。getItemId(int position)中,您null返回position这样的<{p}}

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

2。getItem(int position)中,您null返回specific position of ur array这样的<{p}}

@Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return playerArray[position];
    }

3。getView(int position, View convertView, ViewGroup parent)执行此类操作并尝试按照 ViewHolder 模式

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
View rowView = convertView;
if(rowView == null) {
         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View rowView = inflater.inflate(R.layout.list_view_activity, parent, false);
         viewHolder = new ViewHolder();
         viewHolder.text1 = (TextView) rowView.findViewById(R.id.textView2);
         viewHolder.text2 = (TextView) rowView.findViewById(R.id.textView3);
         viewHolder.text3 = (TextView) rowView.findViewById(R.id.textView4);
         viewHolder.text4 = (TextView) rowView.findViewById(R.id.textView5);
         viewHolder.text5 = (TextView) rowView.findViewById(R.id.textView6);
         viewHolder.text6 = (TextView) rowView.findViewById(R.id.textView7);
         viewHolder.text7 = (TextView) rowView.findViewById(R.id.textView8);
         viewHolder.text8 = (TextView) rowView.findViewById(R.id.textView9);
         viewHolder.text9 = (TextView) rowView.findViewById(R.id.textView10);
         rowView.setTag(viewHolder);        
} else {
    viewHolder = (ViewHolder) rowView.getTag();
}
         viewHolder.text1.setText(playerArray[position]);
        // set text for other TextViews too if u want to set value for each TextView I only show for 1st one
        return rowView;
    }

private class ViewHolder {
    TextView text1;
    TextView text2;
    TextView text3;
    TextView text4;
    TextView text5;
    TextView text6;
    TextView text7;
    TextView text8;
    TextView text9;
}

Example of BaseAdapter to populate ListView using ArrayList(Ur Class)

Why ViewHolder pattern see this

Official tutorial

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

SCREENSHOT:

enter image description here

编辑:

String之外的setText

viewHolder.text2.setText(String.valueOf(playerDataList.get(position)._play));

list_view_activity 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="horizontal" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>