public class ListviewMain extends Activity implements OnClickListener,
OnItemClickListener, OnItemLongClickListener {
private final static String TB_NAME = "test";
public static final String DB_NAME = "test.db";
public static final int DB_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String TITLE = "title";
public static final String CONTENTS = "contents";
EditText editText1;
Button Addbtn;
SQLiteDatabase db;
DBManagerHandler handler;
Button Searchbtn;
DBHelper dbhelper;
SimpleCursorAdapter adapter1 = null;
static ArrayList<String> arrayList;
static ArrayAdapter<String> adapter;
ListView listView;
ClipboardManager cm;
Intent intent;
/* basic_info 테이블 생성 쿼리 */
private static final String Q_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
+ TB_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT " + ","
+ "title TEXT " + "," + "contents TEXT" + ");";
private final String Q_GET_LIST = "SELECT * FROM test"
+ " ORDER BY _id ASC";
BroadcastReceiver receiver;
Intent keyboard;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
try {
keyboard = getIntent();
if (keyboard.hasExtra("keyboard")) { // 키보드에 의해서 어플이 실행됬으면 true를 반환
// 아니면false
String temp = keyboard.getStringExtra("keyboard"); //
Log.i("ggggg", temp);
}
} catch (Exception e) {
e.setStackTrace(null);
}
Searchbtn = (Button) findViewById(R.id.Searchbtn);
Addbtn = (Button) findViewById(R.id.addbtn);
arrayList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(ListviewMain.this,
android.R.layout.simple_list_item_2, arrayList);
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
// 기본적인 서비스 만들어보기
Log.i("TAG", "Controller Activity - startService()");
Intent myserviceintent = new Intent(this, ServiceApp.class);
startService(myserviceintent);
// 버튼들을 implement로 사용하기위해
Addbtn.setOnClickListener(this);
Searchbtn.setOnClickListener(this);
listView.setOnItemClickListener(this);
/* 리스트뷰 롱클릭시 삭제 다이얼로그 */
listView.setOnItemLongClickListener(this);
}// 수정 한뒤
protected void onResume() {
super.onResume();
getDbData();
}
public void onBackPressed() {
Log.i("onBackPressedonBackPressedonBackPressed", "onBackPressed ");
// Do some stuff
finish();
super.onBackPressed();
}
private void getDbData() {
SQLiteDatabase db = null;
if (db == null) {
db = openOrCreateDatabase("test.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
}
// checkTableIsCreated(db); //db에 solo_info 테이블이 있는지 확인
db.execSQL(Q_CREATE_TABLE);
Cursor c = db.rawQuery(Q_GET_LIST, null);
// startManagingCursor(c);
listView = (ListView) findViewById(R.id.listView1);
adapter1 = new SimpleCursorAdapter(getApplicationContext(),
android.R.layout.simple_list_item_2, c, new String[] { "title",
"contents" }, new int[] { android.R.id.text1,
android.R.id.text2 });
listView.setAdapter(adapter1);
if (db != null) {
db.close();
}
}
public static void copytext(String copyContents) {
Log.i("copytext", "copyContents " + copyContents);
arrayList.add("_" + copyContents); // 이렇게 보여주기위한문.
adapter.notifyDataSetChanged();// 어뎁터에게 초기화 됨을 알리는 문
}
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
String tile = "null";
String contents = "null";
Intent intent = Data;
// if (keyboard.hasExtra("keyboard")) { // 키보드에 의해서 어플이 실행됬으면 true를 반환
// // 아니면false
// String temp = keyboard.getStringExtra("keyboard"); //
// Log.i("ggggg", temp);
// }
try {
tile = intent.getStringExtra("TitleEditStr").toString(); // 제목의 글을 가져오는 문
contents = intent.getStringExtra("ContentEditStr").toString(); // 내용의 글을 가져오는 문
} catch (Exception e) {
Log.i("여기는 에러", "에러");
}
switch (requestCode) {
case 0:
arrayList.add(tile + "_" + contents); // 이렇게 보여주기위한문.
adapter.notifyDataSetChanged();// 어뎁터에게 초기화 됨을 알리는 문
break;
case 1:
break;
default:
break;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int getId = v.getId();
switch (getId) {
case R.id.addbtn:
intent = new Intent(ListviewMain.this, EditClass.class);
startActivityForResult(intent, 0); // 에디트클래스의 엑티비티에서 다시
// 돌아옴으로 포리조트함수사용
break;
case R.id.Searchbtn:
Intent intent = new Intent(ListviewMain.this, ServiceApp.class);
stopService(intent);
break;
default:
break;
}
}
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// display the selected country
String tempTitle = cursor.getString(cursor
.getColumnIndexOrThrow(DBHelper.TITLE));
Toast.makeText(getApplicationContext(), tempTitle, Toast.LENGTH_SHORT)
.show();
String Contents = cursor.getString(cursor
.getColumnIndexOrThrow(DBHelper.CONTENTS));
Log.i(tempTitle, Contents);
// starts a new Intent to update/delete a Country
// pass in row Id to create the Content URI for a single row
Intent countryEdit = new Intent(getBaseContext(), SubActivity.class);
Bundle bundle = new Bundle();
bundle.putString("mode", "update");
bundle.putString("tempContents", Contents);
bundle.putString("tempTitle", tempTitle);
countryEdit.putExtras(bundle);
startActivity(countryEdit);
}
글 수정 코드가 있는곳은 맨 마지막부분 onItemClick() 이부분이에요.
목록에서 글을 누르면 수정을 할 수 있는데 수정을 하고 나면, 수정전 글과 수정 후 글 두개가 목록에 떠요ㅠㅠ
수정하면 수정후 글만 목록에 떠야하는데 어떻게 하면 될까요?ㅜㅜ