简体   繁体   中英

Custom adapter won't inflate rows in list view

I have created a custom listview and a custom adapter. However, when I show the activity it appears empty (although I made sure and there are people in the array passed to the adapter).

1) Why won't it show the people list? Is there something wrong in my getView or in my onCreate function?

2) How can I make a list view that is surrounded by another view (searchbar on top list at the bottom)?

Here is the adapters getView function:

@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;
}

Here is the listview onCreate function:

@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);
    }

And the XML for list_row:

<?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>

Implement the getCount method in the adapter:

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

For the second part, add the headerview, create an XML layout for the view that you want to show on top of listview. Inflate it and add as headerview to your listview:

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

Because of the naming of the layout, you are passing "R.layout.person_list_row" to your adapter. I assume that you are passing the layout for one item in the array on which the adapter operates on instead of passing the ListView.

The constructor needs the ListView to be passed while in its getView, the layout for the row is constructed (inflated).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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