Skip to content Skip to sidebar Skip to footer

Add User Input Into A Listview On Button Click

I am trying to make a TO DO LIST. I have a EditText, Button, and ListView. On button click I want to add, what I typed into the EditText into a ListView. main_activity.xml &l

Solution 1:

This should do it.

@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    btnAdd = (Button)findViewById(R.id.addTaskBtn);
    btnAdd.setOnClickListener(this);
    et = (EditText)findViewById(R.id.editText);
    adapter = newArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);

    // set the lv variable to your list in the xml
    lv=(ListView)findViewById(R.id.list);  
    lv.setAdapter(adapter);
}
publicvoidonClick(View v)
{
    String input = et.getText().toString();
    if(input.length() > 0)
    {
        // add string to the adapter, not the listview
        adapter.add(input);
        // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
    }
}

Solution 2:

you need to find your listview and then set the adapter:

  lv=(ListView)findViewById(android.R.id.yourlistview);
  lv.setAdapter(adapter);

Post a Comment for "Add User Input Into A Listview On Button Click"