public class OpenDialogAdapter extends ArrayAdapter<Phone> { private int resourceId; public OpenDialogAdapter(Context context, int textViewResourceId, List<Phone> objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { //第一种方法 /*LayoutInflater mInflater = (LayoutInflater) getContext() .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);*/ //第二种方法 LayoutInflater mInflater = LayoutInflater.from(getContext()); //此处嵌套另外的一个layout convertView = mInflater.inflate(R.layout.custom_dialog_listview_item, null); Phone phone = getItem(position); TextView typeText = (TextView) convertView.findViewById(R.id.type_view); TextView numberText = (TextView) convertView.findViewById(R.id.tv); String type = phone.getType(); typeText.setText(type); String number = phone.getNumber(); numberText.setText(number); return convertView; } }
主布局:
//customer_dialog_listview.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="vertical" > <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true"> </ListView>
子布局 customer_dialog_listview_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/type_view" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:textStyle="bold" android:gravity="center_vertical" /> </LinearLayout>
在onCreat()中
OpenDialogAdapter openDialogAdp = new OpenDialogAdapter( ContactActivity.this, R.layout.custom_dialog_listview, extracts);