Hi i have a fragment and i want to display some information of a specific column this code works perfectly with an activity but nothing work in a listfragment especially db=openOrCreateDatabase here is the activity code :
public class MainActivity extends ListActivity {
ListView listView;
ArrayList<String> arrayList = new ArrayList<String>();
ArrayAdapter<String> adapter;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = getListView();
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
db.execSQL("INSERT INTO student VALUES('hello@aol.com', 'Jeff1', 'Bath')");
db.execSQL("INSERT INTO student VALUES('hello@aol.com', 'Jeff2', 'Bath')");
db.execSQL("INSERT INTO student VALUES('hello@aol.com', 'Jeff3', 'Bath')");
Cursor cursor = db.rawQuery("SELECT * FROM student", null);
// Toast.makeText(myContext, ""+cursor.getCount(), Toast.LENGTH_LONG).show();
if (cursor.moveToFirst())
{
do
{
arrayList.add(cursor.getString(0));
} while (cursor.moveToNext());
}
}
and here is what i did for the listfragment it 's working until i add the database :
public class F1_fr extends ListFragment {
View rootview;
TextView textView1;
ArrayAdapter<String> aa;
ArrayList<String> arrayList = new ArrayList<String>();
SQLiteDatabase db;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootview=inflater.inflate(R.layout.f1_lay,container,false);
textView1=(TextView)rootview.findViewById(R.id.textView1);
aa = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arrayList);
setListAdapter(aa);
return rootview;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db=SQLiteDatabase.openOrCreateDatabase("StudentDB", null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
db.execSQL("INSERT INTO student VALUES('hello@aol.com', 'Jeff1', 'Bath')");
db.execSQL("INSERT INTO student VALUES('hello@aol.com', 'Jeff2', 'Bath')");
db.execSQL("INSERT INTO student VALUES('hello@aol.com', 'Jeff3', 'Bath')");
Cursor cursor = db.rawQuery("SELECT * FROM student", null);
// Toast.makeText(myContext, ""+cursor.getCount(), Toast.LENGTH_LONG).show();
if (cursor.moveToFirst())
{
do
{
arrayList.add(cursor.getString(0));
} while (cursor.moveToNext());
}
}
So i'am asking your help to make it work with a listfragment ,thanks in advance
Aucun commentaire:
Enregistrer un commentaire