gpt4 book ai didi

android - 从对象的 ArrayList 中获取字符串以用于自动完成 TextView

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:14 27 4
gpt4 key购买 nike

我和新手一样,因为同样的问题而奋斗了很多天。我正在从像 Trainee 的 ArrayList 这样的对象中检索数组列表,因为我希望所有数据都存储在 Singleton 类中。对于自动完成 TextView ,我希望列表数组用于显示在自动完成 TextView 的下拉列表中。为此,我需要一串数据。而且我知道,String toString() 方法用于执行此操作。但是我有一个问题,因为我已经使用了 Trainee 类来获取 Trainee 的 arrayList。因此,当我尝试使用该方法时,先前的 arraylist 会使用它。可能是代码解释更多。问题是我在 autocompleteText View 中看不到下拉菜单。我的 Activity 课:

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

最佳答案

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

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

Custom Array Adapter for autocomplete

有了这个,您可以解释与搜索过滤器相匹配的实习生素质。

代码 fragment (游戏是你的实习生):

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

关于android - 从对象的 ArrayList 中获取字符串以用于自动完成 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21156183/

27 4 0