我想知道如何在Android中为我的应用程序创建自定义列表视图。我的应用程序中有一个导航抽屉,其中包含几个片段。在一个片段中,我必须显示一个自定义列表视图,其中包含以下文本字段(名称,名称,电话号码)以及片段中的图像视图(照片)。有字符串类型的(名称指定电话号码)等arraylists和位图类型的arraylist (图像)。此数组列表的值是从sqlite数据库获取的。所以我需要一个自定义列表视图,它应该填充这个arraylists的值。我为活动中的每一行创建了一个布局文件。如下所示:我不知道在我的代码中包含此xml文件的位置。另外,我只看到了不适合我情况的教程。我试图实现它。但未能实施它。
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="0dip" android:layout_gravity="top">
<TableRow>
<LinearLayout
android:layout_width="110dp"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left|top"
android:scaleType="centerCrop"
android:id="@+id/image"
/>
</LinearLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="0dip" android:layout_gravity="top"
>
<TableRow>
<TextView
android:id="@+id/text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1" android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:textColor="#000000"
android:layout_span="1"
android:text="Designation:text"
/>
</TableRow>
<TableRow>
<TextView
android:text="Name : text1"
android:id="@+id/text1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:gravity="left"/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/text2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1" android:layout_gravity="left|center_vertical"
android:textSize="16sp"
android:layout_marginLeft="10dip"
android:layout_marginTop="4dip"
android:textColor="#000000"
android:layout_span="1"
android:text="Phone No : text2"
/>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_call" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_email" />
</LinearLayout>
</TableRow>
</TableLayout>
答案 0 :(得分:1)
您需要创建自己的自定义列表适配器,并为列表的每一行增加自定义行布局。只需创建一个扩展BaseAdapter或ArrayAdapter的类。像这样的东西
public class MyListAdapter extends ArrayAdapter<YourObjectItem> {
private Context context;
private List<YourObjectItem> items;
private LayoutInflater mInflater;
public TerritoryListAdapter(Context context, List<YourObjectItem> listOfStuff) {
super(context, territories);
this.context = context;
this.items= listOfStuff;
this.mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//here inflate your view
if(convetView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
}
//now obtain an object from your list of objects
YourObjectItem item = items.get(position);
//use the fields to fill out your list item layout
TextView text1 = (TextView) convertView.findViewById(R.id.text1);
text1.setText(item.getSomethingFromYourObject);
//and so on...
}
}
您还应该覆盖此类的其他公共方法,如getCount和getItemId等。在这种情况下,只需返回列表的大小和列表中项目的位置。希望这有帮助
编辑:我应该在你的布局中使用listView添加它,你应该在Activity的onCreate方法中调用mListView.setAdapter(mListAdapter)。