您需要創建一個自定義適配器來填充自定義類型列表視圖 您可以按照以下步驟通過步驟 步驟1 創建一個類co ntains屬性要他們的數據加載如下
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" />
現在創建在其中填充您的列表視圖主類,並相應推數據如下:
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" />
這裏是你的完整的定製列表視圖與動態數據
您已經張貼的圖片你想要的ListView控件相同的設計 – Developer
@GauravPandey是 – Sonali
創建自定義列表視圖! – RobinHood