如何将editText中的字符串添加到listView

时间:2017-02-15 00:54:00

标签: java android listview android-intent android-edittext

我有一个MainActivity ListView和一个Button。当用户点击添加Button时,它会转到另一个活动CreateActivity,您可以在数据或字符串中输入editText框。之后,您点击了创建Button,然后将strings添加到它显示的ListView,但每次我尝试将另一个项目添加到ListView时,它都会覆盖第一项,但不会添加到ListView

带有listView和添加按钮的MainActivity.java

package com.example.brian.inventoryapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static android.R.layout.simple_list_item_1;


public class MainActivity extends AppCompatActivity {

    Button addButton;
    ListView itemListView;
    public static ArrayList<String> arrayList;
    public static ArrayAdapter arrayAdapter;

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



        Bundle editTextData = getIntent().getExtras();

        if(editTextData != null){

            itemListView = (ListView)findViewById(R.id.itemListView);
            String data = editTextData.getString("data");
            arrayList = new ArrayList<String>();
            arrayList.add(data);
            arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
            itemListView.setAdapter(arrayAdapter);

            if(data != ""){

                    arrayAdapter.notifyDataSetChanged();
            }
        }


        addButton = (Button) findViewById(R.id.addButton);
        addButton.setOnClickListener(new View.OnClickListener(){
           @Override
           public void onClick(View v) {
               Intent intent = new Intent(MainActivity.this, CreateActivity.class);
               startActivity(intent);
           }

       });
    }

}

带有listView的activity_main xml并添加按钮

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.brian.inventoryapp.MainActivity"
    android:weightSum="1">

    <Button
        android:text="Add"
        android:layout_width="wrap_content"
        android:layout_height="58dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/addButton" />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/itemListView"
         />

</RelativeLayout>

CreateActivity.java包含四个editText框和创建按钮

package com.example.brian.inventoryapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import static com.example.brian.inventoryapp.R.id.editTextItem;

public class CreateActivity extends AppCompatActivity {

    EditText editTextItem;
    EditText editTextModel;
    EditText editTextSerial;
    EditText editTextID;
    Button createButton;

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

        editTextItem = (EditText)findViewById(R.id.editTextItem);
        editTextModel = (EditText)findViewById(R.id.editTextModel);
        editTextSerial = (EditText)findViewById(R.id.editTextSerial);
        editTextID = (EditText)findViewById(R.id.editTextID);
        createButton = (Button) findViewById(R.id.createButton);

        createButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String item = "";
                item = editTextItem.getText().toString().trim() +
                              editTextModel.getText().toString().trim() +
                              editTextSerial.getText().toString().trim() +
                              editTextID.getText().toString().trim();

                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.putExtra("data", item);
                startActivity(intent);
            }
        });

    }
}
使用四个editText框和创建按钮

activity_create xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_create"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.brian.inventoryapp.CreateActivity">

    <TextView
        android:text="Item Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/textView" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/editTextItem" />

    <TextView
        android:text="Model Number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="18dp"
        android:id="@+id/textView2"
        android:layout_below="@+id/editTextItem"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/editTextModel" />

    <TextView
        android:text="Serial Number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextModel"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="18dp"
        android:id="@+id/textView3" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/editTextSerial" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_below="@+id/editTextID"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp"
        android:id="@+id/imageButton" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editTextID"
        android:layout_below="@+id/editTextSerial"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="18dp" />

    <TextView
        android:text="ID Number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView4"
        android:layout_below="@+id/editTextSerial"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="Create"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/createButton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

每次重新启动MainActivity时,都会创建onCreate方法。可以使用注释方法的一个方法:use startactivityforResult; 你可以阅读这个问题 how to manage startactivityforResult。第二种方法是将MainActivity模型设置为SingleInstance。

答案 1 :(得分:0)

您一遍又一遍地启动MainActivity,清除之前加载的所有数据。

试试这个解决方案

主要活动:

 createButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            String item = "";
            item = editTextItem.getText().toString().trim() +
                    editTextModel.getText().toString().trim() +
                    editTextSerial.getText().toString().trim() +
                    editTextID.getText().toString().trim();

            Intent intent = new Intent();
            intent.putExtra("data", item);

            setResult(RESULT_OK, intent);
            finish();
        }
    });

在您的CreateActivity中,将 string fileName = Path.GetFileName(filePath); files.Add(new ListItem(fileName, "C:/Image_folder/" + fileName)); 侦听器更改为:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
    <hr />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowHeader="false">
        <Columns>
            <asp:BoundField DataField="Text" />
            <asp:ImageField  DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" />
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>

答案 2 :(得分:0)

是的,它会假设MainActivity首次启动,因此您应该使用startActivityForResult()方法而不是startActivity()。这将继续您的MainActivity并且不会覆盖您的列表View。