具有2个选项卡和2个列表视图的Tabhost从数据库获取数据

时间:2011-05-18 19:08:30

标签: android listview android-tabhost

我已经按照几个不同的教程,没有任何运气让这个工作。我能看到的唯一区别是我使用数据库从中提取数据,并且教程创建了一个数组来获取数据。

以下是我正在使用的代码,看看是否有人可以帮助我。

main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <ListView android:layout_width="fill_parent" android:id="@+id/listView1"
            android:layout_height="wrap_content"></ListView>
        <ListView android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/listView2">
        </ListView>
    </FrameLayout>
</LinearLayout>

main.java文件:

import android.app.TabActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;

import com.facebook.android.R;

public class MainLib extends TabActivity implements OnTabChangeListener {


private static final String TAG = "MainLib";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainlib);

    //final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
    //tabHost.setup();
    TabHost mTabHost = getTabHost();
    mTabHost.setOnTabChangedListener(this);

    // Find your views NEW LIBS
    final ListView listPro = (ListView) findViewById(R.id.listView1);

    final DBLibAdapter db = new DBLibAdapter(this);

    db.open();

    final Cursor c = db.getAllNewLibs();
    startManagingCursor(c);
    Log.d(TAG, "New Libs Cursor Created");

    // Create the adapter
    NewLibAdapter adapter = new NewLibAdapter(this, c);
    listPro.setAdapter(adapter);
    Log.d(TAG, "New Libs Adapter Created");

    db.close();


    listPro.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            db.open();

            db.updateId(id);

            db.close();

            Intent miamor = new Intent(MainLib.this, Result.class);
            startActivity(miamor);

        }
    });

    // Find your views SAVED LIBS
    final ListView listPro1 = (ListView) findViewById(R.id.listView2);

    db.open();

    final Cursor c1 = db.getAllSavedLibs();
        startManagingCursor(c1);

        Log.d(TAG, "Saved Libs Cursor Created");
    // Create the adapter
    SavedLibAdapter adapter1 = new SavedLibAdapter(this, c1);
    listPro1.setAdapter(adapter1);

    Log.d(TAG, "Saved Libs Adapter Created");

    db.close();

    listPro1.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            db.open();

            db.updateId(id);

            db.close();

            Intent amormio = new Intent(MainLib.this, Result.class);
            startActivity(amormio);

        }
    });

    mTabHost.addTab(mTabHost.newTabSpec("New Libs").setIndicator("New Libs")
            .setContent(new TabContentFactory(){

                @Override
                public View createTabContent(String arg0) {
                    return listPro;
                }
            }));

    mTabHost.addTab(mTabHost.newTabSpec("Saved Libs").setIndicator("Saved Libs")
            .setContent(new TabContentFactory(){

                @Override
                public View createTabContent(String tag) {
                    return listPro1;
                }
            }));

    mTabHost.setCurrentTab(0);
}

  public void onTabChanged(String tabName) {
      if(tabName.equals("New Libs")) {
          //do something
      }
      else if(tabName.equals("Saved Libs")) {
          //do something
      }
}
}

第一个listView有数据但第二​​个没有。我试图删除第二个,但它仍然会抛出一条错误消息:

05-18 16:57:30.964: ERROR/AndroidRuntime(32753): Uncaught handler: thread main exiting due to uncaught exception
05-18 16:57:30.984: ERROR/AndroidRuntime(32753): java.lang.NullPointerException
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at android.os.Looper.loop(Looper.java:123)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at java.lang.reflect.Method.invokeNative(Native Method)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at java.lang.reflect.Method.invoke(Method.java:521)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):     at dalvik.system.NativeStart.main(Native Method)

请帮忙!

1 个答案:

答案 0 :(得分:0)

您在主布局中包含ListView。您需要为每个活动创建布局。您正在按需创建布局,但随后引用另一个布局(主要)中的列表视图。

有关示例,请参阅选项卡布局教程:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html