我在从数据库中提取数据以在输入新文本之前将其插入edittext时遇到问题。我知道在xml edittext android:text中有可能,但我担心会涉及更多编码。 基本上我希望用户在edittext(编辑相应的行)时保留先前输入的值或将其更改为新值。 如有必要,我可以发布我的文件。 亲切的问候你的帮助。
这是我的Diary文件,它使用相应的EditText字段更新行:
package com.example.classorganizer;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.cookbook.data.MyDB;
public class Diary extends Activity {
EditText titleET1;
EditText titleET2;
EditText titleET3;
EditText titleET4;
EditText titleET5;
EditText titleET6;
EditText titleET7;
EditText titleET8;
EditText titleET9;
EditText titleET10;
EditText titleET11;
EditText titleET12;
EditText titleET13;
EditText titleET14;
EditText titleET15;
EditText titleET16;
EditText titleET17;
EditText titleET18;
EditText titleET19;
EditText titleET20;
EditText titleET21;
EditText titleET22;
EditText titleET23;
EditText titleET24;
EditText titleET25;
EditText titleET26;
EditText titleET27;
EditText titleET28;
EditText titleET29;
EditText titleET30;
EditText titleET31;
EditText titleET32;
EditText titleET33;
EditText titleET34;
EditText titleET35;
EditText titleET36;
EditText titleET37;
EditText titleET38;
EditText titleET39;
EditText titleET40;
EditText titleET41;
EditText titleET42;
EditText titleET43;
EditText titleET44;
EditText titleET45;
EditText titleET46;
EditText titleET47;
EditText titleET48;
Button submitBT;
MyDB dba;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diary);
dba = new MyDB(this);
dba.open();
titleET1 = (EditText)findViewById(R.id.diary1);
titleET2 = (EditText)findViewById(R.id.diary2);
titleET3 = (EditText)findViewById(R.id.diary3);
titleET4 = (EditText)findViewById(R.id.diary4);
titleET5 = (EditText)findViewById(R.id.diary5);
titleET6 = (EditText)findViewById(R.id.diary6);
titleET7 = (EditText)findViewById(R.id.diary7);
titleET8 = (EditText)findViewById(R.id.diary8);
titleET9 = (EditText)findViewById(R.id.diary9);
titleET10 = (EditText)findViewById(R.id.diary10);
titleET11 = (EditText)findViewById(R.id.diary11);
titleET12 = (EditText)findViewById(R.id.diary12);
titleET13 = (EditText)findViewById(R.id.diary13);
titleET14 = (EditText)findViewById(R.id.diary14);
titleET15 = (EditText)findViewById(R.id.diary15);
titleET16 = (EditText)findViewById(R.id.diary16);
titleET17 = (EditText)findViewById(R.id.diary17);
titleET18 = (EditText)findViewById(R.id.diary18);
titleET19 = (EditText)findViewById(R.id.diary19);
titleET20 = (EditText)findViewById(R.id.diary20);
titleET21 = (EditText)findViewById(R.id.diary21);
titleET22 = (EditText)findViewById(R.id.diary22);
titleET23 = (EditText)findViewById(R.id.diary23);
titleET24 = (EditText)findViewById(R.id.diary24);
titleET25 = (EditText)findViewById(R.id.diary25);
titleET26 = (EditText)findViewById(R.id.diary26);
titleET27 = (EditText)findViewById(R.id.diary27);
titleET28 = (EditText)findViewById(R.id.diary28);
titleET29 = (EditText)findViewById(R.id.diary29);
titleET30 = (EditText)findViewById(R.id.diary30);
titleET31 = (EditText)findViewById(R.id.diary31);
titleET32 = (EditText)findViewById(R.id.diary32);
titleET33 = (EditText)findViewById(R.id.diary33);
titleET34 = (EditText)findViewById(R.id.diary34);
titleET35 = (EditText)findViewById(R.id.diary35);
titleET36 = (EditText)findViewById(R.id.diary36);
titleET37 = (EditText)findViewById(R.id.diary37);
titleET38 = (EditText)findViewById(R.id.diary38);
titleET39 = (EditText)findViewById(R.id.diary39);
titleET40 = (EditText)findViewById(R.id.diary40);
titleET41 = (EditText)findViewById(R.id.diary41);
titleET42 = (EditText)findViewById(R.id.diary42);
titleET43 = (EditText)findViewById(R.id.diary43);
titleET44 = (EditText)findViewById(R.id.diary44);
titleET45 = (EditText)findViewById(R.id.diary45);
titleET46 = (EditText)findViewById(R.id.diary46);
titleET47 = (EditText)findViewById(R.id.diary47);
titleET48 = (EditText)findViewById(R.id.diary48);
submitBT = (Button)findViewById(R.id.submitButton);
submitBT.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
saveItToDB();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void saveItToDB() {
dba.updateDiaryEntry(titleET1.getText().toString(), 1);
dba.updateDiaryEntry(titleET2.getText().toString(), 2);
dba.updateDiaryEntry(titleET3.getText().toString(), 3);
dba.updateDiaryEntry(titleET4.getText().toString(), 4);
dba.updateDiaryEntry(titleET5.getText().toString(), 5);
dba.updateDiaryEntry(titleET6.getText().toString(), 6);
dba.updateDiaryEntry(titleET7.getText().toString(), 7);
dba.updateDiaryEntry(titleET8.getText().toString(), 8);
dba.updateDiaryEntry(titleET9.getText().toString(), 9);
dba.updateDiaryEntry(titleET10.getText().toString(), 10);
dba.updateDiaryEntry(titleET11.getText().toString(), 11);
dba.updateDiaryEntry(titleET12.getText().toString(), 12);
dba.updateDiaryEntry(titleET13.getText().toString(), 13);
dba.updateDiaryEntry(titleET14.getText().toString(), 14);
dba.updateDiaryEntry(titleET15.getText().toString(), 15);
dba.updateDiaryEntry(titleET16.getText().toString(), 16);
dba.updateDiaryEntry(titleET17.getText().toString(), 17);
dba.updateDiaryEntry(titleET18.getText().toString(), 18);
dba.updateDiaryEntry(titleET19.getText().toString(), 19);
dba.updateDiaryEntry(titleET20.getText().toString(), 20);
dba.updateDiaryEntry(titleET21.getText().toString(), 21);
dba.updateDiaryEntry(titleET22.getText().toString(), 22);
dba.updateDiaryEntry(titleET23.getText().toString(), 23);
dba.updateDiaryEntry(titleET24.getText().toString(), 24);
dba.updateDiaryEntry(titleET25.getText().toString(), 25);
dba.updateDiaryEntry(titleET26.getText().toString(), 26);
dba.updateDiaryEntry(titleET27.getText().toString(), 27);
dba.updateDiaryEntry(titleET28.getText().toString(), 28);
dba.updateDiaryEntry(titleET29.getText().toString(), 29);
dba.updateDiaryEntry(titleET30.getText().toString(), 30);
dba.updateDiaryEntry(titleET31.getText().toString(), 31);
dba.updateDiaryEntry(titleET32.getText().toString(), 32);
dba.updateDiaryEntry(titleET33.getText().toString(), 33);
dba.updateDiaryEntry(titleET34.getText().toString(), 34);
dba.updateDiaryEntry(titleET35.getText().toString(), 35);
dba.updateDiaryEntry(titleET36.getText().toString(), 36);
dba.updateDiaryEntry(titleET37.getText().toString(), 37);
dba.updateDiaryEntry(titleET38.getText().toString(), 38);
dba.updateDiaryEntry(titleET39.getText().toString(), 39);
dba.updateDiaryEntry(titleET40.getText().toString(), 40);
dba.updateDiaryEntry(titleET41.getText().toString(), 41);
dba.updateDiaryEntry(titleET42.getText().toString(), 42);
dba.updateDiaryEntry(titleET43.getText().toString(), 43);
dba.updateDiaryEntry(titleET44.getText().toString(), 44);
dba.updateDiaryEntry(titleET45.getText().toString(), 45);
dba.updateDiaryEntry(titleET46.getText().toString(), 46);
dba.updateDiaryEntry(titleET47.getText().toString(), 47);
dba.updateDiaryEntry(titleET48.getText().toString(), 48);
dba.close();
titleET1.setText("");
titleET2.setText("");
titleET3.setText("");
titleET4.setText("");
titleET5.setText("");
titleET6.setText("");
titleET7.setText("");
titleET8.setText("");
titleET9.setText("");
titleET10.setText("");
titleET11.setText("");
titleET12.setText("");
titleET13.setText("");
titleET14.setText("");
titleET15.setText("");
titleET16.setText("");
titleET17.setText("");
titleET18.setText("");
titleET19.setText("");
titleET20.setText("");
titleET21.setText("");
titleET22.setText("");
titleET23.setText("");
titleET24.setText("");
titleET25.setText("");
titleET26.setText("");
titleET27.setText("");
titleET28.setText("");
titleET29.setText("");
titleET30.setText("");
titleET31.setText("");
titleET32.setText("");
titleET33.setText("");
titleET34.setText("");
titleET35.setText("");
titleET36.setText("");
titleET37.setText("");
titleET38.setText("");
titleET39.setText("");
titleET40.setText("");
titleET41.setText("");
titleET42.setText("");
titleET43.setText("");
titleET44.setText("");
titleET45.setText("");
titleET46.setText("");
titleET47.setText("");
titleET48.setText("");
Intent i = new Intent(Diary.this, Monday.class);
startActivity(i);
}
/** Called when the user clicks the Back button */
public void visitMonday(View view) {
Intent intent = new Intent(this, Monday.class);
startActivity(intent);
}
}
使用EditText字段填充diary.xml。
这是显示行列表的文件:
import java.sql.Date;
import java.text.DateFormat;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.cookbook.data.Constants;
import com.cookbook.data.MyDB;
public class Monday extends ListActivity {
MyDB dba;
DiaryAdapter myAdapter;
private class MyDiary{
public MyDiary(String t, String c){
title=t;
content=c;
}
public String title;
public String content;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
dba = new MyDB(this);
dba.open();
setContentView(R.layout.fragment_monday);
super.onCreate(savedInstanceState);
myAdapter = new DiaryAdapter(this);
this.setListAdapter(myAdapter);
}
private class DiaryAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<MyDiary> fragment_monday;
public DiaryAdapter(Context context) {
mInflater = LayoutInflater.from(context);
fragment_monday = new ArrayList<MyDiary>();
getdata();
}
public void getdata(){
Cursor c = dba.getdiaries();
startManagingCursor(c);
if(c.moveToFirst()){
do{
String title =
c.getString(c.getColumnIndex(Constants.TITLE_NAME));
String content =
c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
MyDiary temp = new MyDiary(title,content);
fragment_monday.add(temp);
} while(c.moveToNext());
}
}
@Override
public int getCount() {return fragment_monday.size();}
public MyDiary getItem(int i) {return fragment_monday.get(i);}
public long getItemId(int i) {return i;}
public View getView(int arg0, View arg1, ViewGroup arg2) {
final ViewHolder holder;
View v = arg1;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.diaryrow, null);
holder = new ViewHolder();
holder.mTitle = (TextView)v.findViewById(R.id.name);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.mdiary = getItem(arg0);
holder.mTitle.setText(holder.mdiary.title);
v.setTag(holder);
return v;
}
public class ViewHolder {
MyDiary mdiary;
TextView mTitle;
}
}
/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
Intent intent = new Intent(this, Diary.class);
startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
Intent intent = new Intent(this, DisplayScheduleScreen.class);
startActivity(intent);
}
}