[英]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.