0

我有一个应用程序,用户将在其中单击列表视图中的项目,然后保存、取消或删除该条目(这是在单击项目时启动的新活动上完成的)。因此,在单击项目时启动的活动中,单击任一按钮后,我调用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();
}
}

此类编辑数据库条目

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>
4

2 回答 2

1

通过 startActivityForResult 方法启动 EditClass 活动。在调用活动中实现 onActivityResult 方法,并在该方法中调用 adapter.notifyDatasetChanged。

于 2013-01-09T18:12:13.327 回答
0
Intent ourIntent = new Intent(Flashcards_List.this, editClass);
ourIntent.putExtras(specificCard);//passing the bundle to the activity
startActivity(ourIntent);
adapter.notifyDataSetChanged();  

notifyDataSetChanged调用后立即执行startActivity(),它不会阻塞。相反,您应该将数据加载到 中onResume(),这样活动返回时将始终显示最新数据。基本上只留下setContentView()你的onCreate.

于 2013-01-09T18:19:58.810 回答