简体   繁体   中英

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

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

    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);" so i dont think its actually finding the edit text from the row.xml layout.

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM