我希望我的Android应用程序在用户单击“全部列出”按钮时将文本文件的内容显示到虚拟表上。我使用来自developer.android网站的示例创建虚拟表。该程序目前无法运行。下面是我正在使用的代码和文本文件的示例。请告诉我可能出了问题。非常感谢。

CEMMainActivity.java

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.os.Build;
import android.provider.ContactsContract;

public class CEMMainActivity extends ActionBarActivity {

protected static final String FTS_TABLE_CREATE = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cemmain);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
    }
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

//Action once Find button is clicked

    Button findequip = (Button)findViewById(R.id.findequip);
    findequip.setOnClickListener(new OnClickListener() {
    public void onClick(View f) {

        public void onCreate(SQLiteDatabase list) {
            SQLiteDatabase mDatabase = list;
            mDatabase.execSQL(FTS_TABLE_CREATE);
            loadEquipmentList();
        }
                    }
    });


fragment_cemmain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="clinical.equipment.monitor.CEMMainActivity$PlaceholderFragment" >
.......

<Button
    android:id="@+id/listallequip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/cleartext"
    android:layout_toRightOf="@+id/equipid"
    android:onClick="listallequip"
    android:text="@string/listallequip" />

 </RelativeLayout>


DatabaseOpenHelper.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.content.Context;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import android.util.Log;

public class DatabaseOpenHelper {

private static final String TAG = "ListAllEquipment";

 //The columns to be included in table
 public static final String COL_TYPE = "EQUIPMENT TYPE";
 public static final String COL_ID = "ID";
 public static final String COL_LOCATION = "LOCATION";
 public static final String COL_STATUS = "STATUS";
 public static final String COL_TIME = "TIME";

 private static final String DATABASE_NAME = "EQUIPMENT LIST";
 private static final String FTS_VIRTUAL_TABLE = "FTS";
 private static final int DATABASE_VERSION = 1;

 private final DatabaseOpenHelper mDatabaseOpenHelper;

 public DatabaseOpenHelper(Context context) {
    // TODO Auto-generated constructor stub
}
 public void Listalldatabase (Context context) {
    mDatabaseOpenHelper = new DatabaseOpenHelper(context);


 private static class DatabaseOpenHelper extends SQLiteOpenHelper {

    private final Context mHelperContext;
    private SQLiteDatabase mDatabase;

    private static final String FTS_TABLE_CREATE =
                "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +
                " USING fts3 (" +
                COL_TYPE + ", " +
                COL_ID + ", " + COL_LOCATION + ", " + COL_STATUS +", " + COL_TIME + ")";

    DatabaseOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mHelperContext = context;
    }

        // populate the virtual table

        private void loadEquipmentList() {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        loadType();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
        }

    private void loadType() throws IOException {
        final Resources resources = mHelperContext.getResources();
        InputStream inputStream = resources.openRawResource(R.raw.info_sample);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        try {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] strings = TextUtils.split(line, "-");
                if (strings.length < 5) continue;
                long list = addItem(strings[0].trim(), strings[1].trim(), strings[2].trim(), strings[3].trim(), strings[4].trim());
                if (list < 0) {
                    Log.e(TAG, "unable to add word: " + strings[0].trim());
                }
            }
        } finally {
            reader.close();
        }

    public long addItem (String type; String id; String location; String status; String time) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(COL_TYPE, type);
        initialValues.put(COL_ID, id);
        initialValues.put(COL_LOCATION, location);
        initialValues.put(COL_STATUS, status);
        initialValues.put(COL_TIME, time);

        return mDatabase.insert(FTS_VIRTUAL_TABLE, null, initialValues);
    }
    }


    }

    @Override
    public void onUpgrade(SQLiteDatabase list, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        list.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
        onCreate(list);
    }
   }
}


info_sample.txt

Equipment Type  Equipment ID    Location        Status      Time
Infusion Pump   1234             2              On      12:00
Hoist           3645             1             Off      13:00
Bed             2563             3              Occupied    14:00

最佳答案

改变这个

setContentView(R.layout.activity_cemmain);




setContentView(R.layout.fragment_cemmain);


因为Buttonfragment_cemmain.xml中,所以setContentView应该引用fragment_cemmain.xml

并删除

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
}

09-27 18:14