很多天以来,我都在为一个和我刚出生时一样的问题而战。我正在从一个像arraylistoftrainee这样的对象中检索一个数组列表,因为我希望所有的数据都存储在singleton类中。
对于自动完成文本视图,我希望在自动完成文本视图的下拉列表中显示列表数组。为此,我需要一系列数据。我知道,string tostring()方法是用来做这件事的。但我有一个问题,因为我已经用了见习班,得到了一份见习名单。因此,当我尝试使用该方法时,前面的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;
}
最佳答案
您必须创建自定义适配器并让它实现可过滤性。
在这里您可以找到有用的培训:
Custom Array Adapter for autocomplete
有了这个,你可以解释与搜索过滤器匹配的受训者。
代码snipset(游戏是你的受训者):
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();
}
}
}