android - 如何在android Tab 中使用ListView?

标签 android listview adapter tabactivity

美好的一天。 在我的应用程序中,我有三个选项卡(一个 Activity 扩展 TabActivity,其他 Activity 提供对内容的访问)。在第一个选项卡中,我有 ImageView,一些 TextView,并且可以正常工作。但是当我添加 ListView 并在包含 ListView 的 Activity 中添加几行时,它没有显示在可能的选项卡中。

谁能告诉我哪里错了?这是我的代码:

在 StartActivity 中:

    intent = new Intent().setClass(this, GoodsAndShopsActivity.class);

    spec = tabHost.newTabSpec("shops").setIndicator("Shops",
                      res.getDrawable(R.drawable.ic_tab_shops))
                  .setContent(intent);
    tabHost.addTab(spec);

在 GoodsAndShopsActivity 中:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.descriptions);

    m_shopsLayout = (ListView) findViewById(R.id.shops);

    m_shopList = new ArrayList<Shop>();
    m_shopAdapter = new ShopAdapter(m_shopList, this);
    m_shopsLayout.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    m_shopsLayout.setAdapter(m_shopAdapter);

    for (int i = 0; i<3; i++) {
        m_shopList.add(new Shop("new description"));
        m_shopAdapter.notifyDataSetChanged();
    }

}

在扩展 BaseAdapter 的类中:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = m_inflater.inflate(R.layout.shop, null);

        holder = new ViewHolder();
        holder.descriptions = (TextView) convertView.findViewById(R.id.shop);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    String textOnView = m_shops.get(position).getDescription();
    holder.descriptions.setText(textOnView);
    return convertView;
}

static class ViewHolder{
    TextView descriptions;
}

我的 xml 定义了 ListView(很抱歉):

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
    android:id="@+id/full_info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_margin="10px"
        android:src="@drawable/icon">
    </ImageView>

    <LinearLayout
        android:id="@+id/short_info"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon"
        android:layout_alignParentRight="true">

        <TextView
            android:id="@+id/name_for_good"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="Наименование товара">
        </TextView>

        <TextView
            android:id="@+id/best_price"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" 

            android:singleLine="true"
            android:ellipsize="marquee"
            android:text="Лучшая цена: ">
        </TextView>

        <TextView
            android:id="@+id/worst_price"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:text="Худшая цена: ">
        </TextView>

    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:id="@+id/description_and_shop"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/full_info">

    <TextView
        android:id="@+id/description"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Большое подробное описание товара с всякими деталями, нюансами и т.п.">
    </TextView>    

    <ScrollView
        android:id="@+id/ScrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView
            android:id="@+id/shops"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        </ListView>
    </ScrollView>

</LinearLayout>

更新: 在 coffe brake 之后我发现了错误:在我的 xml 中我添加了最后一个 LinearLayout“orientation=vertical”并且我的行出现了。 已解决

最佳答案

此外,请记住 ListView 已经是可滚动的,因此您不需要将它放在 ScrollView 下。 我很高兴你自己解决了你的问题:)

关于android - 如何在android Tab 中使用ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5041202/

相关文章:

android - 如何调试android回调?

android - 如何使用java在android中创建按钮数组

java - 如何在带有 i18n 的应用程序中使用 @StringDef

Windows 8 - XAML - ListView 中行的条件背景

安卓。将联系人显示为 ListView

android - 为什么会出现这个错误? java.lang.RuntimeException : ImageLoader must be init with configuration before using 错误

android - 使用带有 GridLayoutManager 的 RecyclerView 的简单 Android 网格示例(如旧的 GridView)

来自可绘制图像的Android可滚动 ListView

android - 我在 ListView 的 getView() 中将 textview 的 VISIBILITY 设置为 VISIBLE - 它对于下一行也保持可见。怎么解决这个问题呢?

java - Android 中的 Listview 适配器和监听器?