ArrayAdapter需要明确,即使我创建一个新的 [英] ArrayAdapter need to be clear even i am creating a new one
问题描述
您好我有理解一个ArrayAdapter是如何工作的问题。我的code是工作,但我不知道如何(的http:// amy-mac.com/images/2013/$c$c_meme.jpg )
Hello I'm having problems understanding how the ArrayAdapter works. My code is working but I dont know how.(http://amy-mac.com/images/2013/code_meme.jpg)
我有我的活动,在它里面,我有2私有类:
I have my activity, inside it i have 2 private classes.:
public class MainActivity extends Activity {
...
private void SomePrivateMethod(){
autoCompleteTextView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, new ArrayList<String>(Arrays.asList(""))));
autoCompleteTextView.addTextChangedListener(new MyTextWatcher());
}
...
private class MyTextWatcher implements TextWatcher {
...
}
private class SearchAddressTask extends AsyncTask<String, Void, String[]> {
...
}
}
现在我textwatcher类中我调用搜索地址的任务:
Now inside my textwatcher class i call the search address task:
@Override
public void afterTextChanged(Editable s) {
new SearchAddressTask().execute(s.toString());
}
到目前为止好。
在我SearchAddressTask我做一些东西在doInBackground()返回正确的数组。
In my SearchAddressTask I do some stuff on doInBackground() that returns the right array.
在所述onPostExecute()方法我尝试只需修改AutoCompleteTextView适配器从doInBackground(得到的数组添加的值),但适配器不能修改
On the onPostExecute() method i try to just modify the AutoCompleteTextView adapter to add the values from the array obtained in doInBackground() but the adapter cannot be modified:
不工作code:
protected void onPostExecute(String[] addressArray) {
ArrayAdapter<String> adapter = (ArrayAdapter<String>) autoCompleteDestination.getAdapter();
adapter.clear();
adapter.addAll(new ArrayList<String>(Arrays.asList(addressArray)));
adapter.notifyDataSetChanged();
Log.d("SearchAddressTask", "adapter isEmpty : " + adapter.isEmpty()); // Returns true!!??!
}
我不明白为什么这是行不通的。即使我在UI线程中运行它...
I dont get why this is not working. Even if i run it on UI Thread...
我一直在调查,如果我重新创建一个ArrayAdapter,在UI(显示的建议)的工作,但我仍然需要清除旧的适配器:
I kept investigating, if i recreate the arrayAdapter, is working in the UI (Showing the suggestions), but i still need to clear the old adapter:
工作code:
protected void onPostExecute(String[] addressArray) {
ArrayAdapter<String> adapter = (ArrayAdapter<String>) autoCompleteDestination.getAdapter();
adapter.clear();
autoCompleteDestination.setAdapter(new ArrayAdapter<String>(NewDestinationActivity.this,android.R.layout.simple_spinner_dropdown_item, new ArrayList<String>(Arrays.asList(addressArray))));
//adapter.notifyDataSetChanged(); // no needed
Log.d("SearchAddressTask", "adapter isEmpty : " + adapter.isEmpty()); // keeps returning true!!??!
}
所以我的问题是,什么是真正与这个ArrayAdapter发生?为什么我不能修改它在我onPostExecute()?为什么工作中的UI,如果我重新适配器?为什么我需要清除旧的适配器呢?
So my question is, what is really happening with this ArrayAdapter? why I cannot modify it in my onPostExecute()? Why is working in the UI if i am recreating the adapter? and why i need to clear the old adapter then?
我不知道有这么多,我需要一些帮助的问题在这里!
I dont know there are so many questions that I need some help in here!!
谢谢!
推荐答案
我跟着pskink的recomendation,非常感谢!
I followed the recomendation of pskink, Thanks a lot!!
我切换都人的职责,以一个ArrayAdapter和实施自己的过滤器。
I switched all the responsability to the ArrayAdapter and implemented my own as filter.
import java.util.ArrayList;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import com.roisoftstudio.travelalarm.gui.utils.StreetListHelper;
public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> mData;
private Context context;
public AutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.context = context;
mData = new ArrayList<String>();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int index) {
return mData.get(index);
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
mData = StreetListHelper.getStreetList(context, constraint.toString());
filterResults.values = mData;
filterResults.count = mData.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
}
};
return myFilter;
}
}
这篇关于ArrayAdapter需要明确,即使我创建一个新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!