2

我想根据 Spinner 过滤到列表视图中。

例如,我有一个 ListView 的代码,它是

("Singapore Discovery Centre",
"Singapore Science Centre",
"Mint Museum",
"Singapore Art Museum",
"Army Museum"). 

我有一个 Spinner 的代码

("Centre", 
"Singapore",
"Museum").

我想要的是:单击新加坡后,在列表视图中,它将过滤掉所有“新加坡”

("Singapore Discovery Centre",
"Singapore Science Centre",
"Singapore Art Museum" ). 

我尝试了我的这个编码,

你能指导我,它有什么问题吗?

MainActivity.java

public abstract class MainActivity extends ListActivity implements OnItemSelectedListener {

    String[] Category = {
    "Singapore discovery Centre",
    "Singapore Science Centre",
    "Mint Museum",
    "Singapore Art Museum",
    "Army Museum"
};

String [] keywords = {
    "Centre",
    "Musuem",
    "Singapore",

};

Spinner s1;
ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ListView) findViewById(R.id.listView1);
        s1 = (Spinner) findViewById(R.id.spinner1);

        s1.setOnItemSelectedListener(this);

                setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Category)); 

                ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, keywords);

                aa.setDropDownViewResource(
                           android.R.layout.simple_spinner_dropdown_item);
                        s1.setAdapter(aa);
                    }

    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {
        listview.setFilterText(Category[position]);
    }


    public void onNothingSelected(AdapterView<?> parent) {
        listview.setFilterText("");
    }

public void onListItemClick(ListView parent, View v, int position,long id)
        {
            Toast.makeText(this, "You have selected " + Category[position], Toast.LENGTH_SHORT).show();
        }
        }

日志猫:

  07-29 14:43:30.429: E/AndroidRuntime(30599): FATAL EXCEPTION: main
    07-29 14:43:30.429: E/AndroidRuntime(30599): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.spinnertest/com.example.spinnertest.MainActivity}: java.lang.InstantiationException: com.example.spinnertest.MainActivity
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at android.os.Handler.dispatchMessage(Handler.java:99)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at android.os.Looper.loop(Looper.java:130)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at android.app.ActivityThread.main(ActivityThread.java:3691)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at java.lang.reflect.Method.invoke(Method.java:507)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at dalvik.system.NativeStart.main(Native Method)
    07-29 14:43:30.429: E/AndroidRuntime(30599): Caused by: java.lang.InstantiationException: com.example.spinnertest.MainActivity
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at java.lang.Class.newInstanceImpl(Native Method)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at java.lang.Class.newInstance(Class.java:1409)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at android.app.Instrumentation.newActivity(Instrumentation.java:1024)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
    07-29 14:43:30.429: E/AndroidRuntime(30599):    ... 11 more
4

1 回答 1

2

您将不得不使用自定义适配器和自定义过滤器。

删除abstractfor you 类。

行.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="TextView" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends ListActivity implements
        OnItemSelectedListener {

    String[] Category = { "Singapore discovery Centre",
            "Singapore Science Centre", "Mint Museum", "Singapore Art Museum",
            "Army Museum" };

    String[] keywords = { "Centre", "Museum", "Singapore",

    };
    ArrayList<String> cat = new ArrayList<String>();
    ArrayList<String> mPostingData = new ArrayList<String>();
    Spinner s1;
    ListView listview;
    CustomAdapter cus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ListView) findViewById(R.id.listView1);
        s1 = (Spinner) findViewById(R.id.spinner1);
        for (int i = 0; i < Category.length; i++) {
            cat.add(Category[i]);
        }

        // aa = new
        // ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Category);
        s1.setOnItemSelectedListener(this);
        mPostingData = cat;
        for (int i = 0; i < mPostingData.size(); i++) {
            if (mPostingData.size() > 0)
                Log.i("Datas", mPostingData.get(i));
        }
        cus = new CustomAdapter(this, 0);
        setListAdapter(cus);

        ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, keywords);

        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(aa);
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {
        // listview.setFilterText(Category[position]);
        String Text = s1.getSelectedItem().toString();
        cus.getFilter().filter(Text);
        cus.notifyDataSetChanged();
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // listview.setFilterText("");
    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        Toast.makeText(this, "You have selected " + mPostingData.get(position),
                Toast.LENGTH_SHORT).show();
    }

    class CustomAdapter extends ArrayAdapter<String> {

        public void setData(ArrayList<String> mPpst) {
            mPostingData = mPpst;// contains class items data.
        }

        @Override
        public Filter getFilter() {
            return new Filter() {
                @Override
                protected void publishResults(CharSequence constraint,
                        FilterResults results) {
                    if (results != null && results.count >= 0) {
                        setData((ArrayList<String>) results.values);
                    } else {
                        setData(cat);// set original values
                    }

                    notifyDataSetInvalidated();
                }

                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults result = new FilterResults();
                    if (!TextUtils.isEmpty(constraint)) {
                        constraint = constraint.toString();
                        ArrayList<String> foundItems = new ArrayList<String>();
                        if (cat != null) {
                            for (int i = 0; i < cat.size(); i++) {

                                if (cat.get(i).contains(constraint)) {
                                    System.out.println("My datas" + cat.get(i));
                                    foundItems.add(cat.get(i));

                                } else {

                                }
                            }
                        }
                        result.count = foundItems.size();// search results found
                                                            // return count
                        result.values = foundItems;// return values
                    } else {
                        result.count = -1;// no search results found
                    }

                    return result;
                }
            };
        }

        LayoutInflater mInflater;

        public CustomAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
            // TODO Auto-generated constructor stub
            mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mPostingData.size();
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder vh;
            if (convertView == null) {
                vh = new ViewHolder();
                convertView = mInflater.inflate(R.layout.row, null);
                vh.t1 = (TextView) convertView.findViewById(R.id.textView1);

                convertView.setTag(vh);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                vh = (ViewHolder) convertView.getTag();
            }
            if (mPostingData.size() > 0)
                vh.t1.setText(mPostingData.get(position));
            return convertView;
        }

    }

    static class ViewHolder {
        TextView t1;
    }
}

模拟器截图

在此处输入图像描述

在此处输入图像描述

于 2013-07-29T09:54:59.117 回答