안드로이드 SQLite를 이용하여
1. ListView -> 2. 리스트 각 아이템을 터치하면 상세 내용 액티비티 -> 3. 버튼을 누르면 수정모드(EditText)
이렇게 만들고 있습니다.
intent와 putExtra, getStringExtra를 이용해서 데이터를 넘기고 있고요.
1에서 2로 넘어가는 과정은 잘 됩니다. 그러나 2에서 3으로 넘어갈 때 에러가 나진 않지만
에디트텍스트에는 리스트뷰의 첫번째 데이터값이 계속 나옵니다.
어떤 곳을 수정해야 할 지 잘 모르겠네요.
지적 부탁드립니다.
1. 리스트뷰 액티비티
ListView mlistView = (ListView) findViewById(android.R.id.list);
DBHandler dbhandler = DBHandler.open(this);
final Cursor cursor = dbhandler.selectAll();
startManagingCursor(cursor);
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.list_row, cursor, new String[] { "message_text",
"date" }, new int[] { R.id.text1, R.id.date });
setListAdapter(cursorAdapter);
mlistView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent i = new Intent(Existing_Text.this, Single_List.class);
//내용과 날짜값 데이터를 넘긴다.
i.putExtra("message_text",
cursor.getString(cursor.getColumnIndex("message_text")));
i.putExtra("date",
cursor.getString(cursor.getColumnIndex("date")));
startActivity(i);
}
});
2. 상세 내용 액티비티
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
//넘어온 데이터를 받는다.
Intent intent = getIntent();
content_text = intent.getStringExtra("message_text");
content_date = intent.getStringExtra("date");
TextView text_content = (TextView) findViewById(R.id.tv1);
text_content.setText(String.valueOf(content_text));
TextView date_content = (TextView) findViewById(R.id.date2);
date_content.setText(String.valueOf(content_date));
for (int btnId : BUTTONS) {
Button btnUnderButton = (Button) findViewById(btnId);
btnUnderButton.setOnClickListener(this);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
DBHandler dbhandler = DBHandler.open(this);
switch (v.getId()) {
case R.id.btn_edit: // 수정모드
Intent i = new Intent(Single_List.this, Edit_Text.class);
cursor = dbhandler.selectAll();
i.putExtra("message_text",
cursor.getString(cursor.getColumnIndex("message_text")));
i.putExtra("date", cursor.getString(cursor.getColumnIndex("date")));
startActivity(i);
//cursor.close();
break;
}
3. 수정모드 액티비티
public class Edit_Text extends Activity implements View.OnClickListener {
static final int[] BUTTONS = { R.id.btn_save2, R.id.btn_can2 };
EditText edit;
private static final String TAG = "Edit_Text";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_edit);
edit = (EditText) findViewById(R.id.et1);
Intent intent = getIntent();
String content_text = intent.getStringExtra("message_text");
//String content_date = intent.getStringExtra("date");
edit.setText(String.valueOf(content_text));
for (int btnId : BUTTONS) {
Button btnUnderButton = (Button) findViewById(btnId);
btnUnderButton.setOnClickListener(this);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
파란 배경 부분에서 데이터를 키값을 잘못 넣었나 싶은데
이리 고치고 저리 고쳐도 원하는 답을 못가져오네요.
실수한 부분이 어딘지 알 수 있을까요?
아...답변 감사합니다.
초보라서 소스가 조금 불필요한 부분이 섞였을 수도 있습니다만
1번에서 2번 과정은 데이터 10개 입력해서 하나하나 확인했습니다.
1번에서 2번으로는 1) -> 1), 2) -> 2)....10) -> 10)
이렇게 넘어가긴 합니다.
어렵네요...
1번에서 2번으로 넘어가는 Cursor는 리스트뷰에서 그리기위해 사용합니다.
그릴때 내부적으로 Cursor 포지션을 바꿔서 그리는걸로 알고 있습니다.
그냥 아무 이벤트도 주지 않고 쓰실경우는 마지막에 그린 뷰에 포지션이 가 있어서 그값을 가져와서
제대로 못가져갈거라고 말씀드린거구요
2번에서 Cursor를 가져올때 selectAll()을 하시는데
여기서 Cursor를 가져 오시면 기본적으로 포지션 값은 -1로 설정 되어 있습니다.
moveToNext 나 moveTo First를 하지 않은 상태에선 포지션이 -1이라
값을 제대로 못가져 옵니다.
그리고 굳이 2번에서까지 DB에서 가져 오실 필요는 없을거 같습니다.
1번에서 2번으로 넘기면 현대 _id, 넘길려는 데이터를 변수로 저장해뒀다가
그 변수를 넘기면 될듯 한데요.
처음부터 잘못 하신거 같습니다.
데이터가 한개일경우는 하나밖에 없어서 정상적으로 데이터를 넘기는 것처럼 보일텐데요
많을 경우 그 아이템을 선택했을때 넘기실려고 하는 데이터가 넘어가지 않을거 같습니다.
mlistView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
<<이 부분에서 id로 db로 새로 쿼리 날려서 선택한 row를 검색합니다.
<<이때 검색한 Cursor를 itemCursor로 하면
if(itemCursor.moveToNext() == true){
Intent i = new Intent(Existing_Text.this, Single_List.class);
//내용과 날짜값 데이터를 넘긴다.
i.putExtra("message_text",
itemCursor.getString(itemCursor.getColumnIndex("message_text")));
i.putExtra("date",
itemCursor.getString(itemCursor.getColumnIndex("date")));
startActivity(i);
}
}
});
굳이 아이템이 하나다라고 하시면 기존 소스에서 돌아 갈 수 있도록 하는 방법은
2번 상세에서
cursor = dbhandler.selectAll();
cursor.moveToFirst(); <<<<<<<<<<<<<<<<<<<추가하시면
i.putExtra("message_text",
cursor.getString(cursor.getColumnIndex("message_text")));
i.putExtra("date", cursor.getString(cursor.getColumnIndex("date")));
startActivity(i);
로 하시면 동작은 되실거 같습니다.