1

我正在尝试使用列表视图制作 Android 应用程序,并在该列表视图中包含项目和子项目。一切正常,除了过滤文本。我一直用

list.setTextFilterEnabled(true);

但是当我输入某些内容时,列表中的所有内容都会消失。这是我的主要活动:

package com.macura.chatdictionary;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.inputmethod.InputMethodManager;
import android.widget.ListView;
import android.widget.SearchView;

public class MainActivity extends Activity {

    public ListView lv;
    private CustomAdapter adapter;
    private ArrayList<Custom> fetch = new ArrayList<Custom>();



String[] from = new String[] { "title", "description" };
int[] to = new int[] { R.id.title, R.id.description };



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Custom one = new Custom("Big1","Small1");
    Custom two = new Custom("Big2","Small2");
    Custom three = new Custom("Big3","Small3");
    fetch.add(one);
    fetch.add(two);
    fetch.add(three);

    lv = (ListView) findViewById(R.id.listView1);
    adapter = new CustomAdapter(MainActivity.this,
            R.id.listView1,
            fetch);
    lv.setAdapter(adapter);
    lv.setTextFilterEnabled(true);


    }

public void filterItems(String newItem){

}    

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_actionbar, menu);
    getMenuInflater().inflate(R.menu.main, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
           (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() { 
        @Override 
        public boolean onQueryTextChange(String newText) {

            if (TextUtils.isEmpty(newText)) {
                lv.clearTextFilter();
            } else {
                lv.setFilterText(newText);
            }
            return true;
        } 

        @Override 
        public boolean onQueryTextSubmit(String query) { 

            InputMethodManager imm = (InputMethodManager)getSystemService(
                      Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
            return true; 
        } 
    }; 

    searchView.setOnQueryTextListener(queryTextListener); 

    return true;
    }



}

这是我的 CustomAdapter.java:

package com.macura.chatdictionary;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<Custom>{
private ArrayList<Custom> entries;
private Activity activity;

public CustomAdapter(Activity a, int textViewResourceId, ArrayList<Custom> entries) {
    super(a, textViewResourceId, entries);
    this.entries = entries;
    this.activity = a;
}

public static class ViewHolder{
    public TextView item1;
    public TextView item2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    if (v == null) {
        LayoutInflater vi =
            (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.listview_layout, null);
        holder = new ViewHolder();
        holder.item1 = (TextView) v.findViewById(R.id.title);
        holder.item2 = (TextView) v.findViewById(R.id.description);
        v.setTag(holder);
    }
    else
        holder=(ViewHolder)v.getTag();

    final Custom custom = entries.get(position);
    if (custom != null) {
        holder.item1.setText(custom.getcustomBig());
        holder.item2.setText(custom.getcustomSmall());
    }
    return v;
}

}

这是我的 Custom.java:

package com.macura.chatdictionary;

public class Custom {
private String customBig;
private String customSmall;

public Custom(String string, String string2) {
this.customBig = string;
this.customSmall = string2;
}
public String getcustomBig() { return customBig; }
public void setcustomBig(String customBig) { this.customBig = customBig; }

public String getcustomSmall() { return customSmall; }
public void setcustomSmall(String customSmall) { this.customSmall = customSmall; }
}

这是我的 listview_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="#000000"
    android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
    android:id="@+id/description"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:textColor="#000000"
    android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

我必须做什么才能使过滤文本起作用?

4

1 回答 1

0

在您的适配器上过滤

            adapter.getFilter().filter("filter text", new FilterListener()
            {
                @Override
                public void onFilterComplete(int count)
                {
                   // do some thing when filter complete
                }
            });
于 2013-07-13T11:30:52.280 回答