Reputation: 11
I wanna make an android app with eclipse that using autocomplete and show a result with explanation page. this is my first try. I am still noob about this, need help so much!
I get a sample source code from codeofninja.com (https://www.codeofaninja.com/2013/11/android-autocompletetextview-example-sqlite-database.html)
this project contain 5 java files and 1 xml file;
activity_main.xml, MainActivity.java, CustomAutoCompleteView.java, DatabaseHandler.java, CustomAutoCompleteTextChangedListener.java, MyObject.java
My question is how to make code to show the result with explanation based from this sample sources. I means, when we search "Cofee" then the result is show up, we can touch "Cofee" and get information about it like a Dictionary on mobile application.
thanks!
this sampe source of MainActivity.Java:
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;
}
}
Upvotes: 1
Views: 1288
Reputation: 24998
If you want to handle the click that results from the user selecting one of the suggestions from the dropdown, you will have to use the following:
actextView.setOnItemSelectedListener(new OnItemClickListener() {
@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
//... your stuff
}
});
Source: how to set setOnClickListener for AutoCompleteTextView?
Upvotes: 1