DMT82
DMT82

Reputation: 881

Send data from edittext to listview

I have created two Activities (MainActivity, ListActivity) and in MainActivity, I have an EditText and a save-button.

In ListActivity, I have a ListView.

I want to save the value/data/string text that is written in EditText and send it to the other Activity in the ListView.

This is how it looks now, but I think I know what I'm missing but don't know how to do it.

MainActivity.

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button save = (Button) findViewById(R.id.btnSave);
        final EditText et = (EditText) findViewById(R.id.editText);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ListActivity.class);
                intent.putExtra("data", et.getText().toString());
                startActivity(intent);
            }
        });
    }
}

And the ListActivity.

public class ListActivity extends Activity {    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        ListView lv = (ListView) findViewById(R.id.listView);

        Intent intent = getIntent();

        String data = intent.getStringExtra("data");
    }
}

Now the text is saved in data-string, but how paste it in ListView?

Upvotes: 2

Views: 3176

Answers (4)

Ryan Sayles
Ryan Sayles

Reputation: 3441

You can add your String from the TextView and then add that to an ArrayAdapter<String> and then set that as the Adapter for the ListView

Pseudo code:

TextView textView = (TextView) getViewById(...);
String s = textView.getText();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(...);
adapter.add(s);
ListView lv = (ListView) getViewById(...)
lv.setAdapter(adapter)

And if I understand your question correctly you want to first populate the ListView and then send that to another activity. If this is the case, don't try to do that, just create an ArrayList of the String(s) and pass that and then create the ListView in the activity that needs it.

EDIT:

To solve your problem you can use a Bundle to send an ArrayList<String> to your other activity then just build an ArrayAdapter similar to my above code to populate the ListView Something like:

 Intent intent = new Intent(getBaseContext(), YourActivity.class);
 intent.putExtra("ArrayListOfStrings", yourArrayList);

Then in your other Activity (YourActivity) you can do:

 Bundle extras = getIntent().getExtras();
 ArrayList<String> myList = extras.getStringArrayList("ArrayListOfStrings");

Upvotes: 0

MrRhum
MrRhum

Reputation: 43

First of all you have to create a List Container, most used is the ArrayList, then you will have to create a ListAdapater which will inflate your data list to your ListView you can find a good article explaining it here : http://windrealm.org/tutorials/android/android-listview.php

Upvotes: 2

Ravil Mussabayev
Ravil Mussabayev

Reputation: 73

You should create ArrayAdapter<String> object and set it to your ListView:

List<String> listItems = new ArrayList<String>();
listItems.add(data);
String[] items = {};
items = listItems.toArray(new String[listItems.size()]);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));

Upvotes: 0

Jonsmoke
Jonsmoke

Reputation: 852

Here is a sample code to add things in a ListView :

listV = (ListView) findViewById(R.id.list);

ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;

while (...) {
    map = new HashMap<String, String>();
    map.put(stuff);
    listItem.add(map);
}

s = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list,
                       new String[] {"title", "desc"}, new int[] {R.id.title, R.id.desc});
listV.setAdapter(s);

Upvotes: 1

Related Questions