Question

I am fighting since many days with a same problem as I am new. I am retrieving an array list from an object like ArrayList of Trainee because I want all the data to be stored in Singleton class. For Autocomplete TextView I want the array of list to be used to show in dropdown of AutoComplete Text View. For that I need a String of data. And I know that, String toString() method is used to do so. But I have a problem because I used the Trainee class already to get an arrayList of Trainee. So when I try to use that method the previous arraylist uses it. May be the code explains more. The problem is that I cannot see the dropdown in my autocompleteText view. My activity class:

SearchTrainee = (AutoCompleteTextView) findViewById(R.id.search);

    DatabaseHelper.getInstance();

    suggestions = DatabaseHelper.getInstance().getTraineesList();
    suggestionAdapter = new ArrayAdapter<Trainee>(this,
            android.R.layout.simple_dropdown_item_1line, suggestions);

    SearchTrainee.setAdapter(suggestionAdapter);
    SearchTrainee.setThreshold(1);

My DatabaseHelper class to retrieve getTrainingList;

public void searchTrainee() {

    this.setTraineesList(new ArrayList<Trainee>());
    Cursor cursor = db.query(TABLE_PERSON,
            new String[] { PERSON_ID, PERSON_FIRSTNAME, PERSON_LASTNAME,
                    PERSON_JOBTITLE, PERSON_EMAIL, PERSON_COMPANY,
                    PERSON_DEPARTMENT, PERSON_BADGE }, null, null, null,
            null, null);

    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            Log.i("HERE", "cursor moving...");
            this.traineesList
                    .add(new Trainee(
                        Integer.parseInt(cursor.getString(cursor
                                    .getColumnIndex(DatabaseHelper.PERSON_ID))),
                            cursor.getString(cursor
                                    .getColumnIndex(DatabaseHelper.PERSON_FIRSTNAME)),
                            cursor.getString(cursor
                                    .getColumnIndex(DatabaseHelper.PERSON_LASTNAME)),
                            cursor.getString(cursor
                                    .getColumnIndex(DatabaseHelper.PERSON_JOBTITLE)),
                            cursor.getString(cursor
                                    .getColumnIndex(DatabaseHelper.PERSON_EMAIL)),
                            cursor.getString(cursor
                                    .getColumnIndex(DatabaseHelper.PERSON_COMPANY)),
                            cursor.getString(cursor
                                    .getColumnIndex(DatabaseHelper.PERSON_DEPARTMENT)),
                            cursor.getString(cursor
                                    .getColumnIndex(DatabaseHelper.PERSON_BADGE))));
        }
    } else {
        this.traineesList.add(new Trainee(-1,
                new String("Create New User"), new String(), new String(),
                new String(), new String(), new String(), new String()));
    }
}

public ArrayList<Trainee> getTraineesList() {
    return traineesList;
}

public void setTraineesList(ArrayList<Trainee> traineesList) {
    this.traineesList = traineesList;
}
Was it helpful?

Solution

You must create your custom adapter and let it implement Filterable.

Here you can find a usefull training:

Custom Array Adapter for autocomplete

With this you can explaine the Trainee propreity thats match the filter on the search.

Code snipset (Game is your Trainee):

public class GameAdapter extends BaseAdapter implements Filterable {

    Context _context;
    ArrayList<Game> games;

    public GameAdapter(Context context, ArrayList<Game> _games) {
        _context = context;
        games = _games;
        orig = games;
       filter = new GameFilter();
   }

   @Override
   public int getCount() {
       if (games != null)
           return games.size();
       else
           return 0;
   }

   @Override
   public Object getItem(int arg0) {
       return games.get(arg0);
   }

   @Override
   public long getItemId(int arg0) {
       return games.get(arg0).getID();
   }

   @Override
   public View getView(int arg0, View arg1, ViewGroup arg2) {
       GameView gv;
       if (arg1 == null)
           gv = new GameView(_context, games.get(arg0));
       else {
           gv = (GameView) arg1;
           gv.setID(games.get(arg0).getID());
           gv.setName(games.get(arg0).getName());
       }
       return gv;
   }

   @Override
   public Filter getFilter() {
       return filter;
   }

   private GameFilter filter;
   ArrayList<Game> orig;

   private class GameFilter extends Filter {

       public GameFilter() {

       }

       @Override
       protected FilterResults performFiltering(CharSequence constraint) {
           FilterResults oReturn = new FilterResults();
           ArrayList<Game> results = new ArrayList<Game>();
           if (orig == null)
               orig = games;

           if (constraint != null)
           {
               if (orig != null && orig.size() > 0) {
                   for (Game g : orig) {
                       if (g.getName().contains(constraint))
                           results.add(g);
                   }
               }
               oReturn.values = results;
           }
           return oReturn;
       }

       @SuppressWarnings("unchecked")
       @Override
       protected void publishResults(CharSequence constraint, FilterResults results) {
           games = (ArrayList<Game>)results.values;
           notifyDataSetChanged();
       }
   }

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top