简体   繁体   中英

android ListActivity not working with custom adapter

I am having trouble with what appears to be a simple use of a ListActivity with a custom adapter. Here is the adapter code:

public class CustomRowAdapter extends BaseAdapter
{
    private Context mContext;

    public CustomRowAdapter ( Context context )
    {
        mContext = context;
    }

    @Override
    public int getCount()
    {
        return 6;
    }

    @Override
    public Object getItem( int position )
    {
        return null;
    }

    @Override
    public long getItemId( int position )
    {
        return position;
    }

    @Override
    public View getView( int position, View convertView, ViewGroup parent )
    {
        LinearLayout rowLayout;

        if( convertView == null )
        {
            rowLayout = ( LinearLayout )LayoutInflater.from( mContext ).inflate( R.layout.list_item_layout, parent, false );
        }
        else
        {
            rowLayout = ( LinearLayout )convertView;
        }

        return rowLayout;
    }
}

and here's the activity:

public class MyListActivity extends ListActivity
{
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );

        setContentView( R.layout.list_layout );
    }

    @Override
    public void onResume()
    {
        super.onResume();

        CustomRowAdapter rowAdapter = new CustomRowAdapter( this );
        setListAdapter( rowAdapter );
    }
}

list_item_layout is just a LinearLayout with a single TextView in it, so I would expect to see 6 identical items in my list, but the list is empty. I've stepped through CustomRowAdapter.getView() and it is being called with positions 0 through 5. I must be missing something simple. Why is the list appearing empty?

EDIT: The ListView has children in hierarchyviewer, so it looks like I may have an issue somewhere in my layouts. Here's the layout containing the ListView:

<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <TableLayout android:id="@+id/TableLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:stretchColumns="*" android:orientation="horizontal">
        <TableRow android:layout_height="wrap_content" android:id="@+id/TableRow01" android:layout_width="fill_parent">
            <Button android:layout_width="wrap_content" android:layout_gravity="left|center_vertical" android:minWidth="65dp" android:layout_height="wrap_content" android:id="@+id/back_button" android:text="@string/back"></Button>
            <TextView android:textColor="#FFFFFF" android:textStyle="bold" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="16sp" android:text="@string/general_information"></TextView>
            <Button android:layout_width="wrap_content" android:layout_gravity="right|center_vertical" android:minWidth="65dp" android:layout_height="wrap_content" android:id="@+id/next_button" android:text="@string/next"></Button>
        </TableRow>
    </TableLayout>
    <ListView android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@android:id/list"></ListView>
</LinearLayout>

and here's the layout for the list items:

<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:textColor="#FFFFFF"></TextView>
</LinearLayout>

Add android:orientation="vertical" to your top id/linearLayout1 (not the row).

On your row, you might want to add android:minHeight="?android:attr/listPreferredItemHeight" as well. That will keep your rows from getting too short.

You are not setting anything in your getView method. Check Efficient Adapter here is link

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