0

为什么我的 listView 没有出现……我的编码有什么问题?listView 应该显示我数据库中的数据.. 这是我的编码......它没有显示任何错误。所以我不知道错误在哪里。

教员.java

package com.example.prototype;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

public class Faculty extends Activity {

    // Search EditText
    EditText inputSearch;
    // List view
    private ListView myListView;
    private DatabaseAdapter DbAdapter;


   /* // Listview Adapter
    ArrayAdapter<String> adapter;    
    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;
    protected SQLiteDatabase db;*/


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qs_faculty);

        inputSearch = (EditText) findViewById(R.id.inputSearch);
        myListView = (ListView) findViewById(R.id.list_view);

        handleIntent(getIntent());
        DbAdapter = new DatabaseAdapter(this);
        DbAdapter.open();

      //Clean all Customers
        DbAdapter.deleteAllCustomers();
        //Add some Customer data as a sample
        DbAdapter.createLocation("PIZZA1", "Pizza Hut", "1107 West Adams Boulevard", "", "Los Angeles", "CA", "90007", null, null);
        DbAdapter.createLocation("PIZZA2", "Pizza Hut", "1562 West Pico Boulevard", "", "Los Angeles", "CA", "90015", null, null);
        DbAdapter.createLocation("PIZZA3", "Pizza Hut", "718 South Los Angeles Street", "", "Los Angeles", "CA", "90014", null, null);
        DbAdapter.createLocation("PIZZA4", "Pizza Hut", "2542 West Temple Street", "", "Los Angeles", "CA", "90026", null, null);
        DbAdapter.createLocation("PIZZA5", "Pizza Hut", "4329 North Figueroa Street", "", "Los Angeles", "CA", "90065", null, null);
        DbAdapter.createLocation("PIZZA6", "Pizza Hut", "4351 South Central Avenue", "", "Los Angeles", "CA", "90011", null, null);
        DbAdapter.createLocation("SUB1", "Subway", "975 West Jefferson", "", "Los Angeles", "CA", "90007", null, null);
        DbAdapter.createLocation("SUB2", "Subway", "2805 South Figueroa Street", "", "Los Angeles", "CA", "90007", null, null);
        DbAdapter.createLocation("SUB3", "Subway", "198 South Vermont Avenue", "", "Los Angeles", "CA", "90004", null, null);
        DbAdapter.createLocation("SUB4", "Subway", "504 West Olympic Boulevard", "", "Los Angeles", "CA", "90015", null, null);

         }    

    @Override
    protected void onNewIntent(Intent intent) {
        // Because this activity has set launchMode="singleTop", the system calls this method
        // to deliver the intent if this activity is currently the foreground activity when
        // invoked again (when the user executes a search from this activity, we don't create
        // a new instance of this activity, so the system delivers the search intent here)
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            // handles a click on a search suggestion; launches activity to show word
            Intent wordIntent = new Intent(this, FacultyDetail.class);
            wordIntent.setData(intent.getData());
            startActivity(wordIntent);
        } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            // handles a search query
            String query = intent.getStringExtra(SearchManager.QUERY);
            showResults(query);
        }
    }
        private void showResults(String query) {

            Cursor cursor = DbAdapter.searchCustomer((query != null ? query.toString() : "@@@@"));

            if (cursor == null) {
                // There are no results
                inputSearch.setText(getString(R.string.no_results, new Object[] {query}));
            } else {
                // Display the number of results
                int count = cursor.getCount();
                String countString = getResources().getQuantityString(R.plurals.search_results,
                                        count, new Object[] {count, query});
                inputSearch.setText(countString);

                // Specify the columns we want to display in the result
                String[] from = new String[] { DatabaseAdapter.KEY_NAME,
                                               DatabaseAdapter.KEY_ALAMAT,
                                               DatabaseAdapter.KEY_NOTEL,
                                               DatabaseAdapter.KEY_FAX,
                                               DatabaseAdapter.KEY_EMAIL,
                                               DatabaseAdapter.KEY_TRANSPORT,
                                               DatabaseAdapter.KEY_LANDMARK,
                                               DatabaseAdapter.KEY_WEBSITE};

                // Specify the corresponding layout elements where we want the columns to go
                int[] to = new int[] { R.id.faculty_name,
                                        R.id.faculty_alamat,
                                        R.id. faculty_notel,
                                        R.id. faculty_fax,
                                        R.id. faculty_email,
                                        R.id. faculty_transport,
                                        R.id. faculty_landmark,
                                        R.id. faculty_website};

                // Create a simple cursor adapter for the definitions and apply them to the ListView
                SimpleCursorAdapter words = new SimpleCursorAdapter(this,
                                              R.layout.qs_faculty_detail, cursor, from, to);
                myListView.setAdapter(words);

             // Define the on-click listener for the list items
                myListView.setOnItemClickListener(new OnItemClickListener() {

                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // Get the cursor, positioned to the corresponding row in the result set
                        Cursor cursor = (Cursor) myListView.getItemAtPosition(position);
                        // Build the Intent used to open WordActivity with a specific word Uri
                        Intent wordIntent = new Intent(getApplicationContext(), FacultyDetail.class);
                        wordIntent.putExtra("KEY_ROWID", cursor.getInt(cursor.getColumnIndex("_id")));
                      /*  Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,String.valueOf(id));
                        wordIntent.setData(data);*/
                        startActivity(wordIntent);
                    }
                });
            }
        }
  }

FacultyDetail.java

package com.example.prototype;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.widget.TextView;

public class FacultyDetail extends Activity {

        private TextView name;
        private TextView alamat;
        private TextView notel;
        private TextView fax;
        private TextView email;
        private TextView transport;
        private TextView landmark;
        private TextView website;
        private int placeId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qs_faculty_detail);


        /*placeId = getIntent().getIntExtra("KEY_ROWID", 0);
        SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT emp._id, emp.firstName, emp.lastName, emp.title, emp.officePhone, emp.cellPhone, emp.email, emp.managerId, mgr.firstName managerFirstName, mgr.lastName managerLastName FROM employee emp LEFT OUTER JOIN employee mgr ON emp.managerId = mgr._id WHERE emp._id = ?", 
                new String[]{""+ placeId});*/

        Uri uri = getIntent().getData();
        Cursor cursor = managedQuery(uri, null, null, null, null);

        if (cursor.getCount() == 1)
        {
            cursor.moveToFirst();

            name = (TextView) findViewById(R.id.faculty_name);
            name.setText(cursor.getString(cursor.getColumnIndex("Name")));

            alamat = (TextView) findViewById(R.id.faculty_alamat);
            alamat.setText(cursor.getString(cursor.getColumnIndex("title")));

            notel = (TextView) findViewById(R.id.faculty_notel);
            notel.setText(cursor.getString(cursor.getColumnIndex("No_tel")));

            fax = (TextView) findViewById(R.id.faculty_fax);
            fax.setText(cursor.getString(cursor.getColumnIndex("Fax")));

            email = (TextView) findViewById(R.id.faculty_email);
            email.setText(cursor.getString(cursor.getColumnIndex("Email")));

            transport = (TextView) findViewById(R.id.faculty_transport);
            transport.setText(cursor.getString(cursor.getColumnIndex("Transport")));

            landmark = (TextView) findViewById(R.id.faculty_landmark);
            landmark.setText(cursor.getString(cursor.getColumnIndex("Landmark")));

            website = (TextView) findViewById(R.id.faculty_website);
            website.setText(cursor.getString(cursor.getColumnIndex("Website")));

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.faculty_detail, menu);
        return true;
    }

}

数据库适配器.java

package com.example.prototype;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseAdapter {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "Name";
    public static final String KEY_ALAMAT = "Alamat";
    public static final String KEY_NOTEL = "No_tel";
    public static final String KEY_FAX = "Fax";
    public static final String KEY_EMAIL = "Email";
    public static final String KEY_TRANSPORT = "Transport";
    public static final String KEY_LANDMARK = "Landmark";
    public static final String KEY_WEBSITE = "Website";
    public static final String KEY_CATEGORY = "Category";

    private static final String TAG = "DatabaseAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_NAME = "AcademicLocation";
    private static final String FTS_VIRTUAL_TABLE = "UKM_Location";
    private static final int DATABASE_VERSION = 1;

    //Create a FTS3 Virtual Table for fast searches
    private static final String DATABASE_CREATE =
        "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + " USING fts3(" +
        KEY_NAME + "," +
        KEY_ALAMAT + "," +
        KEY_NOTEL + "," +
        KEY_FAX + "," +
        KEY_EMAIL + "," +
        KEY_TRANSPORT + "," +
        KEY_LANDMARK + "," +
        KEY_WEBSITE + "," +
        KEY_CATEGORY + "," +
        " UNIQUE (" + KEY_NAME + "));";


    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.w(TAG, DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE);
        }

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

    public DatabaseAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    public DatabaseAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        if (mDbHelper != null) {
            mDbHelper.close();
        }
    }


    public long createLocation(String name, String alamat, String notel, String fax, String email, String transport, String landmark, String website, String category) {

        ContentValues initialValues = new ContentValues();
        String searchValue =     name + " " + 
                                alamat + " " + 
                                notel + " " + 
                                fax + " " + 
                                email + " " + 
                                transport+ " " + 
                                landmark+ " " + 
                                website+ " " + 
                                category;
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_ALAMAT, alamat);
        initialValues.put(KEY_NOTEL, notel);
        initialValues.put(KEY_FAX, fax);
        initialValues.put(KEY_EMAIL, email);
        initialValues.put(KEY_TRANSPORT, transport);
        initialValues.put(KEY_LANDMARK, landmark);
        initialValues.put(KEY_WEBSITE, website);
        initialValues.put(KEY_CATEGORY, category);

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


    public Cursor searchCustomer(String inputText) throws SQLException {
        Log.w(TAG, inputText);
        String query = "SELECT docid as _id," + KEY_NAME + "," + "," +
         " from " + FTS_VIRTUAL_TABLE +
        " where " +  KEY_NAME + " MATCH '" + inputText + "';";
        Log.w(TAG, query);
        Cursor mCursor = mDb.rawQuery(query,null);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }


    public boolean deleteAllCustomers() {

        int doneDelete = 0;
        doneDelete = mDb.delete(FTS_VIRTUAL_TABLE, null , null);
        Log.w(TAG, Integer.toString(doneDelete));
        return doneDelete > 0;

    }

}

请帮我解决这个问题..

4

1 回答 1

0

您确定传入查询有一些结果吗?

String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);

而不是这个:

if (cursor == null) {
   // There are no results
   inputSearch.setText(getString(R.string.no_results, new Object[] {query}));
}

尝试 :

if (cursor == null || !cursor.moveToFirst()) {
   // There are no results
   inputSearch.setText(getString(R.string.no_results, new Object[] {query}));
}
于 2013-06-20T22:55:44.993 回答