Adding user input from Edit Text to List

I am trying to get user input from Edit Text to a List List, I saw the answer to this similar question, but I cannot figure it out

I tried this, did not receive 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); 

I also tried

 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.

thanks

* Very new for Android / Java / SO

+4
source share
3 answers

in the second code snippet, change the line m_adapter

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

Then add String to 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(); } } }); 
+5
source

Try adding String to the adapter in the list:

 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 adapter list knows, the adapter must know about the content that it “adapts” to the list. As soon as you know what this means, you can use adapters without problems;)

In addition, you should read editText as late as possible.

0
source

When you create your activity, you get the value of EditText, and it is empty. Put the line where you declare the "entry" in 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(); } }); 
0
source

Source: https://habr.com/ru/post/1416162/


All Articles