【发布时间】:2015-12-17 13:12:52
【问题描述】:
我想要一个有高分的页面。我为布局制作了一个 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;
}
}
还有我的 mainActivity
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);
}
}
【问题讨论】:
-
return personen.size()在getCount()方法中。 -
在使用 POJO 进行数据保存时使用 BaseAdapter
-
@YogeshSeralia 为什么?为什么不能使用 Array 适配器?
-
@PiyushGupta 我看不到在哪里使用 getCount 方法?我对 android 很陌生。
-
在适配器类中。它是一种覆盖方法。只需复制粘贴我的代码
标签: android listview android-arrayadapter