In my app i am using Navigation drawer and also sqlite database to display data from sqlite in the fragments.When i click on one of the navigation element i.e Search hospitals it should load data from the db but then the app flashes "unfortunately app was closed".
Below is the code of the fragment,
public class SHospitals extends Fragment {
private hospitalDBadapter dbhelper;
private SimpleCursorAdapter dataAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.shospitals, container, false);
dbhelper = new hospitalDBadapter(getActivity());
dbhelper.open();
dbhelper.deleteallhospitals();
dbhelper.insertsomehospitals();
Cursor cursor = dbhelper.fetchallhospitals();
String[] columns = new String[] { hospitalDBadapter.KEY_HOSP,
hospitalDBadapter.KEY_ADD, hospitalDBadapter.KEY_CONT };
int[] to = new int[] { R.id.hospital, R.id.address, R.id.contact, };
dataAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.hospitalinfo, cursor, columns, to, 0);
final ListView listview = (ListView) getView().findViewById(R.id.lv1);
listview.setAdapter(dataAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Cursor cursor = (Cursor) listview.getItemAtPosition(position);
String hospital_name = cursor.getString(cursor
.getColumnIndexOrThrow("hosp_name"));
Toast.makeText(getActivity(), hospital_name, Toast.LENGTH_SHORT)
.show();
}
});
EditText myfilter = (EditText) getView().findViewById(R.id.filter);
myfilter.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
dataAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
// TODO Auto-generated method stub
return dbhelper.fetchhospitalsbyname(constraint.toString());
}
});
return rootView;
}}
and this is my databasehelper class,
public class hospitalDBadapter {
public static final String KEY_ID = "_id";
public static final String KEY_HOSP = "hosp_name";
public static final String KEY_ADD = "hosp_Add";
public static final String KEY_CONT = "hosp_cont";
private static final String DATABASE_NAME = "hospitals_dbs";
private static final String SQLITE_TABLE = "hospitals_tables";
private static final int DATABASE_VERSION = 1;
public static final String TAG = "hospitalDBadapter";
public final Context ourcontext;
private DatabaseHelper mDBHelper;
private SQLiteDatabase mDB;
public static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + SQLITE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_HOSP
+ " TEXT NOT NULL, " + KEY_ADD + " TEXT NOT NULL, "
+ KEY_CONT + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
onCreate(db);
}
}
public hospitalDBadapter(Context c) {
ourcontext = c;
}
public hospitalDBadapter open() throws SQLException {
mDBHelper = new DatabaseHelper(ourcontext);
mDB = mDBHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDBHelper != null) {
mDBHelper.close();
}
}
public boolean deleteallhospitals() {
// TODO Auto-generated method stub
int donedelete = 0;
donedelete = mDB.delete(SQLITE_TABLE, null, null);
Log.w(TAG, Integer.toString(donedelete));
return donedelete > 0;
}
public void insertsomehospitals() {
// TODO Auto-generated method stub
createhospital("Lilavati Hospital", "Peddar Road", "6541324532");
createhospital("Sion Hospital", "A B Road", "6451235478");
createhospital("Dayaben Hospital", "S P Road", "4132456787");
createhospital("ABC Hospital", "P D Road", "8451295421");
createhospital("DEF Hospital", "D S Road", "9832151231");
}
private long createhospital(String hosp_name, String hosp_Add,
String hosp_cont) {
// TODO Auto-generated method stub
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_HOSP, hosp_name);
initialValues.put(KEY_ADD, hosp_Add);
initialValues.put(KEY_CONT, hosp_cont);
return mDB.insert(SQLITE_TABLE, null, initialValues);
}
public Cursor fetchallhospitals() {
// TODO Auto-generated method stub
Cursor mCursor = mDB.query(SQLITE_TABLE, new String[] { KEY_ID,
KEY_HOSP, KEY_ADD, KEY_CONT }, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchhospitalsbyname(String string) throws SQLException {
// TODO Auto-generated method stub
Log.w(TAG, string);
Cursor mCursor = null;
if (string == null || string.length() == 0) {
mCursor = mDB.query(SQLITE_TABLE, new String[] { KEY_ID, KEY_HOSP,
KEY_ADD, KEY_CONT }, null, null, null, null, null);
} else {
mCursor = mDB.query(true, SQLITE_TABLE, new String[] { KEY_ID,
KEY_HOSP, KEY_ADD, KEY_CONT }, KEY_HOSP + " like '%"
+ string + "%'", null, null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
plz.help me out.
Aucun commentaire:
Enregistrer un commentaire