简体   繁体   English

我怎样才能使列表视图中的edittext数据进入数据库?

[英]How can i make data from an edittext in a listview go into the database?

I have a list view with edit text fields like so 我有一个带有编辑文本字段的列表视图,如下所示

http://i.stack.imgur.com/j9mxK.png http://i.stack.imgur.com/j9mxK.png

and basically what i want to do is for the bit in the edit text to be put into the database. 基本上我想做的是将编辑文本中的位放入数据库中。 here is my code so far 到目前为止,这是我的代码

Cursor data = database.query("names", fields, null, null, null, null, null); 游标数据= database.query(“ names”,字段,null,null,null,null,null);

    dataSource = new SimpleCursorAdapter(this, R.layout.row, data, fields,
            new int[] { R.id.first, R.id.entry2 });

    ListView view = getListView();

    setListAdapter(dataSource);

    editText1 = (EditText) findViewById(R.id.entry2);
    editText1.requestFocus();
    OnKeyListener myKeyListener = new OnKeyListener() {

        @Override
        public boolean onKey(View arg0, int KEYCODE_ENTER, KeyEvent arg2) {
            Log.v("Me","Something happened :: ");
            ContentValues values = new ContentValues();
            values.put("last", editText1.getText().toString());
            database.insert("names", null, values);
            return false;
        }
    };
    editText1.setOnKeyListener(myKeyListener);
}

And i get a null pointer exception on the last line "editText1.setOnKeyListener(myKeyListener);" 我在最后一行“ editText1.setOnKeyListener(myKeyListener);”上得到一个空指针异常。 so i dont think its actually finding the edit text from the row.xml layout. 所以我不认为它实际上是从row.xml布局中找到编辑文本的。

so how do i make it so i can type stuff in in the edit text in the list view and then make it put it into the database when enter is hit? 所以我该怎么做,这样我就可以在列表视图的编辑文本中键入内容,然后在按下Enter键时将其放入数据库中?

You have to process each text item in the list view. 您必须处理列表视图中的每个文本项。 All you're doing with this code is putting the first field into the database. 您使用此代码所做的全部工作就是将第一个字段放入数据库中。 To do the rest, you have to "scroll" through the rest of the children of the ListView, get their values, and put them into the database. 为此,您必须“滚动” ListView的其余子级,获取它们的值,然后将它们放入数据库中。

I'm not sure why you would want to do this, though. 不过,我不确定您为什么要这样做。 You're displaying data from somewhere, then you want to press one key and have it all appear in the database? 您要从某个地方显示数据,然后想按一个键并将其全部显示在数据库中? Why not move it directly from its source into the database? 为什么不将其直接从其源移动到数据库中? You can still display it. 您仍然可以显示它。 Or maybe I'm missing something. 也许我想念一些东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我还如何使enter将edittext中的输入文本添加到列表视图? 在创建的添加按钮之上 - How can I also make enter add input text in edittext to my listview? On top of the created add button 如何从 Firebase 数据库中获取密码并与 edittext 匹配? - How can I get password from Firebase database and match with edittext? 如何使用 SQL 数据库在 Fragment 上创建 Adapter ListView? - How can I make an Adapter ListView on a Fragment using an SQL database? 将数据从edittext发送到listview - Send data from edittext to listview 如何将我在EditText中输入的数据插入到数据库中并显示在recyclerview上而不像ajax那样刷新它? - how do I make the data I input in the EditText inserted into the database and appear on the recyclerview without refreshing it like ajax? Android如何使此EditText渐变 - Android How can i make a gradient of this EditText 如何在ContextMenu中制作EditText - How can I make an EditText in a ContextMenu 如何在 android studio 的 EditText Bold 或 Italic 中制作任何选定的文本并将其存储到 SQLite 数据库中? - How can I make any selected text in an EditText Bold or Italic in android studio and store it into a SQLite database? 当要搜索的Edittext为空时,如何显示Listview元素? - How can I display Listview elements when Edittext for searching is empty? 如何使用我的自定义 ListView 访问 EditText - How can I access an EditText withing my custom ListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM