如何在android Tab中使用ListView?

发布于 2024-10-17 22:43:12 字数 4573 浏览 1 评论 0原文

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

有人能告诉我哪里错了吗?这是我的代码:

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

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

Good day.
In my app I have three tab (one Activity extend TabActivity and others activitys provides access to content). In first tab I have ImageView, a few TextView and it is works. But when I add ListView and in activity that contain ListView I add a few rows it was not show in may tab.

Can someone tell me where I was wrong? Here my code:

In 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);

In 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();
    }

}

In class that extends 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;
}

And my xml where define ListView (Sorry that so much):

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

UPDATE: After coffe brake I was find mistake: in my xml I add in last LinearLayout "orientation=vertical" and my rows appeared.
SOLVED

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南汐寒笙箫 2024-10-24 22:43:12

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

Also, remember that the ListView is already scrollable, so you shouldn't need to put it under ScrollView.
Im glad you solved your problem youself :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文