Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding user input from Edit Text into List View

I'm trying to get a user input from Edit Text into List View, I had seen the answer to this similar question but I'm unable to figure it out

I tried this, received no errors from the IDE, but it does not work

public class ListtestActivity extends Activity {
/** Called when the activity is first created. */

Button bt;
EditText et;
TextView tv;
ListView lv;
ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bt = (Button) findViewById(R.id.button1);
    et = (EditText) findViewById(R.id.editText1);
    tv = (TextView) findViewById(R.id.textView1);
    lv = (ListView) findViewById(R.id.listView1);
    String input = et.getText().toString();
    String[] values = new String[] {"", input};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);
    lv.setAdapter(adapter);

Tried the following also

public class ListtestActivity extends Activity {
ArrayAdapter<String> m_adapter;
ArrayList<String> m_listItems = new ArrayList<String>();
/** Called when the activity is first created. */

Button bt;
EditText et;
TextView tv;
ListView lv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bt = (Button) findViewById(R.id.button1);
    et = (EditText) findViewById(R.id.editText1);
    tv = (TextView) findViewById(R.id.textView1);
    lv = (ListView) findViewById(R.id.listView1);
    m_adapter = new ArrayAdapter<String>(this, R.layout.main, m_listItems);
    lv.setAdapter(m_adapter);
    final String input = et.getText().toString();

    bt.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            m_listItems.add(new String(input));
            m_adapter.notifyDataSetChanged();
        }
    });

Any help would be greatly appreciated

Thank You

*Very new to Android/Java/SO

like image 431
David Lamborghini Tan Avatar asked Jun 05 '12 14:06

David Lamborghini Tan


People also ask

How do I edit text in ListView?

Configure the edittextcreate an adapter for the listview and set the position as a tag for the edittext. Normally, when scrolling the item position will change. So, you have to get the edittext tag and set it into the edittext id. from that you can avoid the change of the item position.

How to add data dynamically in ListView in android?

This example demonstrates how do I dynamically add elements in ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

in Second code snippet, change row of m_adapter

  m_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, m_listItems);

Then add in String in m_listItems

bt.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        String input = et.getText().toString();
        if(null!=input&&input.length()>0){     

        m_listItems.add(input);

        m_adapter.notifyDataSetChanged();

      }
    }
});
like image 115
Samir Mangroliya Avatar answered Oct 20 '22 09:10

Samir Mangroliya