Android AutoCompleteTextView自定义下载来自Web的数据

时间:2012-07-14 06:55:29

标签: android autocompletetextview

我使用AutoCompleteTextView来处理默认下拉菜单。我现在正试图弄清楚我是否可以获得自定义下拉菜单来处理我的数据。我感觉非常接近,但我无法将下拉显示出来。 这是我想要做的。

我创建了一个自定义适配器。

public class UserSearchAdapter extends ArrayAdapter<UserSearchItem> {

private Context context;
private LayoutInflater vi;
int count = 0;

public UserSearchAdapter(Context context) {
    super(context, 0, items);
    this.context = context;
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    final UserSearchItem i = items.get(position);
    if (i != null) {
        Log.d("adapter", "in here");
        UserSearchItem si = (UserSearchItem) i;
        v = vi.inflate(R.layout.user_search_item, null);
        final TextView title = (TextView) v
                .findViewById(R.id.tv_userSearchName);
        if (title != null)
            title.setText(si.userName);
    }

    return v;
}

}

这是我的UserSearchItem类

public class UserSearchItem {

public final String userName;
public final String userId;
public final String userFBId;

public UserSearchItem(String userName, String userId, String userFBId) {
    this.userName = userName;
    this.userId = userId;
    this.userFBId = userFBId;
}

}

这是我实现AutoCompleteTextView

的地方
userSearch = (AutoCompleteTextView)findViewById(R.id.tv_UserSearch);
    AutoCompleteAdapter = new UserSearchAdapter(this);
    userSearch.setAdapter(AutoCompleteAdapter);
    userSearch.addTextChangedListener(this);

    @Override
public void afterTextChanged(Editable s) {
    if (shouldAutoComplete) {
        new DoAutoCompleteSearch().execute(s.toString());
    }
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    shouldAutoComplete = true;
    for (int position = 0; position < AutoCompleteAdapter.getCount(); position++) {
        if (AutoCompleteAdapter.getItem(position).userName.equalsIgnoreCase(s.toString())) {
            shouldAutoComplete = false;
            break;
        }
    }

}

 private class DoAutoCompleteSearch extends AsyncTask<String, Void, ArrayList<UserSearchItem>> {


    @Override
    protected ArrayList<UserSearchItem> doInBackground(String... params) {
        ArrayList<UserSearchItem> autoComplete = new ArrayList<UserSearchItem>();

        //get info from DB (userName, userId, userFbId)

                  UserSearchItem item = new UserSearchItem(userName, userId, userFbId);
                  autoComplete.add(item);
              }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return autoComplete;
    }

    @Override
    protected void onPostExecute(ArrayList<UserSearchItem> result) {
        AutoCompleteAdapter.clear();
        for (UserSearchItem s : result)
        {
            AutoCompleteAdapter.add(s);
        }
    }
}

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

如果您仍在寻找答案:在构造函数中使用数据填充适配器:

   UserSearchAdapter adapter = new UserSearchAdapter(this, data);

在适配器类中添加一个构造函数,如下所示:

    public class UserSearchAdapter extends ArrayAdapter<UserSearchItem> {

    private Context context;
    private LayoutInflater vi;
    int count = 0;
    private List<UserSearchItem> mItems;

    public UserSearchAdapter(Context context) {
        super(context, 0, items);
        this.context = context;
        vi = (LayoutInflater) context
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public UserSearchAdapter(Context context, List<UserSearchItem> items) {
       super(context, 0, items);
       this.context = context;
       vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       mItems = items;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        final UserSearchItem i = mItems.get(position);
        if (i != null) {
           Log.d("adapter", "in here");
           UserSearchItem si = (UserSearchItem) i;
           v = vi.inflate(R.layout.user_search_item, null);
           final TextView title = (TextView) v
              .findViewById(R.id.tv_userSearchName);
        if (title != null)
            title.setText(si.userName);
     }

    return v;
   }

答案 1 :(得分:0)

## Initalization ##

AutocompleteTextview actv = (AutoCompleteTextView) view.findViewById(R.id.etSearchPlace);

## set list response of your web API in adapter ##

SpringArrayAdapter spinnerArrayAdapter = new CustomAdapter<String>(mcontext,android.R.layout.simple_spinner_item, nameList);

## set adapter to Autocomplete Textview ##

actv.setAdapter(spinnerArrayAdapter);


## set your drop down layout in custom adapter##
 public class CustomAdapter<T> extends ArrayAdapter<T> {
        public CustomAdapter(Context context, int textViewResourceId,
                             List<T> objects) {
            super(context, textViewResourceId, objects);

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = super.getView(position, convertView, parent);

            if (view instanceof TextView) {
                ((TextView) view).setTextSize(15);
                ((TextView) view).setTransformationMethod(null);
                Typeface typeface = ResourcesCompat.getFont(parent.getContext(), R.font.montesemibold);

                ((TextView) view).setTypeface(typeface);
            }
            return view;
        }
    }