问题描述
我是Android的新手,出于学习目的,我创建了一个简单应用,该应用的主意是我在显示学生信息,例如Parent Table
为College Name
(列表视图中的大学名称列表)- > Child Table
是Student Names
(显示该学院的学生姓名)
1.AnnaUniversity 2.MGRUniversity---->Parent Table
-------------- --------------
1.arun 1.Raja
2.visnu 2.Bharathi
3.vihal ---->Child Table
为此,我在单一数据库中的sqlite中创建了两个表(学院名称和学生名称)
在First Activity
中,我获得了大学名称的用户输入,并将其存储在sqlite中并在Listview中显示
在调用First Activity
ListViewitem is Clciked
的自定义对话框时,用户输入存储在第二个表中的该学院的学生姓名,并在Listview的Second Activity
中显示该名称以及复选框
问题在于学生姓名存储在第二张表中,并且没有根据大学名称显示,也没有显示在第二张活动的列表视图中例如:在AnnaUniversity父表中我添加了一些存储在数据库中的名称(子表),但不会根据父项的名称显示
数据库帮助器
// 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,cv);
if (result == -1) {
Toast.makeText(context,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;
}
添加学生:
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();
}
});
}
学生自定义适配器
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;
}
解决方法
您可以在学生表中使用大学名称作为外键 确保两个列表都具有不同的列名 只需确保COLUMN_ID =“ id”比COLUMN_ID =“ s_id”
//数据库名称 公共静态最终字符串DATABASE_NAME =“ details.db”;
// Table 1
public static final String TABLE_NAME = "CollegeName";
public static final String COLUMN_ID = "c_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 = "s_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,"
+ COLUMN_C_ID + " INTEGER," + "FOREIGN KEY("+
COLUMN_C_ID +") "
+ "REFERENCES " + TABLE_NAME +"("+COLUMN_ID +")"+ ");";
sqLiteDatabase.execSQL(query1);
}