繁体   English   中英

自定义适配器不会使列表视图中的行膨胀

[英]Custom adapter won't inflate rows in list view

我创建了一个自定义列表视图和一个自定义适配器。 但是,当我显示活动时,它似乎是空的(尽管我确定并且数组中有人传递给适配器)。

1)为什么不显示人员列表? 我的getView或onCreate函数中有问题吗?

2)如何制作一个被另一个视图包围的列表视图(搜索栏位于顶部列表的底部)?

这是适配器的getView函数:

@Override
public View getView(int postion, View convertView, ViewGroup parent)
{
    View rowView = convertView;
    if (rowView == null)//set the convert view and the viewholder
    {
        LayoutInflater inflater = _context.getLayoutInflater();
        rowView = inflater.inflate(_layoutResourceId, null,false);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder._personName = (LargeTest) rowView.findViewById(R.id.personName);
        viewHolder._personBirthDate = (TextView) rowView.findViewById(R.id.personBirthDate);
        viewHolder._icon = (ImageView) rowView.findViewById(R.id.personPic);
        rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();
    Person person=_persons.get(postion);//get current person for the list row
    String personName = person.get_firstName()+" "+person.get_lastName();
    ((TextView) holder._personName).setText(personName);

    String birthDate=person.get_birthDate()+";"+person.get_hebBirthDate();
    holder._personBirthDate.setText(birthDate);

    //TODO change icon to a real image for each Person
    holder._icon.setImageResource(R.drawable.about_me);//test icon
    return rowView;
}

这是listview onCreate函数:

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //TODO remove the test case
        //addTestPersons();//add a test case
        //setContentView(R.layout.person_list_activity);
        PersonDbHelper dbHelper=new PersonDbHelper(this);
        ArrayList<Person> persons =dbHelper.getAllPeopleAsPersonList();//get list to display
        PersonListAdapter adapter=new PersonListAdapter(this, R.layout.person_list_row, persons);
        setListAdapter(adapter);
    }

以及list_row的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/personPic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/about_me" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/personName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Person Name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/personBirthDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1990-2001" />
    </LinearLayout>
</LinearLayout>

在适配器中实现getCount方法:

public int getCount(){
    return _persons.size();
}

对于第二部分,添加headerview,为要显示在listview顶部的视图创建XML布局。 对其进行充气并将其作为headerview添加到您的listview中:

LayoutInflater inflater = this.getLayoutInflater();
View    headerView = inflater.inflate(R.layout.header_view, null,false);
getListView().addHeaderView(headerView,null,false);

由于布局的命名,您正在将“ R.layout.person_list_row”传递给适配器。 我假设您正在传递适配器在其上操作的数组中一项的布局,而不是传递ListView。

构造函数需要传递ListView,同时在其getView中构造(放大)行的布局。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM