ユーザーがリストビューのアイテムをクリックして、エントリを保存、キャンセル、または削除するアプリケーションがあります (これは、アイテムのクリックで開始される新しいアクティビティで行われます)。item click で開始されるアクティビティでは、いずれかのボタンをクリックした後、finish() を呼び出して前のアクティビティに戻ります。この場合はリストビューのアクティビティです。ただし、リスト内の項目に変更を加えても、リストはまったく変更されません。.notifyDataSetChanged() と invalidateViews の両方を使用してみましたが、どちらも機能しませんでした。これが私のコードです。
public class Flashcards_List extends ListActivity{
String[] All_Cards = null;
ArrayAdapter<String> adapter;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.flashcards_list);
Flashcards Cards = new Flashcards(this);
Cards.open();
All_Cards = Cards.getAllFronts();
listview = (ListView) findViewById(android.R.id.list);
listview.invalidateViews();
adapter = new ArrayAdapter<String>(Flashcards_List.this, android.R.layout.simple_list_item_1, All_Cards);
// Assign adapter to ListView
listview.setAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String Specific_Card = All_Cards[position];
/* Class to assist us in loading the activity */
Class editClass = null;
try {
editClass = Class.forName("com.example.flashcards.Edit_Flashcard");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bundle specificCard = new Bundle();
specificCard.putString("card", Specific_Card);
Intent ourIntent = new Intent(Flashcards_List.this, editClass);
ourIntent.putExtras(specificCard);//passing the bundle to the activity
startActivity(ourIntent);
adapter.notifyDataSetChanged();
}
}
このクラスは DB エントリを編集します
public class Edit_Flashcard extends Activity implements OnClickListener{
String front_of_card = null;
Bundle bundle_received;
Button cancel, delete, save;
EditText front, back;
String[] card;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_flashcard);
bundle_received = getIntent().getExtras();
front_of_card = bundle_received.getString("card");//current card is the card that was clicked on
Flashcards info = new Flashcards(this);
info.open();
card = info.getCard(front_of_card);
info.close();
initialize();
}
public void initialize(){
front = (EditText) findViewById(R.id.front);
back = (EditText) findViewById(R.id.back);
front.setText(card[1]);
back.setText(card[2]);
cancel = (Button) findViewById(R.id.cancel);
save = (Button) findViewById(R.id.save);
delete = (Button) findViewById(R.id.delete);
cancel.setOnClickListener(this);
save.setOnClickListener(this);
delete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.cancel:
super.finish();//finishes the activity, and returns to previous Activity
break;
case R.id.save:
try{
Flashcards update_entry = new Flashcards(this);
update_entry.open();
update_entry.updateEntry(card[0], front.getText().toString(), back.getText().toString());
update_entry.close();
}catch(Exception e){
String save_text = "The Flashcard could not be saved. Please try again.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(Edit_Flashcard.this, save_text, duration);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
super.finish();
break;
case R.id.delete:
try{
Flashcards delete_entry = new Flashcards(this);
delete_entry.open();
delete_entry.deleteEntry(card[0]);
delete_entry.close();
}catch(Exception e){
/* set the value to NOT INSERTED */
String delete_text = "The Flashcard could not be deleted. Please try again.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(Edit_Flashcard.this, delete_text, duration);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
super.finish();
break;
}/* end Switch */
}/* end onClick */
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>