ListView在平时开发中使用的最广泛,应用也是最多的,而使用系统的适配器并不能满足我们多样化的要求,因此本文就自定义适配器展开,下面直接附上我已经测试通过的代码:
MainActivity.java:
package com.example.listviewpushtorefresh;
import java.util.ArrayList;
import java.util.List;
import bean.Person;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
MyAdapter adapter = new MyAdapter(MainActivity.this,0,getData());
listview.setAdapter(adapter);
}
private List<Person> getData() {
// TODO Auto-generated method stub
List<Person> list= new ArrayList<Person>();
for(int i=0;i<20;i++)
{
Person p = new Person(R.drawable.ic_launcher,i+"","我是第"+i+"项");
list.add(p);
}
return list;
}
}
自定义数据类型:Person.java
package bean;
public class Person {
private int mPicture;
private String mTitle;