Android-从另一个活动增加项目的ListView onButtonClick [英] Android- Adding item to ListView from another activity onButtonClick

查看:157
本文介绍了Android-从另一个活动增加项目的ListView onButtonClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我的code不工作,我看了这个网站上线的其余部分,遵循了方向,但似乎仍有一定误差。

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.

之后,他们将点击提交按钮。当他们点击此按钮提交,我希望用户被带到另一个页面,在这里他们将能够看到自己submitions历史在ListView。

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);

}

在此之后,previousPractices类应该打开。显示这个类的onCreate方法:

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);

行,它成功地进入到正确的ListView的页面,但是没有显示出什么(因为没有适配器设置)。

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

推荐答案

试试这个:

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();
}

我改变你的测试字符串来描述,因为日期是没有得​​到设置(在previous活动不收取额外的日期)。另外,我的ListView.setAdapter前移到你的ArrayList.add()至()。试试这个,看看它是否工作。

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.

这篇关于Android-从另一个活动增加项目的ListView onButtonClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆