samedi 3 octobre 2015

Listview With MultipleTables in Sqlite from Assert

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

This is What is Click When i select the all the Fields in listview

I need This Only When i select Breakfast,Breakfast related Fields want to be display

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;
}

My External Databaseenter code here

Aucun commentaire:

Enregistrer un commentaire