【问题标题】:Can not display a ListView in Tab of TabHost无法在 TabHost 的选项卡中显示 ListView
【发布时间】:2012-12-24 07:20:32
【问题描述】:

我创建了以下应用程序。我想在 TabHost 的选项卡中显示一个 ListView。运行应用程序后,我没有在 Tab 上找到任何 ListItem。我重写适配器并实现getView,然后setAdapter。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myTabhost=this.getTabHost();
    LayoutInflater.from(this).inflate(R.layout.activity_main,myTabhost.getTabContentView(),true);
    myTabhost.addTab(myTabhost.newTabSpec("tab1").setIndicator("tab1",getResources().getDrawable(R.drawable.calendar)).setContent(R.id.tab1));
    myTabhost.addTab(myTabhost.newTabSpec("tab2").setIndicator("tab2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab2));
    myTabhost.addTab(myTabhost.newTabSpec("tab3").setIndicator("tab3").setContent(R.id.tab3));
    listView1=(ListView) findViewById(R.id.listView1);

    data=new ArrayList<Map<String,Object>>();
    Map<String,Object> item;
    item=new HashMap<String,Object>();
    item.put("date", "2012-12-25");
    item.put("item","Christmas");
    data.add(item);

    item.put("date", "2013-1-1");
    item.put("item", "new year");
    data.add(item);

    SimpleAdapter adapter=new SimpleAdapter(this, data, android.R.layout.simple_expandable_list_item_2, new String[]{"date","item"}, new int[]{R.id.textView1,R.id.textView2});
    listView1.setAdapter(adapter);
}

您能帮我找出问题所在吗?

【问题讨论】:

  • 您使用的是哪个版本。

标签: android tabs android-tabhost


【解决方案1】:

每次你必须使用新的哈希映射对象在哈希映射的帮助下将数据存储在数组列表中

 Map<String,Object> item = new HashMap<String,Object>();
      item.put("date", "2012-12-25");
      item.put("item","Christmas");
      data.add(item);

 Map<String,Object> item1 = new HashMap<String,Object>();
      item1.put("date", "2013-1-1");
      item1.put("item", "new year");
      data.add(item1);

【讨论】:

    【解决方案2】:

    API 级别 13 中已弃用 TabActivity 类。新应用程序应使用 Fragments 而不是此类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多