Skip to content

Instantly share code, notes, and snippets.

@godjooyoung
Created June 11, 2020 02:38
Show Gist options
  • Save godjooyoung/7ee82ff5f4ac63b060b7830cefd4acb1 to your computer and use it in GitHub Desktop.
Save godjooyoung/7ee82ff5f4ac63b060b7830cefd4acb1 to your computer and use it in GitHub Desktop.
DB에서 값가져와서 리스트에 부리기
package com.example.diary;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.버튼 찾아불러오기
Button btn1 = findViewById(R.id.btnWirteForm);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//2.Intent를 이용해서 화면을 전환하자.
Intent intent = new Intent(getApplicationContext(), WriteActivity.class);
//3.StratActi호출후 인텐트를 넣어서주면 화면전환이 된다. intent에 값을 실어 보내고 싶다면...
//3.1.putExtr를 사용해서 담으면 된다.
intent.putExtra("title", "첫번째 제목");
intent.putExtra("content","첫번째 내용");
startActivityForResult(intent, 100);
//4.받는쪽에서 잘 받는지 확인해보자. writeActivity
}
});
//7.리스트뷰를 불러온다.
ListView listDiary = findViewById(R.id.listDiary);
//배열 어댑터
final String[] cafe = getResources().getStringArray(R.array.cafe);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, cafe);
listDiary.setAdapter(arrayAdapter);
//커서 어댑터
DBHelper dbhalper = new DBHelper(this); //DB준비
SQLiteDatabase db = dbhalper.getReadableDatabase(); //DB오픈
Cursor cursor = db.rawQuery("select rowid _id, _no, title, content from diary order by _no desc;", null);
CursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
cursor, new String[]{"TITLE", "CONTENT"},
new int[]{android.R.id.text1, android.R.id.text2},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
);
listDiary.setAdapter(cursorAdapter);
listDiary.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView title = view.findViewById(android.R.id.text1);
Toast.makeText(getApplicationContext(), title.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
//end of onCreate
//6.write에서 result가 finish 되면 실행되는 이벤트
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
String msg = data.getStringExtra("msg");
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG).show();
System.out.println(msg);
}
}
}//end of class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment