尝试通过按Enter键将EditText的内容添加到ListView

时间:2014-07-19 19:27:58

标签: java android

我正在尝试通过按Enter键将EditText的内容添加到ListView,但是当我按下Enter键时,EditText字段变得更大,并且没有任何内容添加到列表中,也没有显示任何内容。

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>

2 个答案:

答案 0 :(得分:3)

试试这个 -

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

-

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;
}

});

答案 1 :(得分:0)

aa.notifyDataSetChanged();有时不刷新列表视图。

在此声明之后

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

打印toDoItems中的项目。如果值被插入arraylist然后问题是aa.notifyDataSetChanged(); 。如果这是问题,您必须检查刷新列表视图的其他方法