使用startActivityForResult将数据添加到列表

时间:2016-04-22 19:05:27

标签: java android listview startactivityforresult

我是Android新手。我想在我的应用程序中创建一个目录。我只会在那里显示名字。我正在使用Directory类来显示名称,并使用AddPerson类来添加。但AddPerson无效。这是我的目录:

        package com.example.user.myapplication;

        import android.content.Intent;
        import android.support.annotation.Nullable;
        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.EditText;
        import android.widget.ImageButton;
        import android.widget.ListView;

        import java.util.ArrayList;

        public class Directory extends AppCompatActivity {
            ArrayList<String> personList = new ArrayList<>();

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                ListView list;
                ImageButton addButton;
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_directory);
                list = (ListView) findViewById(R.id.listView);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1, personList);
                list.setAdapter(adapter);
                addButton = (ImageButton)findViewById(R.id.imageButton5);
                addButton.setOnClickListener(
                        new View.OnClickListener() {
                            public void onClick(View v) {
                                Intent intent = new Intent("com.example.user.myapplication.AddPerson");
                                startActivityForResult(intent, 1);
                            }
                        }

                );

            }

            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if ( requestCode == 1 && resultCode == RESULT_OK ){
                    String result=data.getStringExtra("result");
                    personList.add(result);
                }
            }

        }

这是AddPerson类:     包com.example.user.myapplication;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

    public class AddPerson extends AppCompatActivity {
        Button addButton;
        EditText nameText;
        Directory d = new Directory();
        public String name;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_person);
            addButton = (Button) findViewById(R.id.button);
            addButton.setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            nameText = (EditText) findViewById(R.id.editText);
                            EditText mailText = (EditText) findViewById(R.id.editText2);
                            name = nameText.getText().toString();
                            Intent returnIntent = new Intent();
                            returnIntent.putExtra("result", name);
                            setResult(1, returnIntent);
                            finish();
                        }
                    }

            );

        }
}

3 个答案:

答案 0 :(得分:0)

这个if总是假的,因为你将{1}作为resultCode

返回1

if ( requestCode == 1 && resultCode == RESULT_OK )

您需要使用setResult(RESULT_OK, returnIntent);

返回正确的结果代码

最重要的是,每次修改数据集时,都需要通知ListView的适配器它已更改。将适配器移动到类变量,并在添加结果人员后调用adapter.notifyDataSetChanged();

答案 1 :(得分:0)

好吧,最重要的是你没有使用RESULT_OK(或RESULT_CANCELED),也许试试:

public void onClick(View v) {
                            nameText = (EditText) findViewById(R.id.editText);
                            EditText mailText = (EditText) findViewById(R.id.editText2);
                            name = nameText.getText().toString();
                            Intent returnIntent = new Intent();
                            returnIntent.putExtra("result", name);
                            setResult(Activity.RESULT_OK, returnIntent);
                            finish();

另外,为什么要在AddPerson Activity中手动创建目录(在此上下文中是一个Activity)对象?系统是唯一应该创建活动的东西。

答案 2 :(得分:0)

使用setResult(Activity.RESULT_OK, returnIntent);

记住

  

int RESULT_OK的值为常量值:-1(0xffffffff)

然后您将没有问题将数据添加到列表中:

  @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {                   
                if ( requestCode == 1 && resultCode == RESULT_OK ){
                    String result=data.getStringExtra("result");
                    personList.add(result);
                }
            }