I am trying to figure out a search interface for Android. My first question has to do with implenting the listview. I am using a search widget on the action bar. Here is the opening code:
public class SearchableActivity extends ListActivity {
// class fields
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query = intent.getStringExtra(SearchManager.QUERY);
ProgressDialog dialog = ProgressDialog.show(
SearchableActivity.this, "Searching",
"Searching. Please wait...", true);
performSearch(query);
dialog.hide();
dialog.dismiss();
}
}
public void performSearch(String query) {
List<String> dataList = new ArrayList<String>();
dataList = new ArrayList<String>();
new task().execute(); // Task calls an HttpPost and opens search through php and populates list through JSON
ArrayAdapter<String> arr = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, dataList);
this.setListAdapter(arr);
}
- In first method I am setting up a ListView, I am not sure what code I need to do onClick? Where does the search results go? I’d like to display closest results as the user types.
- Not sure where to put the xml R.id.list? Is it built in to the widget as a drop down? (e.g. the search widget in Play Store)
- Also, is my implementation of using a Task correct in this and sending the query to php?
If you want to perform a action on
list itemi.e. a row ..Like you have a List ofProductandOnClickyou want to display thedetails of your productthen you launch your new Activity which shows details of that related product.No need to add it in your
search.xmllike..For more read this.
NO, Use AsyncTask.