ArrayList<HashMap<String, String>> personList;
ListView list;
private static final String TAG_NAME = "name";
private static final String TAG_PHONE ="phone";
SQLiteDatabase sampleDB = null;
ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
editText = (EditText)findViewById(R.id.editText);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
listView.setAdapter(arrayAdapter);
listView.setTextFilterEnabled(true);
editText.addTextChangedListener(this);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
try {
sampleDB = this.openOrCreateDatabase(dbName, MODE_PRIVATE, null);
//테이블이 존재하지 않으면 새로 생성합니다.
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + tableName
+ " (name VARCHAR(20), phone VARCHAR(20) );");
//테이블이 존재하는 경우 기존 데이터를 지우기 위해서 사용합니다.
sampleDB.execSQL("DELETE FROM " + tableName );
//새로운 데이터를 테이블에 집어넣습니다..
for (int i=0; i<name.length; i++ ) {
sampleDB.execSQL("INSERT INTO " + tableName
+ " (name, phone) Values ('" + name[i] + "', '" + phone[i]+"');");
}
sampleDB.close();
} catch (SQLiteException se) {
Toast.makeText(getApplicationContext(), se.getMessage(), Toast.LENGTH_LONG).show();
Log.e("", se.getMessage());
}
showList();
}
protected void showList(){
try {
SQLiteDatabase ReadDB = this.openOrCreateDatabase(dbName, MODE_PRIVATE, null);
//SELECT문을 사용하여 테이블에 있는 데이터를 가져옵니다..
Cursor c = ReadDB.rawQuery("SELECT * FROM " + tableName, null);
if (c != null) {
if (c.moveToFirst()) {
do {
//테이블에서 두개의 컬럼값을 가져와서
String Name = c.getString(c.getColumnIndex("name"));
String Phone = c.getString(c.getColumnIndex("phone"));
//HashMap에 넣
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_NAME,Name);
persons.put(TAG_PHONE,Phone);
//ArrayList에 추가합니다..
personList.add(persons);
} while (c.moveToNext());
}
}
ReadDB.close();
//새로운 apapter를 생성하여 데이터를 넣은 후..
adapter = new SimpleAdapter(
this, personList, R.layout.list_item,
new String[]{TAG_NAME,TAG_PHONE},
new int[]{ R.id.name, R.id.phone}
);
//화면에 보여주기 위해 Listview에 연결합니다.
list.setAdapter(adapter);
} catch (SQLiteException se) {
Toast.makeText(getApplicationContext(), se.getMessage(), Toast.LENGTH_LONG).show();
Log.e("", se.getMessage());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
listView.setFilterText(editText.getText().toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
listView.setFilterText(editText.getText().toString());
}
@Override
public void afterTextChanged(Editable s) {
if(editText.getText().length() == 0) {
listView.clearTextFilter();
}
}
검색햇을때 일부분 만 검색 해도 나올 수 있도록 하고 싶어요
그리고 검색했을때 검색내용이 토스트창 같이 뜨는데 안뜨게 하는 법좀