简体   繁体   English

Android Studio使用onClick按钮将EditText输入添加到ListView

[英]Android Studio add EditText input to a ListView with button onClick

I am trying to make a simple Planner app so I have an XML file with a ListView on it, and another XML file with an EditText and a sumbit button on it, and of course my Java files. 我正在尝试制作一个简单的Planner应用程序,所以我有一个带有ListView的XML文件,还有一个带有EditText和sumbit按钮的XML文件,当然还有我的Java文件。 I have done a ton of research but I can't find a working method to make it when I press the button, it adds the EditText stuff to the ListView as an item. 我已经做了大量研究,但是当我按下按钮时,我找不到一种可行的方法来完成它,它会将EditText内容作为一项添加到ListView中。 Here is my code: 这是我的代码:

activity_main.xml activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:layout_width="300dp"
        android:layout_height="fill_parent"
        android:id="@+id/event_list"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@+id/datePicker" />

    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

    <CalendarView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/calendarView"
        android:layout_alignBottom="@+id/datePicker"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Event"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_alignEnd="@+id/datePicker"
        android:layout_toEndOf="@+id/event_list"
        android:background="@android:color/holo_blue_light"
        android:onClick="newEvent"
        android:textSize="20dp"
        android:textColor="@android:color/white" />
</RelativeLayout>

new_plan.xml new_plan.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/event_name"
        android:hint="Event Name"
        android:inputType="textCapWords"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/event_date"
        android:hint="Event Date" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/button2"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/holo_blue_light"
        android:textSize="20dp"
        android:textColor="@android:color/white" />

</LinearLayout>

MakePlan.java MakePlan.java

package com.kass.planner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MakePlan extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_plan);
    }

}

MainActivity.java MainActivity.java

    package com.kass.planner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void newEvent(View v){
        Intent i = new Intent(this, MakePlan.class);
        startActivity(i);}}

Please help, thank you. 请帮忙,谢谢。

Take a look on this google developers project. 看看这个Google Developers项目。 It's for animations but i think it also have the solution to your question. 它用于动画,但我认为它也可以解决您的问题。

https://developer.android.com/training/animation/layout.html https://developer.android.com/training/animation/layout.html

You should append data to the collection of items linked to your ListView via your adapter and notify your adapter that your data has changed. 您应该将数据附加到通过适配器链接到ListView的项目集合中,并通知适配器数据已更改。 This illustration assumes that you have a List of String objects. 此插图假定您具有一个String对象列表。

EditText editText = ...
List<String> list = ...
YourAdapter adapter = ...
ListView listView = ...

You must have done something like this to setup your listView. 您必须已完成类似的操作才能设置listView。

 listView.setAdapter(new YourAdapter(list, this));

Now on button click you should get the String in EditText, add it to your List and finally notify the adapter that your data has changed in order to update your ListView for eg 现在单击按钮,您应该在EditText中获取字符串,将其添加到列表中,最后通知适配器您的数据已更改,以便更新您的ListView,例如

void OnButtonClick(View view){
    String strToAdd = editText.getText().toString();
    list.add(strToAdd);
    adapter.notifyDataSetChanged();
}

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

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