Skip to content

Instantly share code, notes, and snippets.

@bratawisnu
Created August 18, 2018 14:42
Show Gist options
  • Save bratawisnu/db7c9e76d4ebea657a2dab5c0d5fb897 to your computer and use it in GitHub Desktop.
Save bratawisnu/db7c9e76d4ebea657a2dab5c0d5fb897 to your computer and use it in GitHub Desktop.
Berikut merupakan code untuk menampilkan data dari firebase ke android
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bb013d60"
android:padding="16dp"
android:gravity="center">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result Data Firebase"
android:padding="10dp"
android:textSize="30sp"
android:textColor="#fff"
android:textStyle="bold|italic"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/btnBack"
android:layout_alignParentBottom="true"
android:background="#dc304a9f"
android:textColor="#fff"
android:layout_marginRight="70dp"
android:layout_marginLeft="70dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Kembali"/>
<ListView
android:layout_above="@id/btnBack"
android:id="@+id/listview"
android:layout_below="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
public class GetFirebaseActivity extends Activity implements AdapterView.OnItemClickListener {
Activity activity;
ListView listView;
ListAdapter adapter;
private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
Button btnBack;
DatabaseReference rootRef,demoRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_data_firebase);
activity = this;
btnBack = (Button) findViewById(R.id.btnBack);
listView = findViewById(R.id.listview);
listView.setOnItemClickListener(this);
listView.setOverScrollMode(View.OVER_SCROLL_NEVER);
rootRef = FirebaseDatabase.getInstance().getReference();
demoRef = rootRef.child("demo");
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
getDataValue();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
public void getDataValue(){
list = new ArrayList<HashMap<String, String>>();
rootRef.child("demo")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int i = 1;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String id = (String) snapshot.getKey();
String textTitle = (String) snapshot.child("textTitle").getValue();
String textInput = (String) snapshot.child("textInput").getValue();
String date = (String) snapshot.child("date").getValue();
HashMap<String, String> data = new HashMap<>();
data.put("key", id);
data.put("nomor", ""+i++);
data.put("textTitle", textTitle);
data.put("textInput", textInput);
data.put("date", date);
list.add(data);
}
adapter = new ListAdapter(activity, list,
R.layout.list_item, new String[]{"nomor","textTitle","textInput", "date"},
new int[]{R.id.nomor, R.id.textTitle,R.id.textInput, R.id.date});
Parcelable state = listView.onSaveInstanceState();
listView.setAdapter(adapter);
listView.onRestoreInstanceState(state);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public class ListAdapter extends SimpleAdapter {
private Context mContext;
public LayoutInflater inflater = null;
public ListAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
mContext = context;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.list_item, null);
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
final Button btnDelete = vi.findViewById(R.id.delete);
final TextView nomor = vi.findViewById(R.id.nomor);
final TextView textTitle = vi.findViewById(R.id.textTitle);
final TextView textInput = vi.findViewById(R.id.textInput);
final TextView date = vi.findViewById(R.id.date);
final String strKey = (String) data.get("key");
final String strID = (String) data.get("nomor");
final String strTextTitle = (String) data.get("textTitle");
final String strTextInput = (String) data.get("textInput");
final String strDate = (String) data.get("date");
nomor.setText(strID);
textTitle.setText(strTextTitle);
textInput.setText(strTextInput);
date.setText(strDate);
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Delete(strKey);
}
});
return vi;
}
}
public void Delete(String keyID){
Query applesQuery = demoRef.child(keyID);
applesQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dummySnapshot: dataSnapshot.getChildren()) {
dummySnapshot.getRef().removeValue();
}
getDataValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("delete", "onCancelled", databaseError.toException());
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:paddingBottom="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#acc5e0ef"
android:orientation="vertical"
android:padding="7dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/nomor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingTop="5dp"
android:paddingRight="0dp"
android:paddingBottom="5dp"
android:textStyle="bold"
android:textColor="#250000"
android:text="1." />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#b67474"
android:paddingLeft="0dp"
android:paddingTop="5dp"
android:paddingRight="0dp"
android:paddingBottom="0dp"
android:text="hapus"
android:textColor="#fcfafa"
android:textSize="18sp"
android:textStyle="normal" />
<RelativeLayout
android:layout_toRightOf="@id/nomor"
android:layout_toLeftOf="@id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="15dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:text="Text"
android:textColor="#250000"
android:textStyle="bold" />
<TextView
android:id="@+id/textInput"
android:layout_below="@id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="15dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:text="Text"
android:textColor="#250000" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textInput"
android:layout_alignParentLeft="true"
android:paddingLeft="15dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:text="Date"
android:textColor="#250000" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment