setFilterQueryProvider不会更改我的ListView

时间:2016-05-29 13:33:46

标签: android sqlite listview

我的ListView显示了我的SQLite数据库查询的结果。 当我显示结果时,它有效。但是,如果我有50k项目,我想搜索SQLite数据库,只给我几个项目。所以我制作了这个CursorAdapter:

public class ListAirportAdapter extends CursorAdapter
{
private Context mContext;
TextView code,nome;
private List<CountryAirportItem> countryAirportItems;
ArrayList<CountryAirportItem> data=new ArrayList<>();
private LayoutInflater mLayoutInflater;

public ListAirportAdapter(Context context, Cursor c, List <CountryAirportItem> cai)
{
    super(context, c);
    mContext = context;
    mLayoutInflater = LayoutInflater.from(context);
    this.countryAirportItems = cai;
}
public int getCount()
{
    return countryAirportItems.size();
}

@Override
public Object getItem(int position) {
    return countryAirportItems.get(position);
}

@Override
public long getItemId(int position)
{
    return countryAirportItems.get(position).getId();
}

public View getView(int position, View convertView, ViewGroup parent)
{
   View v;
    if (convertView == null)
    {
        v = newView(mContext, mCursor, parent);
    } else {
        v = convertView;
    }
    bindView(v, mContext, mCursor);
    code.setText(countryAirportItems.get(position).getCode());
    nome.setText(countryAirportItems.get(position).getName());
    return v;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
    View v = mLayoutInflater.inflate(R.layout.item_listview,parent,false);
    return v;
}

@Override
public void bindView(View view, Context context, Cursor cursor)
{
    code = (TextView)view.findViewById(R.id.codAirport);
    nome = (TextView)view.findViewById(R.id.nomeAirport);
    // ImageView imgAir = (ImageView)view.findViewById(R.id.stateImg);
}
}

我在FragmentClass中做了这个:

  private DatabaseHelper databaseHelper;
private String DB_name = "storex";

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.fragment_search_flight, container, false);

    final FragmentManager manager = getActivity().getSupportFragmentManager();

   Bundle bundle = getArguments();
    if(bundle != null)
    {
     code = bundle.getString("code");

    }


    databaseHelper = new DatabaseHelper(getActivity());
    final File database = getActivity().getDatabasePath(DatabaseHelper.DBNAME);

    if(false == database.exists()) {
        databaseHelper.getReadableDatabase();
        if (copyDatabase(getActivity())) {
            Log.d("INFo", "copy database success");
        } else {
            Log.d("INFo", "error copying data");

        }
    }
    cai=databaseHelper.getCountryAirportItem();

    listView = (ListView) v.findViewById(R.id.listview);
    adapter=new ListAirportAdapter(getActivity(),null,cai);
    listView.setAdapter(adapter);

    EditText myFilter = (EditText)v.findViewById(R.id.ricerca);
    myFilter.addTextChangedListener(new TextWatcher()
    {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {
            Log.d("sfdsf",s.toString());

            adapter.getFilter().filter(s.toString());

        }
    });

    adapter.setFilterQueryProvider(new FilterQueryProvider()
    {

        public Cursor runQuery(CharSequence constraint)
        {
            databaseHelper.openDatabase();

            Cursor cursor = databaseHelper.fetchAirportByCode(constraint != null ? constraint.toString() : null);


            cursor.moveToFirst();
            return cursor;

        }
    });
return v;
}

private boolean copyDatabase(Context context)
{
    try
    {
        InputStream inputStream = context.getAssets().open(DatabaseHelper.DBNAME);
        String outFileName = DatabaseHelper.DBLOCATION+DatabaseHelper.DBNAME;
        OutputStream outputStream = new FileOutputStream(outFileName);
        byte[]buffer = new byte[1024];
        int length = 0;

        while ((length = inputStream.read(buffer))>0)
        {
            outputStream.write(buffer,0,length);
        }
        outputStream.flush();
        outputStream.close();
        return true;

    }
    catch (IOException e)
    {
        e.printStackTrace();
        return false;
    }
}

这是DatabaseHelper中的函数:

 public Cursor fetchAirportByCode(String s)
{
    Cursor cursor = null;
  if(s != null)
  {
   cursor = database.rawQuery("SELECT ZICAO_CODE as _id,ZNAME FROM ZAIRPORTS WHERE ZICAO_CODE like '%" + s
           + "%'", null);

  }
    if (cursor != null) {
        cursor.moveToFirst();
    }
    closeDatabase();
  return cursor;

}

但是当我在EditText上输入时,它不会改变我的结果......为什么?

编辑:现在使用SimpleCursorAdapter我做了这个:

 SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
            R.layout.item_listview,databaseHelper.getCountryAirportItem(),
            new String[] { "title" },
            new int[] { android.R.id.text1 });

 listView.setAdapter(adapter);

现在我有两个问题:

  1. 我如何获取信息(例如代码,名称等)?
  2. 如何在onItemClick中获取正确的项目?

0 个答案:

没有答案