MultiAutoCompleteTextView
自定义适配器,如何dismissDropDown
我的代码点击监听器无效。
片段
public class ShareDialogFragment extends DialogFragment implements View.OnClickListener{
Dialog dialog;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
String articleID;
MultiAutoCompleteTextView myMultiAutoCompleteTextView;
TextView Cancel, share;
Context context;
public static ShareDialogFragment fragment;
OnClickListenerCustom onClickListenerCustom;
private PrefManager pref;
ArrayList<ShareWithArticle> userList = new ArrayList<>();
MultiAutoCompleteAdapter adapter;
RequestParse requestParse;
public ShareDialogFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.alert_dialog);
Bundle arg = getArguments();
if(arg!=null){
articleID = getArguments().getString("ArticleId");
}
dialog = new Dialog(getActivity());
requestParse = new RequestParse();
UserNameTo();
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
context = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_share_dialog, container, false);
Cancel = (TextView)view.findViewById(R.id.Cancel);
Cancel.setOnClickListener(this);
share = (TextView) view.findViewById(R.id.textView22);
myMultiAutoCompleteTextView = (MultiAutoCompleteTextView)view.findViewById(R.id.editText);
//ShareWithArticle s = (ShareWithArticle) myMultiAutoCompleteTextView.getAdapter().getItem(position);
return view;
}
public void UserNameTo(){
requestParse.postJson(ConfigApi.UserToArticle(), new RequestParse.VolleyCallBackPost() {
@Override
public void onSuccess(String result) {
userList = ShareArticleuserNameTo.parseResponse(result);
adapter = new MultiAutoCompleteAdapter(context,R.layout.custom_dropdown, userList);
myMultiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
myMultiAutoCompleteTextView.setAdapter(adapter);
myMultiAutoCompleteTextView.dismissDropDown();
}
@Override
public void onRequestError(String errorMessage) {
M.i("============errorMessage", errorMessage);
}
@Override
public Map OnParam(Map<String, String> params) {
return null;
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.Cancel:
getDialog().dismiss();
break;
}
}
}
和我的自定义适配器MultiAutoCompleteTextView
代码:
public class MultiAutoCompleteAdapter extends ArrayAdapter<ShareWithArticle> {
//ShareWithArticle
private final Context mContext;
private final List<ShareWithArticle> mDepartments;
private final List<ShareWithArticle> mDepartments_All;
private final List<ShareWithArticle> mDepartments_Suggestion;
private final int mLayoutResourceId;
private ImageLoader imageLoader;
private RequestQueue requestQueue;
private VolleySingleton volleySingleton;
public MultiAutoCompleteAdapter(Context context, int resource, List<ShareWithArticle> departments) {
super(context, resource, departments);
this.mContext = context;
this.mLayoutResourceId = resource;
this.mDepartments = new ArrayList<>(departments);
this.mDepartments_All = new ArrayList<>(departments);
this.mDepartments_Suggestion = new ArrayList<>();
}
public int getCount() {
return mDepartments.size();
}
public ShareWithArticle getItem(int position) {
return mDepartments.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(mLayoutResourceId, parent, false);
}
ShareWithArticle department = getItem(position);
TextView name = (TextView) convertView.findViewById(R.id.name);
ImageView userProfile = (ImageView) convertView.findViewById(R.id.profileImages);
name.setText(department.getUsrFNm() + " " + department.getUsrLNm());
volleySingleton = VolleySingleton.getInstance();
imageLoader = volleySingleton.getImageLoader();
requestQueue = volleySingleton.getRequestQueue();
String UsrPrf = department.getUsrPicture();
RequestParse.loadImages(UsrPrf, userProfile);
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
/*@Override
public boolean isEnabled(int position) {
return position % 2 == 0 ? false : true;
}*/
@Override
public Filter getFilter() {
return new Filter() {
@Override
public String convertResultToString(Object resultValue) {
return ((ShareWithArticle) resultValue).getUsrFNm()+ " " + ((ShareWithArticle) resultValue).getUsrLNm();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
mDepartments_Suggestion.clear();
for (ShareWithArticle department : mDepartments_All) {
if (department.getUsrFNm().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
mDepartments_Suggestion.add(department);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = mDepartments_Suggestion;
filterResults.count = mDepartments_Suggestion.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mDepartments.clear();
if (results != null && results.count > 0) {
// avoids unchecked cast warning when using mDepartments.addAll((ArrayList<Department>) results.values);
List<?> result = (List<?>) results.values;
for (Object object : result) {
if (object instanceof ShareWithArticle) {
mDepartments.add((ShareWithArticle) object);
}
}
} else if (constraint == null) {
// no filter, add entire original list back in
mDepartments.addAll(mDepartments_All);
}
notifyDataSetChanged();
}
};
}
}