I am New to Android, I created a Simple App for my Learning purpose, the app idea is i am displaying a student informations like Parent Table
was College Name
(List of College Names in listview) -> Child Table
was Student Names
(Displaying Students name of that College)
1.AnnaUniversity 2.MGRUniversity---->Parent Table
-------------- --------------
1.arun 1.Raja
2.visnu 2.Bharathi
3.vihal ---->Child Table
For that i Created a Two Tables(College Name and Students Name) in SQlite in Single Datbase called "Details.db"
in First Activity
I get the User Input of College Name and Stored in SQlite and Display that in Listview
When the First Activity
ListViewitem is Clciked
a Custom Dialog is Called there User enter the Students Name of that College it stored in to the Second Table and Display that in Second Activity
of Listview along with CheckBox
The Problem is the Students Names are Stored inside the Second Table and it doesn't display according to the college name also Not Showing in listview of second activity ex : Inside AnnaUniversity Parent Table i Add Some Names(child table) it Stored into the Database But It Doesnt Display According to the Names of the parent
DatabaseHelper
// Database Name
public static final String DATABASE_NAME = "details.db";
// Table 1
public static final String TABLE_NAME = "CollegeName";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_TITLE = "college_NAME";
private static final String COLUMN_IMAGE = "image_bitmap";
// Table 2
private static final String TABLE2_NAME = "studentsName";
public static final String COLUMN1_ID = "ID";
public static final String COLUMN2_TITLE = "students_NAME";
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query =
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY ,"
+ COLUMN_TITLE + " TEXT, "
+ COLUMN_IMAGE + " BLOB )";
sqLiteDatabase.execSQL(query);
String query1 =
"CREATE TABLE IF NOT EXISTS " + TABLE2_NAME + "("
+ COLUMN1_ID + " INTEGER PRIMARY KEY ,"
+ COLUMN2_TITLE + " TEXT )";
sqLiteDatabase.execSQL(query1);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Creating a College Name ( College Name was Saved in College table )
void createlist(String title, byte[] image) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_IMAGE, image);
Long result = sqLiteDatabase.insert(TABLE_NAME, null, cv);
if (result == -1) {
Toast.makeText(context, "Failed to create", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "College Name Created Sucessfully", Toast.LENGTH_SHORT).show();
}
}
// Read ( Displaying the saved College Names)
Cursor readAllData() {
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if (db != null) {
cursor = db.rawQuery(query, null);
}
return cursor;
}
// Creating a second table Students Name ( Students Name was Saved in student table )
void itemlist(String items) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN2_TITLE, items);
Long result = sqLiteDatabase.insert(TABLE2_NAME, null, cv);
if (result == -1) {
Toast.makeText(context, "Failed to create", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Students name Added Sucessfully", Toast.LENGTH_SHORT).show();
}
}
// Read ( Displaying the saved students Names)
Cursor readlistAllData() {
String query = "SELECT * FROM " + TABLE2_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if (db != null) {
cursor = db.rawQuery(query, null);
}
return cursor;
}
AddStudents:
public class AddStudents extends AppCompatActivity {
private LinearLayout linearLayout;
DatabaseHelper myDB;
ArrayList<String> listitems;
StudentsCustomAdapter sca;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_items);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_button);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Custom Dialog is Called when Plus Button is Clicked to add Students Name inside the Selected College Name
ShowPopup();
}
});
myDB = new DatabaseHelper(AddStudents.this);
listitems = new ArrayList<>();
DisplayList();
sca = new StudentsCustomAdapter(AddStudents.this,listitems);
}
// Displaying the Students Name
private void DisplayList(){
Cursor cursor = myDB.readlistAllData();
if (cursor.getCount() == 0) {
Toast.makeText(this, "No Data.", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
listitems.add(cursor.getString(1));
}
}
}
//adding a Students name in custom dialog
private void ShowPopup() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
final EditText lname = dialog.findViewById(R.id.list_Edit_txt);
Button add = dialog.findViewById(R.id.add);
Button cancel = dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = lname.getText().toString();
if (!TextUtils.isEmpty(lname.getText().toString())) {
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
db.itemlist(name);
Toast.makeText(AddItems.this, "Students Added Sucessfully !", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(AddItems.this, "The name cannot be empty!", Toast.LENGTH_LONG).show();
}
});
}
Students Custom Adapter
public class StudentsCustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context context;
private ArrayList<String> listitem_name;
public StudentsCustomAdapter(Context c, ArrayList<String> listnames)
{
this.context=c;
this.listitem_name=listnames;
this.mInflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listitem_name.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = mInflater.inflate(R.layout.custom_list_items, viewGroup, false);
}
CheckBox checkBox = view.findViewById(R.id.cheeckbox);
TextView listitemnames = view.findViewById(R.id.listitem_name);
listitemnames.setText((CharSequence) listitem_name);
return null;
}
TABLE_NAME.COLUMN_ID
**cannot resolve COLUMN_ID ** i.sstatic.net/Xz0Ig.jpg – Dixil