2015-08-17 65 views
0

我正在製作一個生物醫學應用程序,它提供信息並在此處被引用。我試圖利用從應用程序中的數據庫(.db文件)中提取信息的搜索功能。搜索功能似乎正在工作,但搜索引導用戶到空白頁面。我希望結果以列表視圖呈現。我需要在我的showResults方法中使用搜索數據創建/填充列表視圖? (我有一個空白列表視圖的佈局文件(search_results))。在列表視圖中顯示搜索結果

package com.joe.biomarker; 
    import android.app.Activity; 
    import android.app.SearchManager; 
    import android.content.Intent; 
    import android.database.Cursor; 
    import android.os.Bundle; 
    import android.widget.SimpleCursorAdapter; 
    import android.widget.ListAdapter; 

    public class SearchResultsActivity extends Activity { 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 



    handleIntent(getIntent()); 
} 

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    handleIntent(intent); 
} 

private void handleIntent(Intent intent) { 

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 

     showResults(query); 
    } 
} 

private void showResults(String query) { 
    // Query your data set and show results 
    // ... 
    Cursor results = mDataTableInstance(query, mColumnsArray); 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getContext(), R.layout.search_results, results, mFromArray, mToArray); 
    mListView.setAdapter(adapter); 
} 

    } 
+0

你想使用'SearchView'嗎?如果是這樣的話,首先使用'setOnQueryTextListener(SearchView.OnQueryTextListener listener)'在偵聽器中你可以使用像'adapter.getFilter()。filter(newText)' – pskink

+0

這樣的簡單方法(約20行真實代碼):https:/ /codeshare.io/aFQT7 – pskink

回答

1

你的問題還不清楚。如果您首先啓動該意圖,則必須將搜索拉動移至後臺線程。爲您的搜索功能創建AsyncTask,將您的適配器作爲參數傳輸並覆蓋onPostExecute()以致電adapter.notifyOnDataSetChanged()

+0

當用戶搜索數據時,出現空白屏幕,並且他們看不到搜索結果。我想要使​​用搜索結果進行列表視圖。如何將查詢結果放入用戶將看到的列表視圖中? –

+0

首先用戶搜索,然後提交?如果是這樣,按照我寫的。在「提交」操作 - 運行你的'AsyncTask'。然後在'onPoseExecute()'裏面調用'.notifyOnDataSetChanged()'。 還是你想讓它像一個實時的建議? –

+0

對,用戶輸入他想要搜索到的內容,然後點擊回車。應用程序應該將其重定向到包含搜索結果的列表視圖的另一個頁面。現在應用程序進入第二頁,但結果未顯示。第二頁只是一個空白的屏幕。 –