In my Application userinput data from Diaglogbox was Stored into the Sqlite and want to Display those datas in listview along With Checkbox
Example :
I Stored Userinput in sqlite but i dont know how to Display those saved names along with Checkbox in In Listview
How to Achieve that i am new to Android !
Database :
//Display these Datas in Listview Along With CheckBox
// Table 2
private static final String TABLE2_NAME = "listitem_name";
public static final String COLUMN1_ID = "I_ID";
public static final String COLUMN2_TITLE = "LISTITEMS_NAME";
onCreate(){
String query1 =
"CREATE TABLE IF NOT EXISTS " + TABLE2_NAME + "("
+ COLUMN1_ID + " INTEGER PRIMARY KEY ,"
+ COLUMN2_TITLE + " TEXT ,"
+ COLUMN_ID + " INTEGER, " + "FOREIGN KEY("+
COLUMN_ID +") "
+ "REFERENCES " + TABLE_NAME +"("+COLUMN_ID +")"+ ");";
sqLiteDatabase.execSQL(query1);
}
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;
}
ActivityClass
public class AddItems extends AppCompatActivity {
Toolbar mToolbar;
DatabaseHelper myDB;
ArrayList<String> listitems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_items);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_button);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShowPopup();
}
});
myDB = new DatabaseHelper(AddItems.this);
listitems = new ArrayList<>();
DisplayList();
}
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));
}
}
}
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) {
// Toast.makeText(AddItems.this, "add called", Toast.LENGTH_SHORT).show();
String name = lname.getText().toString();
if (!TextUtils.isEmpty(lname.getText().toString())) {
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
db.itemlist(name);
Toast.makeText(AddItems.this, "Added Sucessfully !", Toast.LENGTH_SHORT).show();
ShowPopup();
} else
Toast.makeText(AddItems.this, "The name cannot be empty!", Toast.LENGTH_LONG).show();
}
});
}
Aucun commentaire:
Enregistrer un commentaire