5

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

3 Answers 3

6

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();

      }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

You're getting the value of the EditText when you create your Activity, and it's empty. Put the line where you declare "input" into your onClick listener.

bt.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        final String input = et.getText().toString();

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

2 Comments

I understand, meaning I tried getting the value of EditText too early, and I should only do it when I need it ? Thanks for the help :)
Exactly, when the value of the EditText changes, it won't update the value of your String, so you need to manually go get it whenever you need it.
0

Try to add the String to the adapter instead to the listview:

m_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, m_listItems);//you can delete your previous initialisation

bt.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        String strAddMe = et.getText().toString();
        if(strAddMe != null)
            m_listItems.add(strAddMe);

        m_adapter.notifyDataSetChanged();

      }
    }
});

As the listview knows about the adapter, the adapter has to know about the content wich it "adapts" to the listview. Once you know what that means you can use adapters without any problems ;)

Furthermore you should read the editText as late as you need its input.

1 Comment

I get it, I should get the value of EditText when I need it instead of getting it too early. Thanks for the help :)

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.