我正在创建一个应用程序,您可以使用意图发送电子邮件,如果用户想将消息保存为草稿,我想使用数据库将消息保存为草稿。 发送邮件的代码有效但我收到了数据库插入部分的多个错误。 我想在listview中只显示edittext上的一些字段 DBHelper.classs
public class DBHelper {
private static final int DATABASE_VERSION = 1;// Table name
public static final String DATABASE_NAME = "mydb";
//public static final String COLUMN_FROM = "From";
public static final String TABLE_NAME = "drafts";
public static final String _TO = "To";
public static final String _ID = "id";
public static final String _SUBJ = "Subject";
public static final String _CC = "CC";
public static final String _BODY = "Body";
private DatabaseHelper dbhelper;
private SQLiteDatabase db;
private final Context context;
public DBHelper(Context aContext) {
this.context=aContext;
dbhelper = new DatabaseHelper(aContext);
}
public void open() {
db = dbhelper.getReadableDatabase();
}
public long insertdraft(String Id, String To, String CC, String Subject, String Body) {
ContentValues contentValues = new ContentValues();
contentValues.put("ID", Id);
contentValues.put("To", To);
contentValues.put("CC", CC);
contentValues.put("Subject", Subject);
contentValues.put("Body", Body);
return db.insert(TABLE_NAME, null, contentValues);
}
public void insert(ContentValues cv){
db.insertOrThrow(TABLE_NAME,null,cv);
}
public Cursor getAllDrafts() {
String buildsql = "select * from drafts";
return db.query(TABLE_NAME, new String[]{_ID, _TO, _CC, _SUBJ, _BODY}, null, null, null, null, null);
}
private class DatabaseHelper extends SQLiteOpenHelper {
static final String TAG = "TAG";
public DatabaseHelper(Context aContext) {
super(aContext, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
// Create your tables here
String buildSQL = "CREATE TABLE drafts(id text, To text, CC text, Subject text, Body text)";
Log.d(TAG, "onCreate SQL: " + buildSQL);
database.execSQL(buildSQL);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
// Database schema upgrade code goes here
String buildSQL = "DROP TABLE IF EXISTS " + TABLE_NAME;
Log.d(TAG, "onUpgrade SQL: " + buildSQL);// drop previous table
onCreate(sqLiteDatabase); // create the table from the beginning
}
public void close() {
dbhelper.close();
}
}
}
主要活动:
public class MainActivity extends Activity {
EditText e,e0,e1,e2,e3,e4,e5;
int id=1;
private ListView lstNames;
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBHelper db;
setContentView(R.layout.activity_main);
// Read and show the contacts
showContacts();
final Button send = (Button) this.findViewById(R.id.button);
final Button save= (Button)this.findViewById(R.id.button1);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("SendMailActivity", "Send Button Clicked.");
String fromEmail = ((EditText) findViewById(R.id.textfrom)).getText().toString();
String fromPassword = ((EditText) findViewById(R.id.textpass)).getText().toString();
AutoCompleteTextView act= (AutoCompleteTextView) findViewById(R.id.textto);
String toEmails=act.getText().toString();
String ccEmails = ((EditText) findViewById(R.id.textcc)).getText().toString();
Log.i("SendMailActivity", "To List: " + toEmails);
String emailSubject = ((TextView) findViewById(R.id.textsub)).getText().toString();
String emailBody = ((TextView) findViewById(R.id.textmsg)).getText().toString();
if (!ccEmails.contains("@"))
ccEmails = "";
new SendMailTask(MainActivity.this).execute(fromEmail, fromPassword, toEmails, ccEmails, emailSubject, emailBody);
}
});
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText e = (EditText) findViewById(R.id.textpass);
EditText e0 = (EditText) findViewById(R.id.textfrom);
EditText e1 = (EditText) findViewById(R.id.textto);
EditText e2 = (EditText) findViewById(R.id.textcc);
EditText e3 = (EditText) findViewById(R.id.textsub);
EditText e4 = (EditText) findViewById(R.id.textmsg);
//Log.i("SaveDraftActivity", "Save Button Clicked.");
id++;
String Id = Integer.toString(id);
String toEmail = e1.getText().toString();
String ccEmail = e2.getText().toString();
String emailSubj = e3.getText().toString();
String emailBod = e4.getText().toString();
if (toEmail.length() != 0 && emailSubj.length() != 0) {
//db.insertdraft(Id,toEmail,ccEmail,emailSubj,emailBod);
Intent intent = new Intent(MainActivity.this, DraftActivity.class);
//intent.putExtra("Id",Id);
intent.putExtra("ETo", toEmail);
intent.putExtra("CC", ccEmail);
intent.putExtra("Subject", emailSubj);
intent.putExtra("Body", emailBod);
startActivity(intent);
}
/* e.setText("");
e0.setText("");
e1.setText("");
e2.setText("");
e3.setText("");
e4.setText("");*/
}
});
}
CustomAdapter类:
public class CustomAdapter extends CursorAdapter {
public CustomAdapter(Context context,Cursor c){
super(context,c);
}
public View newView(Context context,Cursor cursor,ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.list_item_data, parent, false);
return retView;
}
public void bindView(View view, Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewto = (TextView) view.findViewById(R.id.dbto);
textViewto.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
TextView textViewsub = (TextView) view.findViewById(R.id.dbsub);
textViewsub.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4))));
TextView textViewbody = (TextView) view.findViewById(R.id.dbbody);
textViewbody.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(5))));
}
}
草案活动
public class DraftActivity extends AppCompatActivity {
private CustomAdapter customAdapter;
public static ArrayList<String> ArrayDraft = new ArrayList<String>();
private DBHelper db;
private ListView listView;
String s1,s2,s3,s4,s5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draft);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
db=new DBHelper(this);
listView = (ListView) this.findViewById(R.id.list);
Intent in=getIntent();
//s1=in.getExtras().getString("Id");
//Integer i=Integer.parseInt(s1);
s2=in.getExtras().getString("ETo");
s3=in.getExtras().getString("CC");
s4=in.getExtras().getString("Subject");
s5=in.getExtras().getString("Body");
/*final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ArrayDraft);
listView.setAdapter(adapter);
registerForContextMenu(listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String namez = adapter.getItem(i);
Toast.makeText(DraftActivity.this, namez, Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(DraftActivity.this,MainActivity.class);//.putExtra(Intent.EXTRA_TEXT, namez);
}
});*/
new Handler().post(new Runnable() {
public void run() {
customAdapter = new CustomAdapter(DraftActivity.this, db.getAllDrafts());
listView.setAdapter(customAdapter);
}
});
db.insertdraft(s2,s3,s4,s5);
customAdapter.changeCursor(db.getAllDrafts());
}
}
谢谢:)