akano1
akano1

Reputation: 41614

trying to add the content of EditText to ListView by pressing the Enter key

I'm trying to add the content of EditText to a ListView by pressing the Enter key but when I press the Enter Key the EditText field just gets bigger and nothing is added to the list and nothing is shown.

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoListActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_do_list);

        ListView myListView = (ListView) findViewById(R.id.myListView);
        final EditText myEditText = (EditText) findViewById(R.id.myEditText);

        final ArrayList<String> toDoItems = new ArrayList<String>();
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, toDoItems);

        myListView.setAdapter(aa);

        myEditText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN)
                    if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
                            || (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        toDoItems.add(0, myEditText.getText().toString());
                        aa.notifyDataSetChanged();
                        myEditText.setText("");
                        return true;
                    }
                return false;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.to_do_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.action_menu) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/addItemHint" />

    <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

Upvotes: 0

Views: 316

Answers (2)

Ratul Ghosh
Ratul Ghosh

Reputation: 1500

Try this -

    <EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/addItemHint"
    android:maxLines="1" />

in the activity -

myEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int keyCode,
        KeyEvent event) {
     if ( (event.getAction() == KeyEvent.ACTION_DOWN  ) &&
         (keyCode           == KeyEvent.KEYCODE_ENTER)   )
    {    
      toDoItems.add(0, myEditText.getText().toString());
      aa.notifyDataSetChanged();
      myEditText.setText("");



       // hide virtual keyboard
       InputMethodManager imm = 
       (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
       imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
       return true;
    }
    return false;
}

});

Upvotes: 3

sanath_p
sanath_p

Reputation: 2218

aa.notifyDataSetChanged(); doesnot refresh the listview sometimes.

after this statement

toDoItems.add(0, myEditText.getText().toString());

print the items in toDoItems . if the value is getting inserted into the arraylist then the problem is with aa.notifyDataSetChanged(); . If this is the problem you have to check other ways to refresh list view

Upvotes: 0

Related Questions