简体   繁体   English

Android-从另一个活动onButtonClick向ListView添加项目

[英]Android- Adding item to ListView from another activity onButtonClick

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. 当他们单击此提交按钮时,我希望将用户带到另一个页面,在那里他们将能够在ListView中查看其子项目的历史记录。

In the first Activity, when the "Submit" button is clicked, this method will be called: 在第一个Activity中,单击“Submit”按钮时,将调用此方法:

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. 在此之后,应该打开PreviousPractices类。 The onCreate method for this class is shown: 显示此类的onCreate方法:

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). line,它成功转到正确的ListView页面,但是没有显示(因为没有设置适配器)。

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(). 此外,我将您的ArrayList.add()移动到ListView.setAdapter()之前。 Try this and see if it works. 试试这个,看它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM