I created an App to get value from user via EditText and store it in Database. I have no problem with that. Now i have to display the datas of Database in a ListView. Here i used an AlertDialog to display them. My Code is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
SQLiteDatabase db;
db=openOrCreateDatabase("Mute_List", MODE_APPEND, null);
db.execSQL("create table if not exists user(Number INTEGER);");
Button add=(Button)findViewById(R.id.button1);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText num=(EditText)findViewById(R.id.editText1);
SQLiteDatabase db;
db=openOrCreateDatabase("Mute_List", MODE_APPEND, null);
db.execSQL("create table if not exists user(Number INTEGER);");
String cno=num.getText().toString();
if(cno.length()==0){
show("Error", "Field Cannot be Empty");
}else{
db.execSQL("insert into user values('"+cno+"');");
show("Success", "Added Successfully");
}
}
});
Button show=(Button)findViewById(R.id.button2);
show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db;
db=openOrCreateDatabase("Mute_List", MODE_APPEND, null);
db.execSQL("create table if not exists user(Number INTEGER);");
Cursor c=db.rawQuery("select*from user",null);
if (c.getCount()==0)
{
show("Error","No Records Found" );
//Toast.makeText(getApplicationContext(), "No Records Found", Toast.LENGTH_SHORT).show();
return;
}
StringBuffer buffer=new StringBuffer();
while (c.moveToNext())
{
buffer.append(c.getString(0)+"\n");
}
show("User Details", buffer.toString());
}
});
}
public void show(String title, String message) {
AlertDialog.Builder builder=new AlertDialog.Builder(SecondActivity.this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
Please help me.
Aucun commentaire:
Enregistrer un commentaire