Android 带有自定义适配器的ListView,逐个添加元素

Android 带有自定义适配器的ListView,逐个添加元素,android,android-listview,android-arrayadapter,baseadapter,Android,Android Listview,Android Arrayadapter,Baseadapter,在阅读问题时,我尝试了答案中给出的例子,效果很好。 现在我想做同样的事情,但是使用Student类的自定义适配器。因此,我: public class AsyncDemo extends ListActivity { private static final String[] items={"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer",

在阅读问题时,我尝试了答案中给出的例子,效果很好。 现在我想做同样的事情,但是使用
Student
类的自定义适配器。因此,我:

public class AsyncDemo extends ListActivity {
  private static final String[] items={"lorem", "ipsum", "dolor",
                                      "sit", "amet", "consectetuer",
                                      "adipiscing", "elit", "morbi",
                                      "vel", "ligula", "vitae",
                                      "arcu", "aliquet", "mollis",
                                      "etiam", "vel", "erat",
                                      "placerat", "ante",
                                      "porttitor", "sodales",
                                      "pellentesque", "augue",
                                      "purus"};
    private List<Student>  students;
    private StudentAdapter studentsAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

//    setListAdapter(new ArrayAdapter<String>(this,
//                        android.R.layout.simple_list_item_1,
//                        new ArrayList<String>()));

    students = new ArrayList<Student>();
    students.clear();
    studentsAdapter = new StudentAdapter(this, students);
    setListAdapter(studentsAdapter);

    new AddStringTask().execute();
    }

//  class AddStringTask extends AsyncTask<Void, String, Void>
    class AddStringTask extends AsyncTask<Void, Student, Void>
    {
    @Override
    protected Void doInBackground(Void... unused)
    {
      for (String item : items)
      {
        Student student = new Student();
        student.setName(item);
        publishProgress(student);

//        publishProgress(item);

        SystemClock.sleep(200);
  }

  return(null);
}

//    @SuppressWarnings("unchecked")
//    @Override
//    protected void onProgressUpdate(String... item) {
//      ((ArrayAdapter<String>)getListAdapter()).add(item[0]);
//    }
    @Override
    protected void onProgressUpdate(Student... student)
    {
      ((StudentAdapter)getListAdapter()).add(student[0]);
    }

    @Override
    protected void onPostExecute(Void unused) {
      Toast
        .makeText(AsyncDemo.this, "Done!", Toast.LENGTH_SHORT)
        .show();
    }
  }
}


package com.commonsware.android.async;

public class Student
{
private String name;

public void setName(String name) 
  {
  this.name = name;
  }
public String getName() 
  {
  return name;
  }
}
公共类AsyncDemo扩展了ListActivity{
私有静态最终字符串[]项={“lorem”、“ipsum”、“dolor”,
“坐”、“坐”、“坐”、“坐”,
“告别”、“精英”、“摩比”,
“vel”、“ligula”、“vitae”,
“arcu”、“aliquet”、“mollis”,
“etiam”、“vel”、“erat”,
“placerat”,“ante”,
“porttitor”、“sodales”,
“佩伦茨克”,“奥古斯”,
“purus”};
私人名单学生;
私人学生适应者适应者;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//setListAdapter(新阵列适配器)(此,
//android.R.layout.simple\u list\u item\u 1,
//新的ArrayList());
学生=新数组列表();
学生们,清除();
studentsAdapter=新的StudentAdapter(本,学生);
setListAdapter(学生适配器);
新建AddStringTask().execute();
}
//类AddStringTask扩展了AsyncTask
类AddStringTask扩展了AsyncTask
{
@凌驾
受保护的空位背景(空位…未使用)
{
用于(字符串项:项)
{
学生=新生();
学生名称(项目);
出版进度(学生);
//出版进度(项目);
系统时钟。睡眠(200);
}
返回(空);
}
//@SuppressWarnings(“未选中”)
//@覆盖
//受保护的void onProgressUpdate(字符串…项){
//((ArrayAdapter)getListAdapter()).add(项[0]);
//    }
@凌驾
受保护的void onProgressUpdate(学生…学生)
{
((StudentAdapter)getListAdapter()).add(student[0]);
}
@凌驾
受保护的void onPostExecute(未使用的void){
干杯
.makeText(AsyncDemo.this,“完成!”,Toast.LENGTH\u SHORT)
.show();
}
}
}
包com.commonware.android.async;
公立班学生
{
私有字符串名称;
公共void集合名(字符串名)
{
this.name=名称;
}
公共字符串getName()
{
返回名称;
}
}
以及:

公共类StudentAdapter扩展BaseAdapter{
私人充气机;
私人名单学生;
私人持票人;
公共学生适配器(活动上下文,列出学生)
{
this.inflater=layoutiner.from(上下文);
这个。学生=学生;
}
公开作废添加(学生相册)
{
Log.w(“学生适应程序”、“添加”);
添加(相册);
}
@凌驾
public int getCount()
{
w(“StudentAdapter”,“getCount:+students.size());
返回学生。大小();
}
@凌驾
公共对象getItem(int位置)
{
Log.w(“StudentAdapter”、“getItem”);
返回学生。获得(职位);
}
@凌驾
公共长getItemId(int位置)
{
Log.w(“StudentAdapter”、“getItemId”);
返回位置;
}
私有类视窗持有者
{
TextView电视相册名称;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
Log.w(“StudentAdapter”、“getView”);
视图行视图=转换视图;
if(rowView==null)
{
rowView=充气机。充气(R.layout.student_项,空);
holder=新的ViewHolder();
holder.tv_相册_name=(TextView)rowView.findviewbyd(R.id.tv_学生_name);
rowView.setTag(支架);
}
其他的
{
holder=(ViewHolder)rowView.getTag();
}
holder.tv_相册_name.setText(students.get(position.getName());
返回行视图;
}
}
问题是只显示第一行,几秒钟后将显示
Toast
。我不明白为什么(在日志中)只有一个调用
getView

我的代码有错误吗?少了什么?有人能帮我吗?

尝试添加第二行:

((StudentAdapter)getListAdapter()).add(student[0]);
((StudentAdapter)getListAdapter()).notifyDataSetChanged();

这是否显示了其他项?

还可以从
适配器调用
add()
方法中的
notifyDataSetChanged()

public void add(Student album)
{
    Log.w("StudentAdapter","add");
    students.add(album);
    notifyDataSetChanged();
}
检查您的学生。大小()?
public void add(Student album)
{
    Log.w("StudentAdapter","add");
    students.add(album);
    notifyDataSetChanged();
}