如何使用edittext.getText()。toString()

时间:2011-02-13 18:15:56

标签: android sqlite listview android-edittext adapter

我想这样做,以便ListView上面有Button。用户点击该按钮,然后会打开dialog,其中包含EditText和“确定”和“取消”按钮。当用户点击“确定”后,EditText中输入的任何文字都会被放入SQLiteDatabase,这会反映在ListView中。

我已经设置了SQLiteDatabase并为列表设置了adapter以显示SQLiteDatabase,但我需要弄清楚如何使用edittext.getText().toString() 1}}添加到SQLiteDatabase的方法。 我需要一个代码示例

如果你需要它,这里是我的主要.java:

package com.gantt.shoppinglist;

import android.app.Dialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ShoppingList extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final DataHelper dataHelper = new DataHelper(this);
        ListView lv = (ListView) findViewById(android.R.id.list);

        SimpleCursorAdapter adapter = null;
        final SQLiteDatabase db = dataHelper.selectAll();
        Cursor c = db.rawQuery("SELECT DISTINCT oid as _id,name FROM table1 ORDER BY name", null);
        if (c.moveToFirst()) {
            String[] columnNames = new String[]{"name"};
            int[] list = new int[]{android.R.id.list};
            adapter = new SimpleCursorAdapter(this, R.layout.rowlayout, c, columnNames, list);
        }
        lv.setAdapter(adapter);

        Button button1main = (Button) findViewById(R.id.add);
        button1main.setOnClickListener(new OnClickListener()  {
            @Override
            public void onClick(View v)  {
            final Dialog additem = new Dialog(ShoppingList.this);
            additem.setContentView(R.layout.maindialog);
            final EditText et = (EditText)additem.findViewById(R.id.edittext);
            additem.setTitle("Type your item");
            additem.setCancelable(true);
            et.setHint("Type the name of an item...");

            Button button = (Button) additem.findViewById(R.id.cancel);
            button.setOnClickListener(new OnClickListener()  {
                @Override
                public void onClick(View v)  {
                    additem.dismiss();
                }
            });
            additem.show();

            Button ok = (Button) additem.findViewById(R.id.ok);
            ok.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    et.getText().toString();
                    additem.dismiss();
                    et.setText("");
                }
            });
       }
        });
    }
}

这是我的DataHelper类:

package com.gantt.shoppinglist;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataHelper {

       public static final String DATABASE_NAME = "items.db";
       public static final int DATABASE_VERSION = 1;
       public static final String TABLE_NAME = "table1";
       public static final String KEY_ROWID = "_id";

       private Context context;
       private SQLiteDatabase db;

       private SQLiteStatement insertStmt;
       private static final String INSERT = "insert into " 
          + TABLE_NAME + "(name) values (?)";

       public DataHelper(Context context) {
          this.context = context;
          OpenHelper openHelper = new OpenHelper(this.context);
          this.db = openHelper.getWritableDatabase();
          this.insertStmt = this.db.compileStatement(INSERT);
       }

       public long insert(String name) {
          this.insertStmt.bindString(1, name);
          return this.insertStmt.executeInsert();
       }

       public void deleteAll() {
          this.db.delete(TABLE_NAME, null, null);
       }

       public SQLiteDatabase selectAll() {
          List<String> list = new ArrayList<String>();
          Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, 
            null, null, null, null, "name desc");
          if (cursor.moveToFirst()) {
             do {
                list.add(cursor.getString(0)); 
             } while (cursor.moveToNext());
          }
          if (cursor != null && !cursor.isClosed()) {
             cursor.close();
          }
          return db;
       }

       public static String getDatabaseName() {
        return DATABASE_NAME;
    }

    private static class OpenHelper extends SQLiteOpenHelper {

          OpenHelper(Context context) {
             super(context, getDatabaseName(), null, DATABASE_VERSION);
          }

          @Override
          public void onCreate(SQLiteDatabase db) {
              db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT");

          }

          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
             Log.w("Example", "Upgrading database, this will drop tables and recreate.");
             db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
             onCreate(db);
          }
       }
    }

2 个答案:

答案 0 :(得分:2)

使用您拥有的当前方法,并假设您了解用于数据库适配器的代码,只需执行以下操作:

String item = et.getText().toString();// these two lines are the
dataHelper.insert(item);              // only change you have to do
additem.dismiss();
et.setText("");

完成此操作后,您必须调用SimpleCursorAdapter对象的notifyDataSetChanged方法。

答案 1 :(得分:0)

DataHelper类的简单示例方法:

public long createUser(String email, String password, String fullName) {
    ContentValues initialValues = new ContentValues();
    initialValues.put("email", email);
    initialValues.put("password", password);
    initialValues.put("fullName", fullName);
    return mDb.insert(TABLE_NAME, null, initialValues);
}