android ListActivity 不适用于自定义适配器
我在使用自定义适配器来简单使用 ListActivity 时遇到了麻烦。这是适配器代码:
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;
}
}
这是活动:
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 只是一个 LinearLayout,其中有一个 TextView,所以我希望在列表中看到 6 个相同的项目,但列表是空的。我已经单步执行了 CustomRowAdapter.getView(),并且正在使用位置 0 到 5 来调用它。我一定错过了一些简单的东西。为什么列表显示为空?
编辑:ListView 在 HierarchyViewer 中有子级,所以看起来我的布局中的某个地方可能有问题。这是包含 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>
这是列表项的布局:
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
android:orientation="vertical"
添加到您的顶部 id/linearLayout1 (而不是行)。在您的行上,您可能还想添加
android:minHeight="?android:attr/listPreferredItemHeight"
。这将防止你的行变得太短。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.您没有在 getView 方法中设置任何内容。检查高效适配器这里是 链接< /a>
You are not setting anything in your getView method. Check Efficient Adapter here is link