从自动完成文本视图的对象的ArrayList中获取String

时间:2014-01-16 07:54:16

标签: android arraylist sqlite autocompletetextview

我和很多天都在战斗,因为我和新人有同样的问题。我正在从像Trabine的ArrayList这样的对象中检索数组列表,因为我希望所有数据都存储在Singleton类中。 对于自动完成TextView,我希望使用列表数组在自动完成文本视图的下拉列表中显示。为此,我需要一个数据字符串。我知道, String toString()方法用于执行此操作。但是我有一个问题,因为我已经使用了Trainee类来获得Trainee的arrayList。因此,当我尝试使用该方法时,之前的arraylist使用它。可能是代码解释更多。问题是我无法在autocompleteText视图中看到下拉列表。 我的活动课程:

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);

我的DatabaseHelper类检索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;
}

1 个答案:

答案 0 :(得分:1)

您必须创建自定义适配器并让它实现Filterable。

在这里你可以找到一个有用的培训:

Custom Array Adapter for autocomplete

通过这种方式,您可以解释与搜索过滤器匹配的实习生特性。

代码片段(游戏是您的实习生):

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();
       }
   }

}