1

我创建了一个列表视图,其中数据来自存储在string.xml.

我有一个 edittext,我想从我的 listview 中搜索特定数据。我的 listview 数据包含这样的 011 Delhi delhi... 我该怎么做?

这是我的代码..

adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, arrayList);

Resources res = getResources();
String[] a = res.getStringArray(R.array.states);
arrayList.add("" + a);
adapter.notifyDataSetChanged();

editText.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before,
                              int count) {
        // TODO Auto-generated method stub
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        // TODO Auto-generated method stub
    }

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
    }
});
4

3 回答 3

3

我希望下面的代码对你有用。

public class MainActivity extends Activity {
    private ListView lv;
    private EditText et;
    String listview_array[]={"123","TWO","Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "1234", "iPhone 4S", "Samsung Galaxy Note 800", "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
    private ArrayList<String> array_sort = new ArrayList<String>();
    int textlength = 0;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.ListView01);
        et = (EditText) findViewById(R.id.EditText01);
        lv.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, listview_array));
        int x= lv.getHeaderViewsCount ();
        et.addTextChangedListener(new TextWatcher()
        {
            public void afterTextChanged(Editable s)
            {
                // Abstract Method of TextWatcher Interface.
            }
            public void beforeTextChanged(CharSequence s,
            int start, int count, int after)
            {
                // Abstract Method of TextWatcher Interface.
            }
            public void onTextChanged(CharSequence s,
            int start, int before, int count)
            {
                textlength = et.getText().length();
                array_sort.clear();
                for (int i = 0; i < listview_array.length; i++)
                {
                    if (textlength <= listview_array[i].length())
                    {
                        if (et.getText().toString().equalsIgnoreCase(
                        (String)
                        listview_array[i].subSequence(0,
                        textlength)))
                        {
                            array_sort.add(listview_array[i]);
                        }
                    }
                }
                lv.setAdapter(new ArrayAdapter<String>
                (MainActivity.this,
                android.R.layout.simple_list_item_1, array_sort));
            }
        });
    }
}
于 2012-10-14T10:57:26.127 回答
1

在您的适配器中使用此代码。

public void filter(String charText) {
        charText = charText.toLowerCase();
        picList.clear();
        if (charText.length() == 0) {
            picList.addAll(listpicOrigin);
        } else {
            for (Picture pic : listpicOrigin) {
                if (pic.getPicName().toLowerCase().contains(charText)) {
                    picList.add(pic);
                }
            }
        }
        notifyDataSetChanged();
    }

更多细节可以参考本教程

于 2013-03-18T07:27:38.943 回答
1

你可以这样搜索:

String text_to_search = editText.getText().toString();
for(String str : your_list) {
    if(str.equals(text_to_search) {
        //Do your operation
    }
}

如果你想要一个索引,那么你可以使用我们的传统for循环。

于 2012-10-14T11:02:55.393 回答