I have an adapter class which uses the getFilter
function within a ListView
. I wanted to modify it so that when the search is empty, the EditText text color becomes red. I have the following code which doesn't FC my app but the return string is blank instead of what is in the EditText
My partial adapter class:
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
data = (ArrayList<SetRows>)results.values;
if (data.isEmpty()) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.main, null);
EditText myTextView = (EditText) myView.findViewById(R.id.etSearch);
myTextView.setTextColor(Color.RED);
Toast.makeText(getContext(), myTextView.getText().toString(), 2000).show();
}
notifyDataSetChanged();
clear();
for(int i = 0, l = data.size(); i < l; i++)
add(data.get(i));
notifyDataSetInvalidated();
}
How do I modify the code so that I can achieve the result?
Try
... myTextView.getEditable().toString()
, sometimes it worksNOTE: The purpose of baseAdapters is to set data on your listview. So the checking must be done on the class where the EditText has been declared.
By utilizing the TextWatcher interface which checks the updated value of your EditText,
I don't think that the code involving
LayoutInflater
is doing what you think it is doing.inflater.inflate(R.layout.main, null)
will not get you an existing instance ofR.layout.main
; rather it uses the XML layout defined in main.xml to generate an entirely new View tree.Assuming your main Activity is calling
setContentView(R.layout.main)
, callinginflater.inflate(R.layout.main, null)
will result in you have two completely separate instances of that layout in memory. A reference to an EditText in one of these trees will be in no way related to the reference in the other tree.Once you have inflated the second View tree, you don't seem to do anything with it, so it is never displayed to the user. The user is still interacting with the layout you created in your Activity, while you are trying to get data from this second invisible tree.
There are a number of ways to do this, but without knowing how
publishResults()
is related to your Activity, it is difficult to recommend one. Here are some ideas:publishResults()
method