Android自定义Arrayadapter不在列表视图中显示数据

Android自定义Arrayadapter不在列表视图中显示数据,android,listview,android-arrayadapter,Android,Listview,Android Arrayadapter,我想要一个高分的页面。我为布局创建了一个row.xml,并创建了一个自定义的ArrayAdapter。当我打印出我的对象时,我得到了正确的信息,但它没有进入我的列表视图。它什么也看不出来 这是我的row.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_widt

我想要一个高分的页面。我为布局创建了一个row.xml,并创建了一个自定义的ArrayAdapter。当我打印出我的对象时,我得到了正确的信息,但它没有进入我的列表视图。它什么也看不出来

这是我的row.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:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/txtNumber"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"
        android:text="@string/txtNumber" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txtName"
        android:id="@+id/txtName"
        android:layout_gravity="top|bottom"
        android:layout_weight="1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txtScore"
        android:id="@+id/txtScore"
        android:layout_weight="1" />


</LinearLayout>

我的定制阵列适配器

Package be.ehb.dt.multiscreen_highscores;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

// ** * Created by Naomi on 17/12/15. */

public  class PersonAdapter extends ArrayAdapter<Person> {

    public PersonAdapter(Context context, ArrayList<Person> personen) {
        super(context,0, personen);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Person person = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
        }

        TextView txtNumber = (TextView) convertView.findViewById(R.id.txtNumber);
        TextView txtName = (TextView) convertView.findViewById(R.id.txtName);
        TextView txtScore = (TextView) convertView.findViewById(R.id.txtScore);

        txtNumber.setText(""+person.number);
        txtName.setText(person.name);
        txtScore.setText(""+person.score);
    return convertView;
    }
}
Package be.ehb.dt.multiscreen_高分;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
//***由Naomi于2015年12月17日创建*/
公共类PersonalAdapter扩展了ArrayAdapter{
公共角色适配器(上下文,ArrayList personen){
super(上下文,0,personen);
}
公共视图getView(int位置、视图转换视图、视图组父视图){
人员=获取项目(职位);
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.row,parent,false);
}
TextView txtNumber=(TextView)convertView.findViewById(R.id.txtNumber);
TextView txtName=(TextView)convertView.findViewById(R.id.txtName);
TextView txtScore=(TextView)convertView.findViewById(R.id.txtScore);
txtNumber.setText(“+person.number”);
txtName.setText(person.name);
txtScore.setText(“+person.score”);
返回视图;
}
}
我的主要活动是什么

package be.ehb.dt.multiscreen_highscores;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.widget.ListView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity{
    private ArrayList<Person> personen;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<Person> arrayOfPersons = new ArrayList<Person>();
        PersonAdapter adapter = new PersonAdapter(this,arrayOfPersons);
        ListView v = (ListView) findViewById(R.id.listHighscores);


        personen=new ArrayList<>();
        personen.add(new Person(1,"Naomi",3454));
        personen.add(new Person(2,"Steven",2394));
        personen.add(new Person(3, "Lieven", 2254));
        Log.d("print",personen.get(1).toString());
        v.setAdapter(adapter);

    }

}
package be.ehb.dt.multiscreen_高分;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.ListView;
导入java.util.ArrayList;
公共类MainActivity扩展了AppCompatActivity{
私人ArrayList personen;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList arrayOfPersons=新的ArrayList();
PersonalAdapter=新的PersonalAdapter(此为arrayOfPersons);
ListView v=(ListView)findViewById(R.id.listHighscores);
personen=newarraylist();
添加(新人物(1,“娜奥米”,3454));
添加(新人物(2,“史蒂文”,2394));
添加(新人物(3,“Lieven”,2254));
Log.d(“print”,personen.get(1.toString());
v、 设置适配器(适配器);
}
}

您应该修改适配器代码,如下所示:

ArrayList<Person> personen;
public class PersonAdapter extends ArrayAdapter<Person> {

public PersonAdapter(Context context, ArrayList<Person> personen) {
    super(context,0, personen);
    this.personen = personen;

}

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

getCount()
method中的
return personen.size()
。在为dataholding@YogeshSeralia为什么?为什么不能使用数组适配器?@PiyushGupta我不知道在哪里使用getCount方法?我对android非常陌生。在适配器类中。这是一种覆盖方法。只需复制粘贴我的代码,这是毫无意义的。请看一下
ArrayAdapter#getCount()
方法,它的实现方式如下:
return mObjects.size()
其中
mObjects
是传递到适配器中的对象数组。@aga你在哪里看到的?@PiyushGupta由于你的评论,我注意到了我的错误。我没有使用ArrayList personen。我将其更改为:
ArrayList arrayOfPersons=new ArrayList();PersonalAdapter=新的PersonalAdapter(此为arrayOfPersons);ListView v=(ListView)findViewById(R.id.listHighscores);添加(新人物(1,“Naomi”,3454));添加(新人物(2,“Steven”,2394));添加(新人物(3,“Lieven”,2254));v、 设置适配器(适配器)
    ListView v = (ListView) findViewById(R.id.listHighscores);
    personen=new ArrayList<>();
    personen.add(new Person(1,"Naomi",3454));
    personen.add(new Person(2,"Steven",2394));
    personen.add(new Person(3, "Lieven", 2254));
    Log.d("print",personen.get(1).toString());
    PersonAdapter adapter = new PersonAdapter(this,personen)
    v.setAdapter(adapter);