在Android的扩展ArrayAdapter [英] Extending ArrayAdapter in android

查看:203
本文介绍了在Android的扩展ArrayAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从类中重写一个用getFilter()方法 ArrayAdapter ,我发现源$ C ​​$ c从<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java">here在github上

I need to override a getFilter() method from the class ArrayAdapter and i found the source code from here in the github

//package name

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;

public class CustomAdapter<T> extends ArrayAdapter<T> implements Filterable{

    private ArrayList<T> mOriginalValues;
    private List<T> mObjects;
    private CustomFilter mFilter;
    private final Object mLock = new Object();
    public CustomAdapter(Context context, int textViewResourceId, T[] objects) {
        super(context, textViewResourceId, objects);

        mObjects = Arrays.asList(objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        if (mFilter == null) {
            mFilter = new CustomFilter();
        }
        return mFilter;
    }


    private class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();
            Log.d("bajji", "its ---> " + prefix);
            if (mOriginalValues == null) {
                synchronized (mLock) {
                    mOriginalValues = new ArrayList<T>(mObjects);
                }
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList<T> list;
                synchronized (mLock) {
                    list = new ArrayList<T>(mOriginalValues);
                }
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList<T> values;
                synchronized (mLock) {
                    values = new ArrayList<T>(mOriginalValues);
                }

                final int count = values.size();
                final ArrayList<T> newValues = new ArrayList<T>();
                final ArrayList<T> approxValues = new ArrayList<T>();
                final ArrayList<T> secondApproxValues = new ArrayList<T>();


                for (int i = 0; i < count; i++) {
                    final T value = values.get(i);
                    final String valueText = value.toString().toLowerCase();
                    boolean flag = true;
                    // First match against the whole, non-splitted value
                    if (valueText.startsWith(prefixString)) {
                        newValues.add(value);
                        flag = false;
                    } else {
                        final String[] words = valueText.split(" ");
                        final int wordCount = words.length;

                        // Start at index 0, in case valueText starts with space(s)
                        for (int k = 0; k < wordCount; k++) {
                            if (words[k].startsWith(prefixString)) {
                                newValues.add(value);
                                flag = false;
                                break;
                            } 
                        }
                    }

                    if(flag) {
                        if(approxMatch(valueText, prefixString) <= 3) { //change the stuff and do a levi work
                            approxValues.add(value);
                        }
                        else {
                            final String[] words = valueText.split(" ");
                            final int wordCount = words.length;

                            // Start at index 0, in case valueText starts with space(s)
                            for (int k = 0; k < wordCount; k++) {
                                if(approxMatch(words[k], prefixString) <= 3) {
                                    //leve work
                                    secondApproxValues.add(value);
                                    break;
                                }
                            }
                        }
                    }
                }
                newValues.addAll(approxValues);
                newValues.addAll(secondApproxValues);
                results.values = newValues;
                results.count = newValues.size();
            }
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            //noinspection unchecked
            mObjects = (List<T>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }

    private int approxMatch (String s, String t) {
          // an approxmimate string matching algo
          return p;
    }
}

问题是用getFilter方法​​具有私有内部类的一个对象 ArrayFilter 其中有一个方法 peformFiltering 和我需要把不同的code有,所以我必须在此改变类。和我的方法异常。

The problem is the getFilter method has a object of a private inner class ArrayFilter which has a method peformFiltering and i need to put a different code there so i have to overide the class. And i get an exception in the method.

在派生类中延伸ArrayAdapter我创建了类似于 ArrayFilter 私有内部类,并把它称为 MyFilter 我再次得到了同样的异常的方法 performFiltering

In the derived class which extends ArrayAdapter i created a private inner class which is similar to ArrayFilter and called it MyFilter and i get the same exception again in the method performFiltering.

我找到了一个解决方案来解决我的问题。我复制了所有的code。在 ArrayAdapter 类,并创建了一个新的类名为 MyAdapter ,我改变了一些$ C内部类 ArrayFilter 键,应用程序需要在$ c ++工程,我想它的方式。但我觉得它不是最好的解决方案。

I found a solution to solve my problem. I copied all the code in ArrayAdapter class and created a new class called MyAdapter and i altered some code inside the inner class ArrayFilter and the app works the way i wanted it to. But i feel its not the best solution.

Android有不同的API层次所以如果阵列适配器在不同的API层次改变,那么我一定要加入我的codeS这些变化。所以我觉得最好的办法是延长类 ArrayAdapter 以创建 MyAdapter ,而不仅仅是复制和粘贴$ C从$ c中的 ArrayAdapter

Android has various api levels so if the array adapter is changed in different api level then i have to add those changes in my codes to. So i feel the best way is to extend the class ArrayAdapter to create MyAdapter rather than just copying and pasting the code from the ArrayAdapter

我如何可以覆盖父类的内部私有类..?

How can i override the inner private class of a parent class..?

编辑:我得到的例外..

The exception i get..

EDIT2:现在我加满code中的问题。和它完美的作品,如果我复制和编辑阵列适配器......问题是,只有当我向..!现在code和搜索工作完美。我检查与 Log.i ..但下拉列表中的用户界面自动完成建议是行不通的..我只得到了第一个角色,我键入下一个字符过滤发生,但UI更新不会发生。

Now i added the full code in the question. and it works perfectly if i copy and edit the array adapter.. the problem is only when i extend..!! now the code and search is working perfectly. I checked it with Log.i.. but the drop down list for auto complete suggestion in UI is not working.. i only get for the first character i type the next character filtering takes place but UI update is not taking place.

推荐答案

从计算器社区一些帮助后,我删除了异常,后来我发现,返回的建议,并没有真正改变,因为 mObjects 从超类或基类返回。这个问题是有两个公共方法 getCount将()的getItem(INT位置)这得到计数和取从基类mObjects列表。所以,我只需要添加这两种方法在我的课。

After some help from stackoverflow community i removed the exception and later i found out that the suggestions that are returned doesn't really change because the mObjects was returned from the super class or base class. The problem was the there were two public methods getCount() and getItem(int position) which gets the count and fetches the list from mObjects from the base class. So i just have to add those two methods in my class..

public int getCount() {
    return mObjects.size();
}

public T getItem(int position) {
    return mObjects.get(position);
}

现在 mObjects 派生类的将被退回。它们是更新在下拉列表中的用户界面。

Now mObjects of the derived class will be returned. Which are the updated in the dropdown list in the UI.

我不是一个Java专家,但是这解决了我的问题。如果您有任何建议,使code更好,请添加它在评论或随意编辑答案!

I am not a java expert, but this solves my problem. If you have any suggestions to make the code better please add it in comments or feel free to edit the answer.!

这篇关于在Android的扩展ArrayAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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