我想要一个高分的页面。我为布局创建了一个row.xml,并制作了一个自定义的ArrayAdapter。当我打印出我的对象时,我得到了正确的信息,但它没有进入我的列表视图。它根本没有显示任何内容。
这是我的row.xml
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;
}
}
我的自定义arrayAdapter
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);
}
}
我的mainActivity
{{1}}
答案 0 :(得分:3)
您应该修改适配器代码,例如
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();
}
现在到达Person person = personen.get(position);
修改强>
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);