"Lägg until(Add)" を押すと、3 つの編集テキスト ボックスにテキストを入力し、[OK] を押すと、データベースに保存され、最初の編集ボックス テキストがタイトとしてリストビューに表示されます。リストビューのアイテムを押すと、入力したテキストを表示できるようになり、「RADERA(削除)」を押すと、データベースとリストビューから削除できるようになりたいです。
私のデータベースアダプタ
package com.projekt;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
static final String KEY_ROWID = "id";
static final String KEY_PASS = "pass";
static final String KEY_USERNAME = "user";
static final String KEY_TITLE = "title";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "DBPASS";
static final String DATABASE_TABLE = "information";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE = "create table information (id integer primary key autoincrement, "
+ "user text not null, pass text not null, title text not null);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
// ---opens the database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public long insertInfo(String title, String user, String pass) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_USERNAME, user);
initialValues.put(KEY_PASS, pass);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteInfo(long rowId) {
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public void delete_byID(int id){
db.delete(DATABASE_TABLE, KEY_ROWID+"="+id, null);
}
public Cursor getAllInfo() {
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_USERNAME,
KEY_PASS, KEY_TITLE }, null, null, null, null, null);
}
public Cursor getInfo(String title) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_USERNAME, KEY_PASS, KEY_TITLE }, KEY_PASS + "= '" + title + "'",
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateInfo(long rowId, String title, String user, String pass) {
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_USERNAME, user);
args.put(KEY_PASS, pass);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
my program
package com.projekt;
import java.util.ArrayList;
//import java.util.Arrays;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
//import android.text.Editable;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
public class layout2 extends Activity implements OnClickListener, OnItemClickListener {
Button btn3;
ListView lv;
private ArrayAdapter<String> listAdapter;
String titleInfo;
ArrayList<String> nameList;
EditText anvNamn;
EditText pass;
EditText title;
DBAdapter db = new DBAdapter(this);
String passInfo = "";
String anv = "";
String titleInfo2 = "";
String visa;
int del;
Cursor c;
Button btn5;
public void onCreate(Bundle savedInstanceState) {
//Start
super.onCreate(savedInstanceState);
setContentView(R.layout.program_layout);
btn3 = (Button) findViewById(R.id.button3);
btn3.setOnClickListener(this);
btn5 = (Button) findViewById(R.id.button5);
btn5.setOnClickListener(this);
lv = (ListView) findViewById(R.id.listView1);
nameList = new ArrayList<String>();
listAdapter = new ArrayAdapter<String>(this, R.layout.row, nameList);
lv.setAdapter(listAdapter);
lv.setOnItemClickListener(this);
getAllData();
//Stop
}
private ArrayList<String> getAllData() {
// ---get all contacts---
db.open();
c = db.getAllInfo();
if (c.moveToFirst()) {
do {
anv = c.getString(1);
passInfo = c.getString(2);
titleInfo2 = c.getString(3);
//in = new String (anv, passInfo, titleInfo2);
//nameList.add(in);
listAdapter.add(titleInfo2);
//updateList();
} while (c.moveToNext());
//listAdapter.add(titleInfo2);
//del = c.getInt(c.getColumnIndex(db.KEY_ROWID));
}
db.close();
//listAdapter.add(titleInfo2);
return nameList;
}
public void inputDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LinearLayout lila1 = new LinearLayout(this);
lila1.setOrientation(1);
anvNamn = new EditText(this);
pass = new EditText(this);
title = new EditText(this);
lila1.addView(title);
lila1.addView(anvNamn);
lila1.addView(pass);
title.setText("Titel");
anvNamn.setText("Användarnamn");
pass.setText("Lösenord");
alert.setView(lila1);
title.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
title.setText("");
}
});
anvNamn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
anvNamn.setText("");
}
});
pass.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
pass.setText("");
}
});
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String titleDB = title.getText().toString();
String userDB = anvNamn.getText().toString();
String passDB = pass.getText().toString();
listAdapter.add(titleDB);
db.open();
db.insertInfo(titleDB, userDB, passDB);
db.close();
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button3:
inputDialog();
break;
case R.id.button5:
Intent nextActivity = new Intent(this, MainActivity.class);
startActivity(nextActivity);
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
// TODO Auto-generated method stub
String text = nameList.get(arg2);
//final int ar = arg2;
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(titleInfo2);
alert.setMessage("Användarnamn: " + anv + "\nLösenord: "+ passInfo);
final String radera = listAdapter.getItem(arg2).toString();
final long raderaPos = listAdapter.getItemId(arg2);
alert.setNegativeButton("RADERA",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//String s = (String) arg0.getItemAtPosition(arg2);
db.open();
//db.updateInfo(arg2, anv, passInfo, titleInfo);
db.deleteInfo(arg2);
db.updateInfo(arg2, anv, passInfo, titleInfo);
db.close();
listAdapter.remove(radera);
}
});
alert.show();
}
}
im from sweden my string names may be confusing.