您需要创建一个自定义适配器来填充自定义类型列表视图,您可以按照以下步骤一步一步创建一个包含要加载它们的数据的属性的类,如下所示
public class Rec {
private String name;
private String city;
public Rec(String name, String city) {
super();
this.name = name;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
创建一个自定义适配器,其中包含您的记录类及其属性,如下所示
public class customAdapter extends ArrayAdapter<Rec> {
Context context;
public customAdapter(Context context, int textViewResourceId,List<Rec> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view;
Rec currentRec = getItem(position);
LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutinflater.inflate(R.layout.reclist,parent,false);
TextView name = (TextView) view.findViewById(R.id.textView1);
TextView city = (TextView) view.findViewById(R.id.textView2);
name.setText(currentRec.getName());
city.setText(currentRec.getCity());
//return super.getView(position, convertView, parent);
return view;
}
现在为您要显示的列表视图创建一个 Xml,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="16sp"
android:textColor="#000000"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_below="@id/textView1" />
现在创建您的 Main 类,您可以在其中填充列表视图并相应地推送数据,如下所示:
public class MainActivity extends Activity {
private ListView listView;
List<Rec> recList ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recList = new ArrayList<Rec>();
listView= (ListView) findViewById(R.id.listView1);
recList.add(new Rec("Ali","Lahore"));
recList.add(new Rec("Tahir","Karachi"));
recList.add(new Rec("Qasim","Islamabad"));
recList.add(new Rec("Bilal","BWP"));
recList.add(new Rec("Akhtar","Pindi"));
listView.setAdapter(new customAdapter(this,android.R.layout.simple_list_item_1 ,recList));
}
}
创建主要活动的布局,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true">
</ListView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="112dp"
android:text="TextView" />
这是带有动态数据的完整自定义列表视图