aladinsane7 aladinsane7 - 7 months ago 12
Java Question

Using startActivityForResult to Add Data To List

I'm new to Android. I want to create a directory in my app. I will only display names there. I'm using Directory class to display names and AddPerson class to add there. However AddPerson is not working. Here is my Directory:

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

}


This is the AddPerson class:
package 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();
}
}

);

}
}

Answer

This if is always false because you are returning 1 as the resultCode:

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

You need to return the correct result code with setResult(RESULT_OK, returnIntent);

On top of that, each time you modify your dataset you need to notify the ListView's adapter that it has changed. Move your adapter to a class variable and call adapter.notifyDataSetChanged(); after adding the resulting person.