Learning external Sqlite With Multiple Tables
when i click breakfast,the breakfast related fields should be open in a new Activity on a Listview how Can we Do it
Subcateory.class
public class Subcategory extends ActionBarActivity {
ListView listView;
String title;
SqlLiteDbHelper dbHelper;
FoodSupply foodSupply;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
SubcategoryAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subcategory_layout);
listView= (ListView) findViewById(R.id.listView2);
Intent i = getIntent();
title = i.getStringExtra("title");
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.getsubcategory(sqLiteDatabase);
adapter= new SubcategoryAdapter(getApplicationContext(),R.layout.sublist_item);
listView.setAdapter(adapter);
if (cursor.moveToFirst())
{
do {
String subcategory;
subcategory=cursor.getString(0);
foodSupply= new FoodSupply(subcategory);
adapter.add(foodSupply);
}while (cursor.moveToNext());
}
}
public class MainActivity extends ActionBarActivity {
ListView listView;
SqlLiteDbHelper dbHelper;
FoodSupply foodSupply;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
TitleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listView);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
//foodSupply= new FoodSupply();
sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.gettitles(sqLiteDatabase);
adapter= new TitleAdapter(getApplicationContext(),R.layout.list_item);
listView.setAdapter(adapter);
if (cursor.moveToFirst())
{
do {
String title;
title=cursor.getString(0);
foodSupply= new FoodSupply(title);
adapter.add(foodSupply);
}while (cursor.moveToNext());
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this,Subcategory.class);
String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
intent.putExtra("title", title);
startActivity(intent);
}
});
}
Database Class
public Cursor getsubcategory(SQLiteDatabase db)
{
db = this.getReadableDatabase();
Cursor cursor;
cursor = db.query(true, "food_details", new String[]{"subcategory"}, null, null, null, null, null, null);
return cursor;
}
Aucun commentaire:
Enregistrer un commentaire