package com.lin.db;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import com.example.chat1.R;
public class UserActivity extends ListActivity {
private DBOpration db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
db=new DBOpration(this);
db.openOrCreateDB();
display();
}
private void display() {
Cursor c=db.selectAll();
SimpleCursorAdapter sa=new SimpleCursorAdapter(this,R.layout.dbactivity,
c, new String[]{
"name","address"}, new int[]{R.id.textView1,R.id.textView2});
setListAdapter(sa);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
if(item.getTitle()=="添加"){
<span style="color:#ff0000;">showDialog(123);</span>
}
return super.onMenuItemSelected(featureId, item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(1, 1, 1, "添加");
menu.add(1,2,2,"查询");
return super.onCreateOptionsMenu(menu);
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
<span style="color:#ff0000;">if(id==123</span>){
AlertDialog ad=null;
ad=new AlertDialog.Builder(this).setTitle("添加")
.setView(LayoutInflater.from(this).inflate(R.layout.dbdialog, null)).
setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
AlertDialog aa=(AlertDialog) dialog;
EditText name1=(EditText)aa.findViewById(R.id.name1);
EditText address1=(EditText)aa. findViewById(R.id.address1);
User user=new User(name1.getText().toString(), address1.getText().toString());
db.insert(user);
display();
}
}).create();
ad.show();
}
return super.onCreateDialog(id);
}
oncreatedialog()与showdialog()的使用
最新推荐文章于 2023-09-05 13:39:36 发布