
这个问题已经被问到:the type android.widget.Filter.FilterResults is not visible
但是没有明确的答案,现在我遇到了同样的问题.在那个讨论中,有一些关于变量被标记为final的内容,当它们不应该用于getFilter时……
好吧,这是我的代码:
package com.example.project;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Locale;import androID.app.Activity;import androID.content.Context;import androID.content.Intent;import androID.content.SharedPreferences;import androID.content.res.Configuration;import androID.content.res.Resources;import androID.os.Bundle;import androID.preference.PreferenceManager;import androID.support.v4.app.NavUtils;import androID.text.Editable;import androID.text.TextWatcher;import androID.util.displayMetrics;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OntouchListener;import androID.vIEw.VIEwGroup;import androID.Widget.AdapterVIEw;import androID.Widget.ArrayAdapter;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Filter;import androID.Widget.Filter.FilterResults;import androID.Widget.Filterable;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class PlacesMain extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreferenceManager.setDefaultValues(this, R.xml.preferences, false); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); String localePref = sharedPref.getString("pref_locale", ""); Configuration conf = getResources().getConfiguration(); conf.locale = new Locale(localePref); displayMetrics metrics = new displayMetrics(); getwindowManager().getDefaultdisplay().getMetrics(metrics); Resources resources = new Resources(getAssets(), metrics, conf); setContentVIEw(R.layout.activity_places_main); final ListVIEw ListvIEw = (ListVIEw) findVIEwByID(R.ID.ListVIEw1); final EditText edittext = (EditText) findVIEwByID(R.ID.editText1); String[] galilee = getResources().getStringArray(R.array.galilee_places); String[] judea = getResources().getStringArray(R.array.judea_places); String[] galilee_en = new String[galilee.length]; String[] judea_en = new String[judea.length]; if(localePref != "en"){ Locale current = conf.locale; conf.locale = new Locale("en"); metrics = new displayMetrics(); getwindowManager().getDefaultdisplay().getMetrics(metrics); Resources resources_en = new Resources(getAssets(), metrics, conf); galilee_en = resources_en.getStringArray(R.array.galilee_places); judea_en = resources_en.getStringArray(R.array.judea_places); conf.locale = current; getwindowManager().getDefaultdisplay().getMetrics(metrics); resources = new Resources(getAssets(), metrics, conf); } else{ galilee_en = galilee; judea_en = judea; } final ArrayList<Item> galileeArrayList = new ArrayList<Item>(); final ArrayList<Item> judeaArrayList = new ArrayList<Item>(); final ArrayList<Item> allArrayList = new ArrayList<Item>(); for (int i = 0; i < galilee.length; ++i) { galileeArrayList.add(new Item(galilee[i],galilee_en[i],i)); allArrayList.add(new Item(galilee[i],galilee_en[i],i)); } for (int i = 0; i < judea.length; ++i) { judeaArrayList.add(new Item(judea[i],judea_en[i],i)); allArrayList.add(new Item(judea[i],judea_en[i],i)); } Collections.sort(galileeArrayList,new Comparator<Item>(){ public int compare(Item o1, Item o2){ return o1.getPlace().compareto(o2.getPlace()); } }); Collections.sort(judeaArrayList,new Comparator<Item>(){ public int compare(Item o1, Item o2){ return o1.getPlace().compareto(o2.getPlace()); } }); Collections.sort(allArrayList,new Comparator<Item>(){ public int compare(Item o1, Item o2){ return o1.getPlace().compareto(o2.getPlace()); } }); final ItemAdapter all_adapter = new ItemAdapter(this, androID.R.layout.simple_List_item_1,allArrayList); final ItemAdapter galilee_adapter = new ItemAdapter(this, androID.R.layout.simple_List_item_1,galileeArrayList); final ItemAdapter judea_adapter = new ItemAdapter(this, androID.R.layout.simple_List_item_1,judeaArrayList); ListvIEw.setTextFilterEnabled(true); ListvIEw.setAdapter(all_adapter); edittext.addTextChangedListener(new TextWatcher(){ @OverrIDe public voID onTextChanged(CharSequence s, int start, int before, int count) { // Todo auto-generated method stub String mytext = edittext.getText().toString(); all_adapter.getFilter().filter(s); } @OverrIDe public voID beforeTextChanged(CharSequence s, int start, int count, int after) { // Todo auto-generated method stub } @OverrIDe public voID afterTextChanged(Editable s) { // Todo auto-generated method stub } }); } private class ItemAdapter extends ArrayAdapter<Item> implements Filterable { private final Object mlock = new Object(); private ItemsFilter mFilter; private ArrayList<Item> mItems; public ItemAdapter(Context context, int textVIEwResourceID, ArrayList<Item> mItems) { super(context, textVIEwResourceID, mItems); this.mItems = mItems; } @OverrIDe public VIEw getVIEw (int position, VIEw convertVIEw, VIEwGroup parent) { VIEw vIEw = super.getVIEw(position, convertVIEw, parent); Item i = mItems.get(position); if(i != null){ TextVIEw text1 = (TextVIEw) vIEw.findVIEwByID(androID.R.ID.text1); text1.setText(i.getPlace()); vIEw.setTag(i.getPlaceEn()); } return vIEw; } public Filter getFilter(){ if (mFilter == null){ mFilter = new ItemsFilter(); } return mFilter; } private class ItemsFilter extends Filter { protected FilterResults performFiltering(CharSequence prefix) { //Initiate our results object FilterResults results = new FilterResults(); // If the adapter array is empty, check the actual items array and use it if (mItems == null) { synchronized (mlock) { // Notice the declaration above mItems = new ArrayList<Item>(); } } // If no prefix is sent to the filter we'll send back the original array if(prefix == null || prefix.length() == 0){ synchronized (mlock) { results.values = mItems; results.count = mItems.size(); } } else{ // compare lower case strings String prefixString = prefix.toString().tolowerCase(); ArrayList<Item> items = mItems; final int count = items.size(); final ArrayList<Item> newItems = new ArrayList<Item>(count); for (int i = 0; i < count; i++){ final Item item = items.get(i); final String itemPlace = item.getPlace().tolowerCase(); // First match against the whole, non splitted value if (itemPlace.startsWith(prefixString)){ // Todo this index won't be correct, need separate index from loop increment newItems.add(new Item(item.getPlace(),item.getPlaceEn(),i)); } else{ } } // Set and return results.values = newItems; results.count = newItems.size(); } return results; } @SuppressWarnings("unchecked") protected voID publishResults(CharSequence prefix, FilterResults results){ //noinspection unchecked mItems = (ArrayList<Item>) results.values; // Let the adapter kNow about the updated List if(results.count > 0){ notifyDataSetChanged(); } else{ notifyDataSetInvalIDated(); } } } }}这是代码的主要部分,我删除了对此问题不太感兴趣的其他部分(按钮及其触摸侦听器).
我没有错误,除了导入androID.Widget.Filter.FilterResults;
我试过从适配器上取下“最终”,但这给了我错误,说他们必须是最终的.我已经尝试从ArrayLists中取出“final”,但是没有改变任何东西,导入时仍然有错误“类型androID.Widget.Filter.FilterResults不可见”.
还有什么可能是错的?
我正在使用自己的自定义Item类:
public class Item { private String place; private String placeEn; private int ID; public Item(String place, String placeEn, int ID) { this.place = place; this.placeEn = placeEn; this.ID = ID; } public String getPlace() { return place; } public String getPlaceEn() { return placeEn; } public int getID(){ return ID; } public voID setPlace(String place){ this.place = place; } public voID setPlaceEn(String placeEn){ this.placeEn = placeEn; } public voID setID(int ID){ this.ID = ID; }}我不能在另一篇文章中询问解决方案是什么原因我在这里没有足够的“声誉”来发表评论,呃.
解决方法:
从我之前对原始问题的评论中回过头来标记它已回答:
Eclipse自动添加额外不需要的“import androID.Widget.Filter.FilterResults;”在“import androID.Widget.Filter”之后.它足以拥有“import androID.Widget.Filter”,所以只需注释掉或删除Filter.FilterResults的第二个导入.
一旦我这样做,就没有更多错误,所有错误都按预期工作.
以上是内存溢出为你收集整理的java导入错误“android.widget.Filter.FilterResults类型不可见”全部内容,希望文章能够帮你解决java导入错误“android.widget.Filter.FilterResults类型不可见”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)