Java导入错误"类型android.widget.Filter.FilterResults是不可见的" [英] java import error "the type android.widget.Filter.FilterResults is not visible"

查看:268
本文介绍了Java导入错误"类型android.widget.Filter.FilterResults是不可见的"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经被问:<一href=\"http://stackoverflow.com/questions/18135039/the-type-android-widget-filter-filterresults-is-not-visible\">the键入android.widget.Filter.FilterResults是不可见的
但是,有没有明确的答案,现在我得到了同样的问题。在讨论有关于变量提到的某些东西被标记为final时,他们不应该是用getFilter ...
嗯,这里是我的code:

This question was already asked: the type android.widget.Filter.FilterResults is not visible But there was no clear answer, and now I'm getting the same problem. In that discussion there was something mentioned about variables being marked as final when they shouldn't be for the getFilter... Well, here is my code:

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();
                }
            }
        }
    }

}

这是code的主要部分,我切出了对这个问题不感兴趣的其他部分(按键和触摸他们的听众)。
我除了在进口的android.widget.Filter.FilterResults没有错误;
我曾尝试服用适配器的最终了,但给我的错误说,他们必须是最终的。我曾尝试以最后的落的ArrayList的,但这并没有改变什么,还是在进口式android.widget.Filter.FilterResults不可见的错误。
还有什么可问题呢?

This is the main part of the code, I cut out other parts (buttons and their touch listeners) that are less interesting to this problem. I have no errors except on the import for android.widget.Filter.FilterResults; I have tried taking "final" off of the adapters, but that gives me errors saying that they have to be final. I have tried taking "final" off of the ArrayLists, but that didn't change anything, still have the error in the import "the type android.widget.Filter.FilterResults is not visible". What else can be wrong with this?

我用我自己的自定义项目类:

I'm using my own custom Item class:

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;
    }
}

和我不能在其他职位什么解决办法是问,因为我不就在这里有足够的信誉作评论,唉。

And I can't ask on the other post what the solution was cause I don't have enough "reputation" on here to make comments yet, ugh.

推荐答案

即将回到这个将其标记为回答,从原来的问题我的previous评论:

Coming back to this to mark it as answered, from my previous comment on the original question:

Eclipse中自动添加额外的不必要的进口android.widget.Filter.FilterResults;经过进口android.widget.Filter。这足以有进口android.widget.Filter,所以只需注释掉或删除Filter.FilterResults第二进口。
有一次,我这样做,有没有更多的错误,一切都按预期工作。

Eclipse automatically adds that extra unneeded "import android.widget.Filter.FilterResults;" after "import android.widget.Filter". It's enough to have "import android.widget.Filter", so just comment out or delete the second import for Filter.FilterResults. Once I did that there were no more errors and all was working as expected.

这篇关于Java导入错误&QUOT;类型android.widget.Filter.FilterResults是不可见的&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆