2016-03-02 3 views
0

J'inclus une recherche en suivant la réponse à cette question. J'ai créé un élément de suggestion personnalisé qui inclut un texte et un ImageButton. Le code important est ci-dessous:Traitement des clics d'audit avec suggestion personnalisée item

Filtrer l'activité, dans cette classe la vue de recherche est initialisée, et j'ai les écouteurs OnQuery.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() 
{ 
    @Override 
    public boolean onQueryTextSubmit(String query) 
    { 
     if (Utils.isQueryOk(query)) 
     { 
      .... 
     } 

     return true; 
    } 

    @Override 
    public boolean onQueryTextChange(String newText) 
    { 
     if (newText.length() > 1) 
     { 
      new getSuggestionsFromServer().execute(newText); 
     } 

     return true; 
    } 
}); 

private class getSuggestionsFromServer extends AsyncTask<String, Void, Void> 
{ 
    List<String> suggestions; 

    @Override 
    protected Void doInBackground(String... params) 
    { 
     // Retrieve suggestions from server 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void unused) 
    { 
     String[] columns = new String[] { "_id", "text" }; 
     Object[] temp = new Object[] { 0, "default" }; 

     MatrixCursor cursor = new MatrixCursor(columns); 

     for(int i = 0; i < suggestions.size(); i++) 
     { 
      temp[0] = i; 
      temp[1] = suggestions.get(i); 

      cursor.addRow(temp); 
     } 

     SearchManager manager = (SearchManager)getSystemService(Context.SEARCH_SERVICE); 

     final SearchView search = (SearchView)mMenu.findItem(R.id.menu_item_search).getActionView(); 

     search.setOnSuggestionListener(new SearchView.OnSuggestionListener() 
     { 
      @Override 
      public boolean onSuggestionSelect(int position) 
      { 
       return true; 
      } 

      @Override 
      public boolean onSuggestionClick(int position) 
      { 
       // This is not getting called! 

       Log.d(TAG, "SuggestClick: " + search.getSuggestionsAdapter().getCursor().getString(1)); 

       return true; 
      } 
     }); 

     search.setSuggestionsAdapter(new SuggestionAdapter(FilterUI.this, cursor, suggestions)); 
     search.getSuggestionsAdapter().notifyDataSetChanged(); 
    } 

} /* [END getSuggestionsFromServer] */ 

classe SuggestionAdapter

public class SuggestionAdapter extends CursorAdapter 
{ 
    private List<String> items; 
    private TextView text; 
    private ImageButton include; 

    public SuggestionAdapter(Context context, Cursor cursor, List<String> items) 
    { 
     super(context, cursor, false); 

     this.items = items; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) 
    { 
     text.setText(items.get(cursor.getPosition())); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) 
    { 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View view = inflater.inflate(R.layout.suggestion_item, parent, false); 

     text = (TextView)view.findViewById(R.id.suggestion_item); 
     include = (ImageButton)view.findViewById(R.id.suggestion_include); 

     return view; 
    } 
} 

Suggestion article XML

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@drawable/suggestion_bottom_border" 
     android:layout_marginLeft="16dp"> 

     <TextView 
      android:id="@+id/suggestion_item" 
      android:layout_width="0dp" 
      android:layout_weight="0.8" 
      android:layout_height="wrap_content" 
      android:textSize="14dp" 
      android:padding="16dp" 
      android:textColor="@color/colorMediumText"/> 

     <ImageButton 
      android:id="@+id/suggestion_include" 
      android:layout_width="24dp" 
      android:layout_height="24dp" 
      android:layout_margin="16dp" 
      android:scaleType="centerCrop" 
      android:src="@drawable/ic_call_made_black" 
      android:alpha="0.4" 
      android:background="@android:color/transparent"/> 

    </LinearLayout> 

</LinearLayout> 

Alors, ce que je veux faire est d'écouter si elle est cliqué sur le textview, ou ImageButton. Comment puis-je atteindre cet objectif?

Répondre

0
public class SuggestionAdapter extends CursorAdapter 
{ 
    private List<String> items; 
    private TextView text; 
    private ImageButton include; 

    public SuggestionAdapter(Context context, Cursor cursor, List<String> items) 
    { 
     super(context, cursor, false); 

     this.items = items; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) 
    { 
     text.setText(items.get(cursor.getPosition())); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) 
    { 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View view = inflater.inflate(R.layout.suggestion_item, parent, false); 

     text = (TextView)view.findViewById(R.id.suggestion_item); 
     include = (ImageButton)view.findViewById(R.id.suggestion_include); 

     return view; 
    } 
} 


public class Filter extends Activity{ 


searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() 
{ 
    @Override 
    public boolean onQueryTextSubmit(String query) 
    { 
     if (Utils.isQueryOk(query)) 
     { 
      .... 
     } 

     return true; 
    } 

    @Override 
    public boolean onQueryTextChange(String newText) 
    { 
     if (newText.length() > 1) 
     { 
      new getSuggestionsFromServer().execute(newText); 
     } 

     return true; 
    } 
}); 

private class getSuggestionsFromServer extends AsyncTask<String, Void, Void> 
{ 
    List<String> suggestions; 

    @Override 
    protected Void doInBackground(String... params) 
    { 
     // Retrieve suggestions from server 

     return null; 
    } 

} 


Implemet Suggesion Adapter within the Filter Activity 
+0

Pourriez-vous élaborer votre réponse? – cuoka