有适配器设置为“站(名称/代码)AutocompleteTextView”。第一次设置整个列表。之后,如果我过滤了ch
,它将显示所有过滤的项目。
现在,如果我删除ch
,它将显示整个列表。这里没有问题。
但是现在,如果单击“后退”按钮或导航到其他屏幕并再次进入该屏幕,则仅显示过滤的项目(以ch开头)。
为什么只显示过滤的项目?
请帮助我。
这对我的项目非常重要。
我在Google上尝试了许多解决方案。但没有得到输出。
任何帮助将不胜感激。
这是我的自定义适配器:
public class CustomAdapter extends ArrayAdapter {
private final Context mContext;
private ArrayList<TextItem> textObjects;
private ArrayList<TextItem> textObjects_All;
private final int mLayoutResourceId;
View rowView;
private LayoutInflater inflater;
public CustomAdapter(Context mContext, int resource, ArrayList<TextItem> AObjects) {
super(mContext, resource, AObjects);
this.mContext = mContext;
mLayoutResourceId = resource;
setTextObjects(AObjects);
}
public void setTextObjects(ArrayList<TextItem> AObjects) {
try {
textObjects = AObjects;
textObjects_All = new ArrayList<>();
synchronized (this) {
textObjects_All.addAll(AObjects);
}
}catch (Exception e){
Log.d("eEmp/setTextObj", "Expt due to " + e.toString());
}
}
public int getCount() {
return textObjects.size();
}
public TextItem getItem(int position) {
return textObjects.get(position);
//return (textObjects != null ? textObjects.get(position) : "");
}
public long getItemId(int position) {
return position;
}
public void addTextObject(TextItem newItem) {
try {
if ((textObjects_All != null) && (!textObjects_All.contains(newItem))) {
textObjects_All.add(newItem);
}
} catch (Exception expt) {
Log.d("eEmp/setTextObj", "Expt due to " + expt.toString());
}
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
try {
rowView = convertView;
if (rowView == null){
inflater = LayoutInflater.from(mContext);
rowView = inflater.inflate(mLayoutResourceId, parent, false);
}
TextItem selectedItem = (TextItem) getItem(position);
TextView txtHeaderObject = (TextView) rowView.findViewById(R.id.tvHeaderItem);
txtHeaderObject.setText(selectedItem.headItemName);
TextView txtSubItem1Object = (TextView) rowView.findViewById(R.id.tvSub1Item);
if (txtSubItem1Object != null) {
txtSubItem1Object.setText(selectedItem.subItem1Name);
}
TextView txtSubItem2Object = (TextView) rowView.findViewById(R.id.tvSub2Item);
if (txtSubItem2Object != null) {
txtSubItem2Object.setText(selectedItem.subItem2Name);
}
ViewGroup.LayoutParams params = rowView.getLayoutParams();
return rowView;
} catch (Exception e) {
Log.e("/eEmp","/Convert View Exception"+e.toString());
e.printStackTrace();
}
return rowView;
}
private static class ViewHolder{
TextView Header;
TextView SubItem1;
TextView SubItem2;
}
@NonNull
@Override
public Filter getFilter() {
return new Filter() {
@Override
public CharSequence convertResultToString(Object resultValue) {
return ((TextItem) resultValue).headItemName;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {FilterResults filterResults = new FilterResults();
try {
if ((constraint == null) || (constraint.length() == 0)) {
synchronized (this) {
filterResults.values = textObjects_All;
filterResults.count = textObjects_All.size();
}
} else {
ArrayList<TextItem> textObjectsFiltered = new ArrayList<TextItem>();
for (TextItem obj : textObjects_All) {
TextItem itemObj = obj;
if (itemObj != null) {
if ((itemObj.headItemName != null) && (itemObj.headItemName.length() != 0)
&& (itemObj.headItemName.toLowerCase().startsWith(constraint.toString().toLowerCase()))) {
textObjectsFiltered.add(obj);
}
}
}
filterResults.values = textObjectsFiltered;
filterResults.count = textObjectsFiltered.size();
}
} catch (Exception expt) {
Log.d("eEmp/FilterResults", "Exception Occurred due to " + expt.toString());
}
return filterResults;
}
@SuppressWarnings("SingleStatementInBlock")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
try{
textObjects.clear();
if (results != null && results.count > 0) {
ArrayList<?> result = (ArrayList<?>) results.values;
for (Object object : result) {
if (object instanceof TextItem) {
textObjects.add((TextItem) object);
}
}
}
notifyDataSetChanged();
}catch (Exception expt) {
Log.d("eEmp/publishResults", "Exception Occurred due to " + expt.toString());
}
}
};
}
}
AutoCompleteTextWatcher.java
public class AutoCompleteTextWatcher implements TextWatcher {
private CustomAutoCompleteTextView currATV;
public AutoCompleteTextWatcher(CustomAutoCompleteTextView tv)
{
currATV = tv;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(currATV != null)
{
currATV.setTextItemTag(null);
}
}
}
atcvDivName是我的AutoCompleteTextView
活动
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.entry_fab_activity, container, false);
try {
Inst_Act_List_works_entry = new ArrayList<>();//added by pavani
status_pojo = new Status_POJO();
edtFromDate = (EditText) rootView.findViewById(R.id.edtFromDate);
edtFromDate.setText(getTodayDates()); // Added in V 1.3 by Shamili
// Get Available WorkTypes
WorkTypes = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.WorkTypes)));
actvDivName.addTextChangedListener(textWatcher());
//set event for clear button
btnClear_stn.setOnClickListener(onClickListener());
actvEntryCategory.addTextChangedListener(textWatcher_Entry());
btn_clear_EntryType.setOnClickListener(onClickListener());
btn_clear_Product.setOnClickListener(onClickListener());
actvPType.addTextChangedListener(textWatcher_Product());
cd_leaves.setVisibility(View.GONE);
if (stn_fal_falg == 1) {
btnClear_stn.setVisibility(View.GONE);
btn_clear_EntryType.setVisibility(View.GONE);
btn_clear_Product.setVisibility(View.GONE);
} else {
btnClear_stn.setVisibility(View.VISIBLE);
btn_clear_EntryType.setVisibility(View.VISIBLE);
btn_clear_Product.setVisibility(View.VISIBLE);
}
try {
WorksList = dbHandler.getWorkTypes();
for (String newWorkType : WorksList) {
if (!WorkTypes.contains(newWorkType)) {
WorkTypes.add(newWorkType);
}
}
} catch (Exception getWorkExp) {
}
EntryTypeAdapter = new CustomArrayAdapter(getActivity(), R.layout.autocomplete_text_layout, WorkTypes);
actvEntryCategory.setAdapter(EntryTypeAdapter);
actvEntryCategory.setThreshold(0);
Rlys = new ArrayList<Rly>();
try {
DivStnList = mContext.StnsPlacesList;
for (int i = 0; i < DivStnList.size(); i++) {
QRDivTextItemList = mContext.StnsPlacesList;
DivIDsList.add(DivStnList.get(i).did);
StnIDsList.add(DivStnList.get(i).id);
DivList.add(DivStnList.get(i).subItem2Name);
StnsList.add(DivStnList.get(i).headItemName);
}
} catch (Exception getDivExp) {
}
actvDivName.setThreshold(0);
actvDivName.addTextChangedListener(new AutoCompleteTextWatcher(actvDivName));
actvDivName.setOnItemClickListener(new AutoCompleteItemClickListener<TextItem>(QRDivTextItemList, actvDivName));
QRDivArrayAdapter = new CustomAdapter(mContext, R.layout.autocomplete_text_view, QRDivTextItemList);
actvDivName.setAdapter(QRDivArrayAdapter);
actvDivName.setCustomAutoCompleteTextViewListener(new CustomAutoCompleteTextView.CustomAutoCompleteTextViewListener() {
@Override
public void onItemSelected() {
try {
TextItem selectedItem = (TextItem) actvDivName.getTag();
InstRecyclerclear();
FailureRecyclerclear();
GeneralRecyclerclear();
actvPType.setText("");
ProdsItemList.clear();
actvDivName.setVisibility(View.VISIBLE);
actvPType.setVisibility(View.INVISIBLE);
cdAmc_entry.setVisibility(View.GONE);
trAMC.setVisibility(View.GONE);
if (selectedItem != null) {
instProd = (List<InstProd>) selectedItem.childObject;
for (int i = 0; i < instProd.size(); i++) {
TextItem item = new TextItem();
stn_pla_id = selectedItem.id;
item.headItemName = instProd.get(i).IName;
item.subItem1Name = instProd.get(i).MName;//String.valueOf(instProd.get(i).PID);
item.subItem2Name = instProd.get(i).NAME;//String.valueOf(instProd.get(i).IID);
item.id = instProd.get(i).PID;
item.did = instProd.get(i).IID;
prod_ID = instProd.get(i).PID;
ProdsItemList.add(item);
actvPType.setVisibility(View.VISIBLE);
actvPType.setEnabled(true);
}
}
QRProdArrayAdapter.setTextObjects(ProdsItemList);
} catch (Exception e) {
}
}
});
QRProdArrayAdapter = new CustomAdapter(mContext, R.layout.autocomplete_text_view, ProdsItemList);//added by pavani
actvPType.setAdapter(QRProdArrayAdapter);//added by pavani
actvPType.setThreshold(0);
actvPType.addTextChangedListener(new AutoCompleteTextWatcher(actvPType));
actvPType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
InstRecyclerclear();
FailureRecyclerclear();
GeneralRecyclerclear();
cdAmc_entry.setVisibility(View.GONE);
trAMC.setVisibility(View.GONE);
val = position;
iid = ProdsItemList.get(position).did;//Integer.parseInt(ProdsItemList.get(position).subItem2Name);
pid = ProdsItemList.get(position).id;//Integer.parseInt(ProdsItemList.get(position).subItem1Name);
station = ProdsItemList.get(position).headItemName;
product = ProdsItemList.get(position).subItem1Name;
} catch (Exception e) {
Log.d("eEmp/OnProdClick", "Exception Occurred due to " + e.toString());
}
}
});
actvDivName.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
entryFragment = (Entry_Fragment)
getFragmentManager().findFragmentByTag(EmpConstants.Entry_Info_Tag);
//set Station Name
if (StnIDFromAdapter != null) {
try {
long id = Long.parseLong(StnIDFromAdapter);
stn_pla_id = id;
for (int i = 0; i < DivStnList.size(); i++) {
if (id == DivStnList.get(i).id) {
actvDivName.setText(DivStnList.get(i).headItemName);
actvDivName.setEnabled(false);
}
}
} catch (Exception expt) {
}
}
//setting Product Name
if (ProdFromAdapter != null) {
try {
pid = ProdIdFromAdapter;
iid = ProdSno;
actvPType.setVisibility(View.VISIBLE);
actvPType.setText(ProdFromAdapter);
actvPType.setEnabled(false);
} catch (Exception expt) {
}
}
} catch (Exception expt) {
Log.d("eEmp/EntryCreate", "Exception Occurred due to " + expt.toString());
}
return rootView;
}
private TextWatcher textWatcher() {
return new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
if (!actvDivName.getText().toString().equals("")) {
btnClear_stn.setVisibility(View.VISIBLE);
} else {
}
} catch (Exception e) {
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
}