-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
395 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,395 @@ | ||
#03_13 | ||
|
||
#Firebase | ||
|
||
firebase.google.com | ||
|
||
- database - noSQL, node.js의 구조라고 보면 된다. | ||
key - value 의 형태 | ||
비구조적인 계층형태 | ||
|
||
``` | ||
fir-test-66f76addclose | ||
- 1 | ||
- content: "내용" | ||
- ndate: "2017-03-13" | ||
- title: "제목" | ||
- 2 | ||
- content: "내용2" | ||
- ndate: "2017-03-14" | ||
- title: "제목2" | ||
``` | ||
|
||
|
||
- 안드로이드 스튜디오 toos - firebase | ||
|
||
|
||
- realtime databse - save&retrieve - connect to firebase - add the realtime database to your app | ||
|
||
|
||
- 권한설정 -> | ||
firebase 웹페이지 콘솔에 들어가서 | ||
database - rules - learn more.. | ||
|
||
|
||
|
||
>-----여기까지 database 쓰기위한 준비완료 ----- | ||
|
||
## database 입력 | ||
|
||
- HashMap | ||
|
||
```java | ||
package com.hyugnmin.android.firebaseone; | ||
|
||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
|
||
import com.google.firebase.database.DatabaseReference; | ||
import com.google.firebase.database.FirebaseDatabase; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
Button btnSave; | ||
EditText editText1, editText2; | ||
|
||
// Write a message to the database | ||
FirebaseDatabase database; | ||
DatabaseReference bbsRef; | ||
|
||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
database = FirebaseDatabase.getInstance(); | ||
bbsRef = database.getReference("bbs"); | ||
|
||
|
||
btnSave = (Button)findViewById(R.id.btnSave); | ||
editText1 = (EditText)findViewById(R.id.editText); | ||
editText2 = (EditText)findViewById(R.id.editText2); | ||
|
||
btnSave.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
String title = editText1.getText().toString(); | ||
String content = editText2.getText().toString(); | ||
|
||
//bbs 레퍼런스(테이블)에 키를 생성 | ||
String key = bbsRef.push().getKey(); | ||
//입력될 레코드(키,값 세트)를 생성한다 | ||
Map<String, String> postValues = new HashMap<>(); | ||
postValues.put("title", title); | ||
postValues.put("content", content); | ||
|
||
//생성된 레코드를 데이터베이스에 입력 | ||
// | ||
// bbs - 키1 - title : 값 | ||
// content : 값 | ||
// 키2 - title : 값 | ||
// content : 값 | ||
// | ||
|
||
Map<String, Object> keyMap = new HashMap<>(); | ||
keyMap.put(key, postValues); | ||
bbsRef.updateChildren(keyMap); | ||
|
||
} | ||
}); | ||
|
||
} | ||
} | ||
|
||
|
||
``` | ||
- setValue | ||
|
||
```java | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
Button btnSave; | ||
EditText editText1, editText2; | ||
|
||
// Write a message to the database | ||
FirebaseDatabase database; | ||
DatabaseReference bbsRef; | ||
|
||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
database = FirebaseDatabase.getInstance(); | ||
bbsRef = database.getReference("bbs"); | ||
|
||
|
||
btnSave = (Button)findViewById(R.id.btnSave); | ||
editText1 = (EditText)findViewById(R.id.editText); | ||
editText2 = (EditText)findViewById(R.id.editText2); | ||
|
||
btnSave.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
String title = editText1.getText().toString(); | ||
String content = editText2.getText().toString(); | ||
|
||
//bbs 레퍼런스(테이블)에 키를 생성 | ||
String key = bbsRef.push().getKey(); | ||
//입력될 레코드(키,값 세트)를 생성한다 | ||
Map<String, String> postValues = new HashMap<>(); | ||
postValues.put("title", title); | ||
postValues.put("content", content); | ||
|
||
//생성된 레코드를 데이터베이스에 입력 | ||
// | ||
// bbs - 키1 - title : 값 | ||
// content : 값 | ||
// 키2 - title : 값 | ||
// content : 값 | ||
// | ||
DatabaseReference keyRef = bbsRef.child(key); | ||
keyRef.setValue(postValues); | ||
|
||
DatabaseReference titleRef = bbsRef.child(key).child("title"); | ||
titleRef.setValue("제목입니다"); | ||
//트리구조를 child로 계속 타고 가서 ref를 가져와 수정할 수 있다. | ||
|
||
} | ||
}); | ||
|
||
} | ||
} | ||
|
||
``` | ||
## listView에 출력 | ||
|
||
- Main | ||
```java | ||
package com.hyugnmin.android.firebaseone; | ||
|
||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.ListView; | ||
|
||
import com.google.firebase.database.DataSnapshot; | ||
import com.google.firebase.database.DatabaseError; | ||
import com.google.firebase.database.DatabaseReference; | ||
import com.google.firebase.database.FirebaseDatabase; | ||
import com.google.firebase.database.ValueEventListener; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
Button btnSave; | ||
EditText editText1, editText2; | ||
|
||
// Write a message to the database | ||
FirebaseDatabase database; | ||
DatabaseReference bbsRef; | ||
|
||
ListView listView; | ||
ListAdapter listAdapter; | ||
List<Bbs> datas = new ArrayList<>(); | ||
|
||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
database = FirebaseDatabase.getInstance(); | ||
bbsRef = database.getReference("Bbs"); | ||
|
||
listView = (ListView)findViewById(R.id.listView); | ||
listAdapter = new ListAdapter(datas, this); | ||
listView.setAdapter(listAdapter); | ||
|
||
|
||
btnSave = (Button)findViewById(R.id.btnSave); | ||
editText1 = (EditText)findViewById(R.id.editText); | ||
editText2 = (EditText)findViewById(R.id.editText2); | ||
|
||
btnSave.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
String title = editText1.getText().toString(); | ||
String content = editText2.getText().toString(); | ||
|
||
//Bbs 레퍼런스(테이블)에 키를 생성 | ||
String key = bbsRef.push().getKey(); | ||
//입력될 레코드(키,값 세트)를 생성한다 | ||
Map<String, String> postValues = new HashMap<>(); | ||
postValues.put("title", title); | ||
postValues.put("content", content); | ||
|
||
//생성된 레코드를 데이터베이스에 입력 | ||
// | ||
// Bbs - 키1 - title : 값 | ||
// content : 값 | ||
// 키2 - title : 값 | ||
// content : 값 | ||
// | ||
DatabaseReference keyRef = bbsRef.child(key); | ||
keyRef.setValue(postValues); | ||
// | ||
// DatabaseReference titleRef = bbsRef.child(key).child("title"); | ||
// titleRef.setValue("제목입니다"); | ||
// //트리구조를 child로 계속 타고 가서 ref를 가져와 수정할 수 있다. | ||
|
||
} | ||
}); | ||
|
||
bbsRef.addValueEventListener(postListener); | ||
|
||
} | ||
|
||
ValueEventListener postListener = new ValueEventListener() { | ||
@Override | ||
public void onDataChange(DataSnapshot dataSnapshot) { | ||
// Get Post object and use the values to update the UI | ||
// Post post = dataSnapshot.getValue(Post.class); | ||
// ... | ||
Log.w("MainActivity", "data count = " + dataSnapshot.getChildrenCount()); | ||
|
||
datas.clear(); | ||
|
||
for(DataSnapshot snapshot : dataSnapshot.getChildren()) { | ||
String key = snapshot.getKey(); | ||
Bbs bbs = snapshot.getValue(Bbs.class); //컨버팅 | ||
bbs.key = key; | ||
datas.add(bbs); | ||
} | ||
listAdapter.notifyDataSetChanged(); | ||
} | ||
|
||
@Override | ||
public void onCancelled(DatabaseError databaseError) { | ||
// Getting Post failed, log a message | ||
Log.w("MainActivity", "loadpost : OnCancelled", databaseError.toException()); | ||
// ... | ||
} | ||
}; | ||
} | ||
|
||
|
||
``` | ||
|
||
- listAdapter | ||
|
||
```java | ||
package com.hyugnmin.android.firebaseone; | ||
|
||
import android.content.Context; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.BaseAdapter; | ||
import android.widget.TextView; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by besto on 2017-03-13. | ||
*/ | ||
|
||
public class ListAdapter extends BaseAdapter { | ||
|
||
List<Bbs> datas; | ||
Context context; | ||
LayoutInflater inflater; | ||
View view; | ||
|
||
public ListAdapter(List<Bbs> datas, Context context) { | ||
this.datas = datas; | ||
this.context = context; | ||
this.inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE); | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return datas.size(); | ||
} | ||
|
||
@Override | ||
public Object getItem(int position) { | ||
return datas.get(position); | ||
} | ||
|
||
@Override | ||
public long getItemId(int position) { | ||
return position; | ||
} | ||
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup parent) { | ||
if(convertView == null) | ||
convertView = inflater.inflate(R.layout.list_item, null); | ||
|
||
TextView textTitle = (TextView)convertView.findViewById(R.id.textTitle); | ||
TextView textContent= (TextView)convertView.findViewById(R.id.textContent); | ||
|
||
Bbs bbs = datas.get(position); | ||
textTitle.setText(bbs.title); | ||
textContent.setText(bbs.content); | ||
|
||
return convertView; | ||
} | ||
} | ||
|
||
|
||
``` | ||
|
||
- Bbs | ||
|
||
```java | ||
|
||
package com.hyugnmin.android.firebaseone; | ||
|
||
/** | ||
* Created by besto on 2017-03-13. | ||
*/ | ||
|
||
public class Bbs { | ||
public String key; | ||
public String title; | ||
public String content; | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
} | ||
|
||
``` | ||
|
||
|