use of android.support.v4.widget.CursorAdapter in project mintube by imshyam.
the class MainActivity method onQueryTextChange.
@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
newText = newText.replace(" ", "+");
String url = "http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&client=firefox&q=" + newText;
JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONArray jsonArraySuggestion = (JSONArray) response.get(1);
String[] suggestions = new String[10];
for (int i = 0; i < 10; i++) {
if (!jsonArraySuggestion.isNull(i)) {
suggestions[i] = jsonArraySuggestion.get(i).toString();
}
}
Log.d("Suggestions", Arrays.toString(suggestions));
// Cursor Adaptor
String[] columnNames = { "_id", "suggestion" };
MatrixCursor cursor = new MatrixCursor(columnNames);
String[] temp = new String[2];
int id = 0;
for (String item : suggestions) {
if (item != null) {
temp[0] = Integer.toString(id++);
temp[1] = item;
cursor.addRow(temp);
}
}
CursorAdapter cursorAdapter = new CursorAdapter(getApplicationContext(), cursor, false) {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.search_suggestion_list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final TextView suggest = (TextView) view.findViewById(R.id.suggest);
ImageView putInSearchBox = (ImageView) view.findViewById(R.id.put_in_search_box);
String body = cursor.getString(cursor.getColumnIndexOrThrow("suggestion"));
suggest.setText(body);
suggest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchView.setQuery(suggest.getText(), true);
searchView.clearFocus();
}
});
putInSearchBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchView.setQuery(suggest.getText(), false);
}
});
}
};
searchView.setSuggestionsAdapter(cursorAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Tag", "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
return true;
}
use of android.support.v4.widget.CursorAdapter in project devbricks by dailystudio.
the class AbsCursorAdapterFragment method bindAdapterView.
@Override
protected void bindAdapterView() {
final ListAdapter oldAdapter = getAdapter();
Cursor oldCursor = null;
if (oldAdapter instanceof CursorAdapter) {
oldCursor = ((CursorAdapter) oldAdapter).getCursor();
}
super.bindAdapterView();
if (oldCursor != null) {
swapCursor(oldCursor);
}
}
use of android.support.v4.widget.CursorAdapter in project assertj-android by square.
the class SearchViewAssert method hasSuggestionsAdapter.
public SearchViewAssert hasSuggestionsAdapter(CursorAdapter adapter) {
isNotNull();
CursorAdapter actualAdapter = actual.getSuggestionsAdapter();
//
assertThat(actualAdapter).overridingErrorMessage("Expected suggestions adapter <%s> but was <%s>.", adapter, //
actualAdapter).isSameAs(adapter);
return this;
}
Aggregations