2

How can i get user inputs from one activity and populate the listView with user data in another activity. I am able to get user input and populate the listView in the same activity. but now i want to get user inputs in one form and populate the list in another activity.

the code that i used to populate the listView by getting user input is as follows

public class MainActivity extends ListActivity {
ArrayList<String> list = new ArrayList<String>();

/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn = (Button) findViewById(R.id.btnAdd);

    /** Defining the ArrayAdapter to set items to ListView */
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

    /** Defining a click event listener for the button "Add" */
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText edit = (EditText) findViewById(R.id.txtItem);
            String name=edit.getText().toString();
            list.add(name);
            edit.setText("");
            adapter.notifyDataSetChanged();
        }
    };

    /** Setting the event listener for the add button */
    btn.setOnClickListener(listener);
Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
  • You can pass your data via intent. reference link : http://stackoverflow.com/questions/19286970/using-intents-to-pass-data-between-activities-in-android – KishuDroid Sep 28 '15 at 07:34
  • If you'd want to pass a large amount of data, @Sarah's answer is good. However if your data is only a single string you could consider passing it by `Intent`. – frogatto Sep 28 '15 at 07:45

4 Answers4

2
  1. you can store your user input / data into a local database; that will allow you to access your data anywhere in the app (recommended since you are dealing with listview).

  2. you can use shared preferences to store data if your data is relatively small.

frogatto
  • 28,539
  • 11
  • 83
  • 129
Sarah Maher
  • 790
  • 1
  • 10
  • 21
0

In your current Activity (activity contains your button), create a new Intent:

String name = ""; 
name = edit.getText().toString();
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("keyword",name);
startActivity(i);

Then in the NewActivity (activity contains your Listview), retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String name = extras.getString("keyword");
    if(name != ""){
        // adapter.notifyDataSetChanged();
    }
}

It is simple way, hope this help

Linh
  • 57,942
  • 23
  • 262
  • 279
0

Declare a public method in second Activity like

SecondActivity.class

public static ArrayList<String> list = new ArrayList<String>();

/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter

onCreate()
{
...
;
 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
...
}

public static void ModifyList()
{
   adapter.notifyDataSetChanged();
}

FirstActivity.class

 View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText edit = (EditText) findViewById(R.id.txtItem);
            String name=edit.getText().toString();
            SecondActivity.list.add(name);
            edit.setText("");
            SecondActivity.ModifyList();
        }
    };
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

Send your ArrayList like this from FirstActivity :

    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    intent.putStringArrayListExtra("Datalist",list);
    startActivity(intent);

In secondActivity Recieve the list using :

  Intent i = getIntent();  
  list = i.getStringArrayListExtra("Datalist");

Then display it in your SecondActivitys listview

Ashish Shukla
  • 1,027
  • 16
  • 36