无法在Android中使用自定义数组适配器进行搜索?

时间:2013-01-18 17:06:47

标签: android listview android-arrayadapter

我无法从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());
}
 }

任何帮助都将受到高度赞赏..

由于

2 个答案:

答案 0 :(得分:0)

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

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

使用TextWatcher linked here

答案 1 :(得分:0)

我必须按Implementing Filterable Class

切换另一种搜索方式

请参阅我提供的类似answer