0

I want to make a ListView that gets its elements(strings) from user input. I have a button that directs the user to another activity and while in it, the user enters a name and presses another button to come back to the original activity. The same button gets and adds a string to the ArrayAdapter that the ListView uses and displays it as an element in the ListView. It doesn't seem to work and I know it's a stupid mistake, but I'm fresh to android development and this in particular I haven't done before.

Here's all the code:

the MainActivity

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.ArrayList;


public class MainActivity extends ActionBarActivity {

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

    ArrayList<String> simpleArray =new ArrayList<String>();

    ListAdapter simpleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
            simpleArray);

    ListView lv = (ListView) findViewById(R.id.lv);
    lv.setAdapter(simpleAdapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void enterActivity(View view) {
    Intent toEnterSecond = new Intent(this, SecondActivity.class);
    startActivity(toEnterSecond);
}
}

The second acitivity

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

import java.util.ArrayList;

public class SecondActivity extends Activity{

private EditText projectName;
ArrayList simpleArray;
ArrayAdapter simpleAdapter;

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

    projectName = (EditText) findViewById(R.id.eTxt);

}

public void getBack(View view) {
    String projectCalling = String.valueOf(projectName.getText());
    simpleArray.add(projectCalling);
    simpleAdapter.notifyDataSetChanged();

    Intent comeBack = new Intent(this, MainActivity.class);
    startActivity(comeBack);
}
}

And the layouts

<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">

<Button
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:id="@+id/simpleButton"
    android:text="click me plox"
    android:onClick="enterActivity"/>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/simpleButton"
    android:id="@+id/lv">
</ListView>

</RelativeLayout>

//

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:text="Enter the name of your project:"
    android:id="@+id/txt"/>
<EditText
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:layout_below="@+id/txt"
    android:id="@+id/eTxt"/>
<Button
    android:layout_width="100dp"
    android:layout_height="60dp"
    android:layout_below="@+id/eTxt"
    android:text="click"
    android:onClick="getBack"/>

</RelativeLayout>
Richard
  • 121
  • 10
  • When you go back, you are calling MainActivity again, and you are reseting the list and set the adapter for a empty list in your onCreate method. This no make sense – rafaelasguerra Apr 07 '15 at 18:58
  • You are calling simpleAdapter.notifyDataSetChanged(); from second activity, which is not even initialized i think. Why you have simpleAdapter in second activity? – Jemshit Apr 07 '15 at 18:58
  • The other thing is do not start new activity when trying to go back first activity, pressing back button of phone will do it. Just call finish() – Jemshit Apr 07 '15 at 18:59
  • That's a good question :D I'm a total noob, thought that was the way to do it – Richard Apr 07 '15 at 19:00
  • I wanted to make that when the button in second activity retrieves the input not only will it add it to the listview but also automatically bring the user back to where the list is. In this way the user doesn't have to do it manually – Richard Apr 07 '15 at 19:02
  • @Richard yes, for that call finish() which will finish second activity and go back to mainactivity – Jemshit Apr 07 '15 at 19:03
  • @rguerra im very fresh to android and programming as a whole so I think that im still lacking in a lot of ways :D – Richard Apr 07 '15 at 19:04
  • @JCodex i added finish() instead of the intent but where and how do i setup the simpleAdapter correctly? – Richard Apr 07 '15 at 19:07

1 Answers1

0

Explanation: You can start SecondActivity with startActivityForResult() instead of startActivity as answered here and in onResultActivity method you can call simpleAdapter.notifyDataSetChanged();. Don't call simpleAdapter.notifyDataSetChanged(); in SecondActivity.

To learn how to use startActivityForResult check this.

Also use finish() instead of

Intent comeBack = new Intent(this, MainActivity.class);
startActivity(comeBack);

in SecondActivity to go back to MainActivity.

Solution: Change MainActivity enterActivity method to this:

...
public void enterActivity(View view) {
    Intent toEnterSecond = new Intent(this, SecondActivity.class);
    startActivityForResult(toEnterSecond,1);
}

Add this to end of SecondActivity getBack method:

MainActivity.simpleArray.add(projectCalling);
Intent returnIntent = new Intent();    
setResult(RESULT_OK,returnIntent);
finish();

Finally add this new method to MainActivity:

protected void onActivityResult(int requestCode, int resultCode, Intent data{
    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
             ((ArrayAdapter) simpleAdapter).notifyDataSetChanged();
        }

    }
}

To be able to access simpleAdapter, you need to define it outside of onCreate:

public class MainActivity extends ActionBarActivity {
private ListAdapter simpleAdapter;
public static ArrayList<String> simpleArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    simpleArray =new ArrayList<String>();
    simpleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
            simpleArray);
    ...
}
Community
  • 1
  • 1
Jemshit
  • 9,501
  • 5
  • 69
  • 106
  • Everything's cool except one thing: returnIntent.putExtra("result",result); it shows an error on result – Richard Apr 07 '15 at 19:23
  • @Richard you dont need that line actually – Jemshit Apr 07 '15 at 19:27
  • added everything as you said, still crashes when pressing the button to come back to main activity :/ – Richard Apr 07 '15 at 19:29
  • the biggest problem is that there's no error :D the program just crashes, nothing in log so... – Richard Apr 07 '15 at 19:57
  • Actually missed to copy last part, have it now and there's one error with the simpleAdapter.notifyDataSetChanged(); Error:(61, 30) error: cannot find symbol method notifyDataSetChanged() Error:Execution failed for task ':app:compileDebugJava'. > Compilation failed; see the compiler error output for details. – Richard Apr 07 '15 at 20:04
  • @Richard ListAdapter doesn not have that method, yu need to cast it. Try ((ArrayAdapter) simpleAdapter).notifyDataSetChanged(); or ((ArrayAdapter) lv.getAdapter()).notifyDataSetChanged(); – Jemshit Apr 07 '15 at 20:10
  • Ok, the first suggestion worked, but nothing appears in the ListView – Richard Apr 07 '15 at 20:15
  • @Richard because you didnt add item to array of MainActivity, instead you created array in SecondActivity which has nothing to do with list. I updated code above, just declare simpleArray as static in MainActivity and call it in second activity to add item. – Jemshit Apr 07 '15 at 20:20