3

How to take data that was typed in EditText and by clicking "submit" in that window should add it to previous activity listview items? What I need to do is:

  1. Creating EditText and submit button
  2. Creating listview in same Activity
  3. By clicking submit button it should display it in listview.

I saw this similar question here:add items to listview dynamically android

But i couldn't understand the answer.Somebody please explain how to do this.

5
  • what to implement till now post it first? Commented Mar 3, 2014 at 10:58
  • @SimplePlan I did until the button click.Now i need to know how the text entered in the EditText can be added to ListView. Commented Mar 3, 2014 at 11:01
  • then show me your ListView Adapter code. Commented Mar 3, 2014 at 11:01
  • Retrieve edittext value in string and just add this string value to List<String> arraylist. Commented Mar 3, 2014 at 11:03
  • and then called adapter.notifyDataSetChanged(); Commented Mar 3, 2014 at 11:03

3 Answers 3

11

You just do the following : Prepare your xml like this :

<?xml version="1.0" encoding="utf-8"?>
<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" >

  <EditText
     android:id="@+id/editText"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_toLeftOf="@+id/addItem"
     android:hint="Add a new item to List View" />

  <Button
     android:id="@+id/addItem"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentRight="true"
     android:text="Add" /> 

  <ListView
     android:id="@+id/listView"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_below="@+id/editText" >
  </ListView>

</RelativeLayout>

Activity looks like following :

public class MainActivity extends Activity {
    EditText editText;
    Button addButton;
    ListView listView;
    ArrayList<String> listItems;
    ArrayAdapter<String> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.editText);
        addButton = (Button) findViewById(R.id.addItem);
        listView = (ListView) findViewById(R.id.listView);
        listItems = new ArrayList<String>();
        listItems.add("First Item - added on Activity Create");
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listItems);
        listView.setAdapter(adapter);
        addButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                listItems.add(editText.getText().toString());
                adapter.notifyDataSetChanged();
            }
        });
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position,
                    long id) {
                Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
                        .show();
            }
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Create String Arraylist and initialize it

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

Add some values on it

     str1.add("First Row");
     str1.add("Second Row");
     str1.add("Third Row");
     str1.add("Fourth Row");
     str1.add("Fifth Row");

Then set Adapter to ListView

adapter=new ListAdapter(this,str1);
list.setAdapter(adapter);

then add your EditText text into str1 and then called adapter.notifyDataSetChanged(); like

str1.add(edit_message.getText().toString());
adapter.notifyDataSetChanged();

Try to add this code into Button onclick()

Demo Output:

enter image description here

1 Comment

i see ur above image..... but do u know how to open new activity when u click on "new item"... if u know then pls tell me
1

Suppose you have an arraylist

ArrayList<String> dataList = new Arraylist ();

So On clicking of button, you need to add the new data item into your data arraylist.

For this first take the value entered in edittext and store in a string.

String editedValue = yourEditText.getText.toString();

Then we need to add this in our datalist.

Like

dataList.add(editedValue);

And then just call adapter.notifyDataSetChanged()

yourAdapter.notifyDataSetChanged();

It will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.