我想用eclipse製作一個使用自動完成並顯示結果和解釋頁的android應用程序。這是我的第一次嘗試。我仍然對此很不樂觀,需要幫助這麼多!AutoCompleteTextView單擊Android代碼中的事件代碼
我從codeofninja.com(https://www.codeofaninja.com/2013/11/android-autocompletetextview-example-sqlite-database.html)
一個示例源代碼,這個項目包含5個java文件和1個xml文件;
activity_main.xml中,MainActivity.java,CustomAutoCompleteView.java,DatabaseHandler.java,CustomAutoCompleteTextChangedListener.java,MyObject.java
我的問題是,如何讓代碼來顯示與該樣本的來源基於解釋的結果。我的意思是,當我們搜索「Cofee」時,結果顯示出來,我們可以觸摸「Cofee」並獲取有關它的信息,例如移動應用程序上的Dictionary。
謝謝!
MainActivity.Java這個SAMPE來源:
package com.example.autocompletetextviewdb;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
/*
* Change to type CustomAutoCompleteView instead of AutoCompleteTextView
* since we are extending to customize the view and disable filter
* The same with the XML view, type will be CustomAutoCompleteView
*/
CustomAutoCompleteView myAutoComplete;
// adapter for auto-complete
ArrayAdapter<String> myAdapter;
// for database operations
DatabaseHandler databaseH;
// just to add some initial value
String[] item = new String[] {"Please search..."};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
// instantiate database handler
databaseH = new DatabaseHandler(MainActivity.this);
// put sample data to database
insertSampleData();
// autocompletetextview is in activity_main.xml
myAutoComplete = (CustomAutoCompleteView) findViewById(R.id.myautocomplete);
// add the listener so it will tries to suggest while the user types
myAutoComplete.addTextChangedListener(new CustomAutoCompleteTextChangedListener(this));
// set our adapter
myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item);
myAutoComplete.setAdapter(myAdapter);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void insertSampleData(){
// CREATE
databaseH.create(new MyObject("January"));
databaseH.create(new MyObject("February"));
databaseH.create(new MyObject("March"));
databaseH.create(new MyObject("April"));
databaseH.create(new MyObject("May"));
databaseH.create(new MyObject("June"));
databaseH.create(new MyObject("July"));
databaseH.create(new MyObject("August"));
databaseH.create(new MyObject("September"));
databaseH.create(new MyObject("October"));
databaseH.create(new MyObject("November"));
databaseH.create(new MyObject("December"));
databaseH.create(new MyObject("New Caledonia"));
databaseH.create(new MyObject("New Zealand"));
databaseH.create(new MyObject("Papua New Guinea"));
databaseH.create(new MyObject("COFFEE-1K"));
databaseH.create(new MyObject("coffee raw"));
databaseH.create(new MyObject("authentic COFFEE"));
databaseH.create(new MyObject("k12-coffee"));
databaseH.create(new MyObject("view coffee"));
databaseH.create(new MyObject("Indian-coffee-two"));
}
// this function is used in CustomAutoCompleteTextChangedListener.java
public String[] getItemsFromDb(String searchTerm){
// add items on the array dynamically
List<MyObject> products = databaseH.read(searchTerm);
int rowCount = products.size();
String[] item = new String[rowCount];
int x = 0;
for (MyObject record : products) {
item[x] = record.objectName;
x++;
}
return item;
}
}