我正在處理應用程序,其中用戶鍵入他朋友的個人信息並保存到SQLite數據庫中。然後用戶可以在另一個頁面(活動)中查看他的朋友的信息。但是當用戶需要查看他朋友的信息時,它不會顯示在第二個活動中。這些信息需要從數據庫表中檢索。我怎麼做?如何將數據庫表中的數據顯示到Android TextView中?
這裏有碼 -
第一項活動 -
public class PersonalInformation extends Activity
{
EditText txtLikes, txtDislikes, txtType, txtDate;
Button btnView, btnBack;
Spinner nameSpinner;
ArrayAdapter<String> adapter;
String friends[] = {"Kanak Priya", "Joanne Liew", "Melissa Haiting", "Michelle Lam", "Teo Kin Hua", "David Yeo", "Nur Ashiqin", "Stephanie"};
final Context context = this;
private int namesSpinnerId;
LikesDBAdapter likeDB = new LikesDBAdapter(this);
DislikesDBAdapter dlikeDB = new DislikesDBAdapter(this);
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
adapter = new ArrayAdapter<String>(this,
R.layout.spinner_text, friends);
nameSpinner.setAdapter(adapter);
/*BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);
// buddyDB.open();
Cursor friendsCursor = buddyDB.getAllNames();
startManagingCursor(friendsCursor);
String[] from = new String[]{BuddyDBAdapter.KEY_NAME};
int[] to = new int[]{R.id.name};
SimpleCursorAdapter friendsCursorAdapter = new SimpleCursorAdapter(this, R.layout.spinner_text, friendsCursor, from, to);
friendsCursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
nameSpinner = (Spinner)findViewById(R.id.nameSpinner);
nameSpinner.setAdapter(friendsCursorAdapter);
//buddyDB.close();
nameSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
Cursor c = (Cursor)parent.getItemAtPosition(pos);
namesSpinnerId = c.getInt(c.getColumnIndexOrThrow(BuddyDBAdapter.KEY_ROWID));
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
//buddyDB.close();
*/
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
likeDB.open();
long like_id;
nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
String NameValue = nameSpinner.getSelectedItem().toString();
txtLikes = (EditText) findViewById(R.id.txtLikes);
String LikeValue = txtLikes.getText().toString();
like_id = likeDB.insertLikes(LikeValue, NameValue);
likeDB.close();
dlikeDB.open();
long dlike_id;
nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
String NamesValue = nameSpinner.getSelectedItem().toString();
txtDislikes = (EditText) findViewById(R.id.txtDislikes);
String DislikeValue = txtDislikes.getText().toString();
dlike_id = dlikeDB.insertDislikes(DislikeValue, NamesValue);
dlikeDB.close();
txtDate = (EditText) findViewById(R.id.txtDate);
txtType = (EditText) findViewById(R.id.txtType);
txtLikes = (EditText) findViewById(R.id.txtLikes);
txtDislikes = (EditText) findViewById(R.id.txtDislikes);
if(txtDate.getText().toString().length() == 0 || txtType.getText().toString().length() == 0 ||
txtLikes.getText().toString().length() == 0 || txtDislikes.getText().toString().length() == 0)
{
txtDate.setError("Please key in the date");
txtType.setError("Please key in category");
txtLikes.setError("Please key in their likes");
txtDislikes.setError("Please key in their dislikes");
}
else
{
Toast.makeText(getBaseContext(), "Your information is saved successfully! :D", Toast.LENGTH_LONG).show();
}
}
});
btnBack = (Button) findViewById(R.id.btnMain);
btnBack.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
finish();
}
});
btnView = (Button) findViewById(R.id.btnView);
btnView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
EditText txtDate = (EditText) findViewById(R.id.txtDate);
EditText txtType = (EditText) findViewById(R.id.txtType);
EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
Intent i = new Intent(PersonalInformation.this, SavedInfo.class);
Bundle b = new Bundle();
b.putString("name", nameSpinner.getSelectedItem().toString());
b.putString("date", txtDate.getText().toString());
b.putString("category", txtType.getText().toString());
b.putString("likes", txtLikes.getText().toString());
b.putString("dislikes", txtDislikes.getText().toString());
i.putExtras(b);
startActivity(i);
}
});
EditText txtDate = (EditText) findViewById(R.id.txtDate);
txtDate.setHint("birthdate or wedding date");
txtDate.setFilters(new InputFilter[] {
new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend)
{
if(source.equals(""))
{
return source;
}
if(source.toString().matches("[0-9 -]+"))
{
return source;
}
return "";
}
}
});
EditText txtType = (EditText) findViewById(R.id.txtType);
txtType.setHint("birthday or wedding");
txtType.setFilters(new InputFilter[] {
new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend)
{
if(source.equals(""))
{
return source;
}
if(source.toString().matches("[birthday wedding Birthday Wedding BIRTHDAY WEDDING]+"))
{
return source;
}
return "";
}
}
});
EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
txtLikes.setHint("e.g. Skating, Photography separate by comma");
txtLikes.setFilters(new InputFilter[] {
new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend)
{
if(source.equals(""))
{
return source;
}
if(source.toString().matches("[a-z A-Z -]+"))
{
return source;
}
return "";
}
}
});
EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
txtDislikes.setHint("e.g. Skating, Photography separate by comma");
txtDislikes.setFilters(new InputFilter[] {
new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend)
{
if(source.equals(""))
{
return source;
}
if(source.toString().matches("[a-z A-Z -]+"))
{
return source;
}
return "";
}
}
});
}
}
第二活性
public class SavedInfo extends Activity
{
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.saved_info);
Bundle b = this.getIntent().getExtras();
String name = b.getString("name");
String date = b.getString("date");
String category = b.getString("category");
String likes = b.getString("likes");
String dislikes = b.getString("dislikes");
((TextView)findViewById(R.id.textName)).setText(name);
((TextView)findViewById(R.id.textDate)).setText(date);
((TextView)findViewById(R.id.textType)).setText(category);
((TextView)findViewById(R.id.textLikes)).setText(likes);
((TextView)findViewById(R.id.textDislikes)).setText(dislikes);
Button btnEdit = (Button) findViewById(R.id.btnEdit);
btnEdit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent backIntent = new Intent(context, PersonalInformation.class);
startActivity(backIntent);
}
});
Button btnBack = (Button) findViewById(R.id.btnMain);
btnBack.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent menuIntent = new Intent(context, MainPage.class);
startActivity(menuIntent);
}
});
}
}
請幫助我如何從數據庫中檢索表中的數據到第2活性的TextView
我會感謝提供的任何幫助。提前致謝! =)
您是否獲取第二個活動中顯示的數據? –
哦,我應該說,在用戶鍵入朋友的詳細信息並查看信息後,第二項活動中將顯示詳細信息。但是,如果用戶返回第一個活動並選擇他的同一個朋友並查看他所鍵入的信息,則數據不存在,並且應該從數據庫表中顯示。讓我知道你是否明白我的意思。謝謝 – Preeyah
你第二次在logcat中得到任何警告/錯誤? –