I have a List Of Columns like Title,Subcatgory,Details
i will Display all the Fields of Titles in Listview,now what is need is when i click Title the Correspondings fields related to Subcatogys are want to display here is my database
Title|Subcategory|Details
Bf|idly|good
Bf|Dosai|poor
Bf|Pongal|Good
Lu|meals|Good
Lu|hmeals|poor
Lu|fmeals|Good
DR|idly|good
DR|parota|good
DR|dosai|good** When i Click BF i want to display idly,dosai,pongal in a new Activity likewise for others how can we Do It
Subcategory:
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,title);
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 class SqlLiteDbHelper extends SQLiteOpenHelper{
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Food.sqlite";
private static final String DB_PATH_SUFFIX = "/databases/";
static Context ctx;
public static final String KEY_TITLE="Title";
public SqlLiteDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
ctx = context;
}
public void CopyDataBaseFromAsset() throws IOException {
InputStream myInput = ctx.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = getDatabasePath();
// if the path doesn't exist first, create it
File f = new File(ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX);
if (!f.exists())
f.mkdir();
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
private static String getDatabasePath() {
return ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX + DATABASE_NAME;
}
public SQLiteDatabase openDataBase() throws SQLException {
File dbFile = ctx.getDatabasePath(DATABASE_NAME);
if (!dbFile.exists()) {
try {
CopyDataBaseFromAsset();
System.out.println("Copying sucess from Assets folder");
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}
return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor gettitles(SQLiteDatabase db)
{
db = this.getReadableDatabase();
Cursor cursor;
//cursor=db.rawQuery("SELECT Title FROM food_details",null);
cursor = db.query(true, "food_details", new String[]{"Title"}, null, null, null, null, null, null);
return cursor;
}
public Cursor getsubcategory(SQLiteDatabase db,String mealname)
{
db = this.getReadableDatabase();
Cursor cursor;
cursor = db.query("food_details", new String[]{"subcategory"},, null, null, null, null);
return cursor;
}enter code here
Aucun commentaire:
Enregistrer un commentaire