package
com.example.a.wiho;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.database.Cursor;
import
android.database.sqlite.SQLiteDatabase;
import
android.database.sqlite.SQLiteOpenHelper;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.view.inputmethod.InputMethodManager;
import
android.widget.AdapterView;
import
android.widget.Button;
import
android.widget.CursorAdapter;
import
android.widget.EditText;
import
android.widget.ListView;
import
android.widget.TextView;
import
android.widget.Toast;
import
java.util.ArrayList;
public
class
PlusActivity
extends
Activity {
private
ArrayList<String> aitems;
private
EditText eName;
private
EditText eAge;
MyDBHelper mHelper;
SQLiteDatabase db;
Cursor cursor;
MyCursorAdapter myAdapter;
final
static
String KEY_ID =
"_id"
;
final
static
String KEY_NAME =
"name"
;
final
static
String KEY_AGE =
"age"
;
final
static
String TABLE_NAME =
"mytable"
;
final
static
String querySelectAll = String.format(
"SELECT * FROM %s"
, TABLE_NAME);
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_plus);
ListView list = (ListView) findViewById(R.id.lv_name_age);
mHelper =
new
MyDBHelper(
this
);
db = mHelper.getWritableDatabase();
cursor = db.rawQuery(querySelectAll,
null
);
aitems =
new
ArrayList<String>();
myAdapter =
new
MyCursorAdapter(
this
, cursor, aitems);
list.setAdapter(myAdapter);
list.setOnItemLongClickListener(
new
ListViewItemLongClickListener());
}
private
Button.OnClickListener clickListener =
new
Button.OnClickListener() {
@Override
public
void
onClick(View view) {
eName = (EditText) findViewById(R.id.et_name);
eAge = (EditText) findViewById(R.id.et_age);
String name = eName.getText().toString();
String age = eAge.getText().toString();
if
(eName.getText().toString().equals(
""
))
{
Toast.makeText(PlusActivity.
this
,
"Wifi의 이름을 입력하세요."
, Toast.LENGTH_SHORT).show();
}
else
if
(eAge.getText().toString().equals(
""
))
{
String query = String.format(
"INSERT INTO %s VALUES ( null, '%s', null);"
, TABLE_NAME, name, age);
db.execSQL(query);
cursor = db.rawQuery(querySelectAll,
null
);
myAdapter.changeCursor(cursor);
}
else
{
String query = String.format(
"INSERT INTO %s VALUES ( null, '%s', %s );"
, TABLE_NAME, name, age);
db.execSQL(query);
cursor = db.rawQuery(querySelectAll,
null
);
myAdapter.changeCursor(cursor);
}
eName.setText(
""
);
eAge.setText(
""
);
InputMethodManager imm =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(eAge.getWindowToken(),
0
);
}
};
class
MyCursorAdapter
extends
CursorAdapter {
@SuppressWarnings
(
"deprecation"
)
public
MyCursorAdapter(Context context, Cursor c, ArrayList<String> aitems) {
super
(context, c);
}
@Override
public
void
bindView(View view, Context context, Cursor cursor) {
TextView tvName = (TextView) view.findViewById(R.id.tv_name);
TextView tvAge = (TextView) view.findViewById(R.id.tv_age);
String name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
String age = cursor.getString(cursor.getColumnIndex(KEY_AGE));
Log.d(
"스트링 확인"
, name +
", "
+ age);
tvName.setText(name);
tvAge.setText(age);
}
@Override
public
View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.alist_item, parent,
false
);
return
v;
}
}
class
MyDBHelper
extends
SQLiteOpenHelper {
public
MyDBHelper(Context context) {
super
(context,
"MyData.db"
,
null
,
2
);
}
public
void
onCreate(SQLiteDatabase db) {
String query = String.format(
"CREATE TABLE %s ("
+
"_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+
"%s TEXT, "
+
"%s INTEGER );"
, TABLE_NAME, KEY_NAME, KEY_AGE);
db.execSQL(query);
}
public
void
onUpgrade(SQLiteDatabase db,
int
oldVersion,
int
newVersion) {
String query = String.format(
"DROP TABLE IF EXISTS %s"
, TABLE_NAME);
db.execSQL(query);
onCreate(db);
}
}
int
selectedPos = -
1
;
private
class
ListViewItemLongClickListener
implements
AdapterView.OnItemLongClickListener
{
@Override
public
boolean
onItemLongClick(AdapterView<?> parent,
final
View view,
int
position,
long
id)
{
selectedPos = position;
AlertDialog.Builder alertDlg =
new
AlertDialog.Builder(view.getContext());
alertDlg.setTitle(R.string.alert_title_question);
alertDlg.setNegativeButton( R.string.button_yes,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick( DialogInterface dialog,
int
which )
{
aitems.remove(selectedPos);
myAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
alertDlg.setPositiveButton( R.string.button_no,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick( DialogInterface dialog,
int
which ) {
dialog.dismiss();
}
});
alertDlg.setMessage(String.format(getString(R.string.alert_msg_adelete), aitems.get(position)));
alertDlg.show();
return
false
;
}
}
}