Note that there are some explanatory texts on larger screens.

plurals
  1. POTo do list example doesn't work for me
    primarykey
    data
    text
    <p>The following example doesn't work in the right way for me. As per the code, it should add the entry in the ListView when "ENTER" key is pressed. But it just enters the new line in the EditText field and doesn't really add the entry in the ListView. I don't get any error while running the application. Here's my Activity and XML file.</p> <p>MainActivity.java</p> <pre><code>package com.example.todo_list; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; public class MainActivity extends Activity { /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Inflate your view setContentView(R.layout.activity_main); // Get references to UI widgets ListView myListView = (ListView)findViewById(R.id.myListView); final EditText myEditText = (EditText)findViewById(R.id.myEditText); // Create the array list of to do items final ArrayList&lt;String&gt; todoItems = new ArrayList&lt;String&gt;(); // Create the array adapter to bind the array to the listview final ArrayAdapter&lt;String&gt; aa; aa = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, todoItems); // Bind the array adapter to the listview. myListView.setAdapter(aa); myEditText.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) if (keyCode == KeyEvent.KEYCODE_ENTER) { todoItems.add(0, myEditText.getText().toString()); aa.notifyDataSetChanged(); myEditText.setText(""); return true; } return false; } }); } } </code></pre> <p>activity_main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;EditText android:id="@+id/myEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="New To Do Item" /&gt; &lt;ListView android:id="@+id/myListView" android:layout_width="match_parent" android:layout_height="427dp" &gt; &lt;/ListView&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload