Android- Adding item to ListView from another activity onButtonClick

喜夏-厌秋 提交于 2019-12-08 06:52:53

问题


I'm not sure why my code isn't working, I've looked at the rest of the threads on this site and followed the directions but there still seems to be some error.

In my app, there is a page for a user to enter some data, including dates, descriptions, hours, and minutes of duration. The description/date are Strings and hours and minutes are ints. I've focused on only the String date for now to simplify things.

After the user enters this data in the activity, they will click the "Submit" button. When they click this submit button, I want the user to be taken to another page where they will be able to see a history of their submitions in a ListView.

In the first Activity, when the "Submit" button is clicked, this method will be called:

public void newEntrySubmit(View v)
{
    hours = npHours.getValue();
    minutes = npMinutes.getValue();
    description=String.valueOf(etDescription.getText().toString());
    dateOfPractice=String.valueOf(etDate.getText().toString());

    Intent i = new Intent(this, PreviousPractices.class);
    i.putExtra("Hours", hours);
    i.putExtra("Minutes", minutes);
    i.putExtra("Description", description);
    i.putExtra("dateOfPractice", dateOfPractice);
    startActivity(i);

}

After this, the PreviousPractices class should open. The onCreate method for this class is shown:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.previous_practices_layout);

    Bundle extras = getIntent().getExtras();
    hours = extras.getInt("Hours");
    minutes = extras.getInt("Minutes");
    description=extras.getString("Description");
    date = extras.getString("Date");

    practiceList=(ListView)findViewById(R.id.practiceList);
    ArrayList<String> listItems = new ArrayList<String>();
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,listItems);
    practiceList.setAdapter(adapter);
    listItems.add(date);
    adapter.notifyDataSetChanged();
}

The problem is that when I click the "Submit" button, the app crashes. When I remove the

practiceList.setAdapter(adapter);

line, it successfully goes to the correct ListView page, however there is nothing shown(since no adapter is set).


回答1:


Try this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.previous_practices_layout);

    Bundle extras = getIntent().getExtras();
    hours = extras.getInt("Hours");
    minutes = extras.getInt("Minutes");
    description=extras.getString("Description");
    // date = extras.getString("Date");

    practiceList=(ListView)findViewById(R.id.practiceList);
    ArrayList<String> listItems = new ArrayList<String>();
    listItems.add(description);
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,listItems);
    practiceList.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

I changed your test string to description, since date is not getting set (no extra for "Date" in previous activity). Also, I moved your ArrayList.add() to before your ListView.setAdapter(). Try this and see if it works.



来源:https://stackoverflow.com/questions/30542271/android-adding-item-to-listview-from-another-activity-onbuttonclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!