Search in sources :

Example 1 with CursorAdapter

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;
}
Also used : Context(android.content.Context) VolleyError(com.android.volley.VolleyError) JsonArrayRequest(com.android.volley.toolbox.JsonArrayRequest) ViewGroup(android.view.ViewGroup) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) SearchView(android.support.v7.widget.SearchView) ImageView(android.widget.ImageView) View(android.view.View) WebView(android.webkit.WebView) TextView(android.widget.TextView) MatrixCursor(android.database.MatrixCursor) WebResourceResponse(android.webkit.WebResourceResponse) Response(com.android.volley.Response) CursorAdapter(android.support.v4.widget.CursorAdapter) TextView(android.widget.TextView) ImageView(android.widget.ImageView)

Example 2 with CursorAdapter

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);
    }
}
Also used : CursorAdapter(android.support.v4.widget.CursorAdapter) SimpleDatabaseObjectCursorAdapter(com.dailystudio.app.widget.SimpleDatabaseObjectCursorAdapter) Cursor(android.database.Cursor) ListAdapter(android.widget.ListAdapter)

Example 3 with CursorAdapter

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;
}
Also used : CursorAdapter(android.support.v4.widget.CursorAdapter)

Aggregations

CursorAdapter (android.support.v4.widget.CursorAdapter)3 Cursor (android.database.Cursor)2 Context (android.content.Context)1 MatrixCursor (android.database.MatrixCursor)1 SearchView (android.support.v7.widget.SearchView)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 WebResourceResponse (android.webkit.WebResourceResponse)1 WebView (android.webkit.WebView)1 ImageView (android.widget.ImageView)1 ListAdapter (android.widget.ListAdapter)1 TextView (android.widget.TextView)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 JsonArrayRequest (com.android.volley.toolbox.JsonArrayRequest)1 SimpleDatabaseObjectCursorAdapter (com.dailystudio.app.widget.SimpleDatabaseObjectCursorAdapter)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1