通过适配器

时间:2015-08-27 20:30:49

标签: android listview simplecursoradapter listactivity

  

我正在尝试过滤列表视图中的路径点,这些路径点已填充到布局中。在 初始化视图部分 中,我调试代码及其工作正常,因为它查找与搜索栏输入值匹配的路由名称,并将其传递给adapter.getFilter ().filter(S);.但是,过滤器可能无法正常工作或者未刷新布局,因为搜索后我的自定义适配器值未在布局上显示。

活动类

public class SelectRoutePointsActivity extends ListActivity implements OnClickListener, OnCheckedChangeListener{
SelectRoutePointsActivity.MyAdapter adapter;
SelectRoutePointsActivity.MyAdapter lva;
long routePointID = -1;
Context this_;
private EditText inputSearch;
public SelectRoutePointsActivity(){
    this_ = this;
}

@Override
protected void onCreate(Bundle savedInstanceState){
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.route_layout);
    init();
}

初始化视图

void init(){
    RouteDB db = new RouteDB(this_);
    db.open();
    Cursor cur = db.getAll();

    adapter = new SelectRoutePointsActivity.MyAdapter(this_, cur, this, this);

    int count = adapter.getCount();
    if (count <= 0)
    {
        Toast.makeText(this_, "No Routes Points are available", Toast.LENGTH_LONG).show();
        finish();

        return;
    }

    setListAdapter(adapter);
    inputSearch = (EditText) findViewById(R.id.inputSearch);


    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            String itemSearch = inputSearch.getText().toString();
            int itemLength = inputSearch.length();

            for (int i = 0; i < adapter.getCount(); i++) {                  
                adapter.getCursor().moveToPosition(i);
                String listGet = adapter.getCursor().getString(RouteDB.I_NAME);
                if(itemLength<=listGet.length()){
                    if(listGet.contains(s)){
                        adapter.getFilter().filter(s);

                    }
                }


            }

            adapter.notifyDataSetInvalidated();
            adapter.notifyDataSetChanged();
            setListAdapter(adapter);
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });      

    db.close();
}
  

适配器类

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
    CheckBox chk = (CheckBox) v.findViewById(R.lst_item.checkbox);
    chk.toggle();       
}

private class MyAdapter extends ResourceCursorAdapter
{
    OnClickListener btnOnClick;
    OnCheckedChangeListener chkOnChange;


    @SuppressWarnings("deprecation")
    public MyAdapter(Context context, Cursor c, OnClickListener btnOnClick, OnCheckedChangeListener chkOnChange)
    {
        super(context, R.layout.list_item, c);
        this.btnOnClick = btnOnClick;
        this.chkOnChange = chkOnChange;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return li.inflate(R.layout.list_item, parent, false);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        return super.getView(position, convertView, parent);
    }

    @Override
    public void bindView(View view, Context context, Cursor cur)
    {
        String pointName = cur.getString(RouteDB.I_NAME);
        long pointID = cur.getLong(RouteDB.I_ID);
        String routeSpeed = cur.getString(RouteDB.I_SPEED);
        String routeType = cur.getString(RouteDB.I_TYPE);
        boolean visible = (cur.getInt(RouteDB.I_VISIBLE) == 1);


        view.setTag(pointID);

        CheckBox checkbox = (CheckBox) view.findViewById(R.lst_item.checkbox);
        checkbox.setText(pointName);
        checkbox.setTag(pointID);
        checkbox.setChecked(visible);
        checkbox.setOnCheckedChangeListener(chkOnChange);

        Button button = (Button) view.findViewById(R.lst_item.btnMore);
        button.setTag(pointID);
        button.setOnClickListener(btnOnClick);
    }
}

0 个答案:

没有答案