i want to display my second Column name based on the selection of First Column name from Sqlite...i load my First column in listview and when i click my first column name in listview the second colum name was Not Display it just show an empty Activity on Listview based on the selection of First Column
Sublist_item.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal"
android:textColor="#000000"
/>Subcategory_layout<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView2"
></ListView>
Subcategory:
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());
}
}
SubcategoryAdapter:
public class SubcategoryAdapter extends ArrayAdapter { List list= new ArrayList();
public SubcategoryAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
static class LayoutHandler
{
TextView SUBCATEGORY;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
LayoutHandler layoutHandler;
if (row==null)
{
LayoutInflater layoutInflater= (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=layoutInflater.inflate(R.layout.sublist_item,parent,false);
layoutHandler= new LayoutHandler();
layoutHandler.SUBCATEGORY=(TextView)row.findViewById(R.id.textView2);
row.setTag(layoutHandler);
}else {
layoutHandler= (LayoutHandler) row.getTag();
}
FoodSupply foodSupply= (FoodSupply) this.getItem(position);
layoutHandler.SUBCATEGORY.setText(foodSupply.getSubcategory());
return row;
}
} 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"},KEY_TITLE + "=" + mealname, null, null, null, null);
return cursor;
}enter code here
Aucun commentaire:
Enregistrer un commentaire