-1

我无法从中搜索listview,我尝试了各种方法,但它对我不起作用(没有错误)。我有其他搜索方式,但我想让这种方式成为可能。

这是代码

public class MainActivity extends Activity implements TextWatcher   {

CustomAdapter mAdapter;
ListView mListview;
EditText search;
List<String> tempCity, cityNames;
String list[];

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    init();

    bindComponents();

    addListeners();
}

private void addListeners() {
    // TODO Auto-generated method stub

    search.addTextChangedListener(this);
}

private void init() {
    // TODO Auto-generated method stub

    mPos = new ArrayList<Integer>();
    tempCity = new ArrayList<String>();
    cityNames = new ArrayList<String>();
    list = getResources().getStringArray(R.array.cityList);
    cityNames.addAll(Arrays.asList(list));


}



private void bindComponents() {
    // TODO Auto-generated method stub

    mListview = (ListView) findViewById(R.id.cityList);

    mAdapter = new CustomAdapter();
    search = (EditText) findViewById(R.id.eTsearch);
    mListview.setAdapter(mAdapter);

}

public class CustomAdapter extends ArrayAdapter<String> {

    public CustomAdapter() {
        super(MainActivity.this, android.R.layout.simple_list_item_1,
                cityNames);
        // TODO Auto-generated constructor stub

    }

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

    @Override
    public String getItem(int pos) {
        // TODO Auto-generated method stub
        return cityNames.get(pos);
    }

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

    @Override
    public View getView(int pos, View v, ViewGroup vGroup) {
        // TODO Auto-generated method stub

        View row = v;

        if (row == null) {

            row = getLayoutInflater().inflate(R.layout.main_row, null);

        }

        TextView mCityTv = (TextView) row.findViewById(R.id.itemTv);
        mCityTv.setText(cityNames.get(pos));

        return row;
    }

}

@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

}

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

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

    mAdapter.getFilter().filter(s.toString());
}
 }

任何帮助将不胜感激..

谢谢

4

2 回答 2

0

1) 调用mListview.setTextFilterEnabled(true);ListView 启用过滤

CustomAdapter2)为您的类创建一个相关的构造函数。您需要传递上下文以及各种资源/数据。阅读它:http: //developer.android.com/reference/android/widget/ArrayAdapter.html

或者

在此处使用TextWatcher 链接

于 2013-01-18T20:36:56.493 回答
0

我必须切换另一种按Implementing Filterable类别搜索的方式

请看我给出的类似答案

于 2013-01-21T17:45:02.083 回答