如何使用SQLite中的游标适配器在列表视图中显示图像和文本?使用名称搜索视图

时间:2018-04-13 06:38:13

标签: sqlite listview android-sqlite simplecursoradapter android-cursoradapter

我收到此错误可以任何人向我发布解决方案

致命的例外:主要                                                                          android.database.StaleDataException:尝试访问已关闭的CursorWindow.Most可能的原因:在调用此方法之前,游标已停用。     ImageCursorAdapter.java

private Cursor c;
private Context context;
byte[] image;

public ImageCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.c = c;
    this.context = context;
}

public View getView(int pos, View inView, ViewGroup parent) {
    View v = inView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.activity_view__patients, null);
    }
    this.c.moveToPosition(pos);

 String firstName = this.c.getString(this.c.getColumnIndex("_id"));
   String lastName = this.c.getString(this.c.getColumnIndex("Name"));
// String titleStr = this.c.getString(this.c.getColumnIndex("title"));
    image = this.c.getBlob(this.c.getColumnIndex("Image"));


ImageView iv = (ImageView) v.findViewById(R.id.all_img);
if (image != null) {
    // If there is no image in the database "NA" is stored instead of a blob
    // test if there more than 3 chars "NA" + a terminating char if more than
    // there is an image otherwise load the default
    if (image.length > 0) {
        iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length));
    } else {
        iv.setImageResource(R.mipmap.ic_launcher);
    }
}
TextView fname = (TextView) v.findViewById(R.id.db_id);
fname.setText(firstName);

TextView lname = (TextView) v.findViewById(R.id.name_id);
lname.setText(lastName);

// this.c.close();
//  this.c.moveToNext();
return (v);


}

}

这是我的主要活动.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    db = new DatabaseHelper(this);
    open();
    db.getReadableDatabase();
 db.getWritableDatabase();
    lv = (ListView) findViewById(R.id.listV);

    int layoutstyle = R.layout.activity_view__patients;
    xml_id = new int[]{
            R.id.db_id,
            R.id.name_id,
            R.id.all_img
    };
    column = new String[]{
            "patients._id",
            "patients.Name",
            "patients.Image"

    };
    row = db.fetchAllData();
    adapter = new ImageCursorAdapter(getApplicationContext(), layoutstyle, row, column, xml_id);
    // adapter = new SimpleCursorAdapter(this, layoutstyle, row, column, xml_id);

    lv.setAdapter(adapter);

    adapter.notifyDataSetChanged();
    //onClick function
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) {
            Cursor row = (Cursor) adapterview.getItemAtPosition(position);
            String Id = row.getString(row.getColumnIndex("_id"));
            String name = row.getString(row.getColumnIndex("Name"));

            //go to detailsContact page
            Intent todetais = new Intent(ViewPatients2.this, TabViewActivity.class);
            todetais.putExtra("ListViewClickedItemValue", Id);
            // todetais.putExtra("NAME", name);
            // todetais.putExtra("NUMBER", number);
            //  todetais.putExtra("EMAIL", email);
            // todetais.putExtra("ADDRESS", address);
            startActivity(todetais);
        }
    });

    //dispay data by filter
   et = (EditText) findViewById(R.id.edit4);

    et.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) {
                                      adapter.getFilter().filter(s.toString());


                                  }
                              });
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return db.fetchdatabyfilter(constraint.toString(), "patients.Name");
        }
    });


enter code here

}

private void open() {
    sqLiteDatabase = openOrCreateDatabase(DatabaseHelper.DATABASE_NAME, Context.MODE_PRIVATE, null);

}
@Override
public void onDestroy() {
    super.onDestroy();
    row.close();
    db.close();
}

}

这是我的DatabaseHelper.java

public Cursor fetchdatabyfilter(String inputText,String Name) throws SQLException {
    SQLiteDatabase sqlDb=this.getReadableDatabase();
        Cursor row = null;
        String query = "SELECT * FROM "+Patient_Table;
        if (inputText == null  ||  inputText.length () == 0)  {
            row = sqlDb.rawQuery(query, null);
        }else {
            query = "SELECT * FROM "+Patient_Table+" WHERE "+Name+" like '%"+inputText+"%'";
            row = sqlDb.rawQuery(query, null);
        }
       if (row != null) {
            row.moveToFirst();

        }

        return row;
    }

0 个答案:

没有答案