Guest User

Untitled

a guest
Aug 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. public class GetFirebaseActivity extends Activity implements AdapterView.OnItemClickListener {
  2.  
  3. Activity activity;
  4. ListView listView;
  5. ListAdapter adapter;
  6. private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
  7. Button btnBack;
  8. DatabaseReference rootRef,demoRef;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_getdatafirebase);
  13. activity = this;
  14. btnBack = (Button) findViewById(R.id.btnBack);
  15. listView = findViewById(R.id.listview);
  16. listView.setOnItemClickListener(this);
  17. listView.setOverScrollMode(View.OVER_SCROLL_NEVER);
  18.  
  19. rootRef = FirebaseDatabase.getInstance().getReference();
  20. demoRef = rootRef.child("demo");
  21. btnBack.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24.  
  25. onBackPressed();
  26. }
  27. });
  28.  
  29. getDataValue();
  30.  
  31. }
  32.  
  33. @Override
  34. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  35.  
  36. }
  37.  
  38. public void getDataValue(){
  39. list = new ArrayList<HashMap<String, String>>();
  40. rootRef.child("demo")
  41. .addListenerForSingleValueEvent(new ValueEventListener() {
  42. @Override
  43. public void onDataChange(DataSnapshot dataSnapshot) {
  44. int i = 1;
  45. for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
  46. String id = (String) snapshot.getKey();
  47. String textTitle = (String) snapshot.child("textTitle").getValue();
  48. String textInput = (String) snapshot.child("textInput").getValue();
  49. String date = (String) snapshot.child("date").getValue();
  50.  
  51. HashMap<String, String> data = new HashMap<>();
  52. data.put("key", id);
  53. data.put("nomor", ""+i++);
  54. data.put("textTitle", textTitle);
  55. data.put("textInput", textInput);
  56. data.put("date", date);
  57. list.add(data);
  58. }
  59.  
  60. adapter = new ListAdapter(activity, list,
  61. R.layout.list_item, new String[]{"nomor","textTitle","textInput", "date"},
  62. new int[]{R.id.nomor, R.id.textTitle,R.id.textInput, R.id.date});
  63. Parcelable state = listView.onSaveInstanceState();
  64. listView.setAdapter(adapter);
  65. listView.onRestoreInstanceState(state);
  66. adapter.notifyDataSetChanged();
  67. }
  68. @Override
  69. public void onCancelled(DatabaseError databaseError) {
  70. }
  71. });
  72. }
  73.  
  74. public class ListAdapter extends SimpleAdapter {
  75. private Context mContext;
  76. public LayoutInflater inflater = null;
  77.  
  78. public ListAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
  79. super(context, data, resource, from, to);
  80. mContext = context;
  81. inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  82. }
  83.  
  84. @Override
  85. public View getView(int position, View convertView, ViewGroup parent) {
  86. View vi = convertView;
  87. if (convertView == null)
  88. vi = inflater.inflate(R.layout.list_item, null);
  89.  
  90. HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
  91. final Button btnDelete = vi.findViewById(R.id.delete);
  92. final TextView nomor = vi.findViewById(R.id.nomor);
  93. final TextView textTitle = vi.findViewById(R.id.textTitle);
  94. final TextView textInput = vi.findViewById(R.id.textInput);
  95. final TextView date = vi.findViewById(R.id.date);
  96.  
  97. final String strKey = (String) data.get("key");
  98. final String strID = (String) data.get("nomor");
  99. final String strTextTitle = (String) data.get("textTitle");
  100. final String strTextInput = (String) data.get("textInput");
  101. final String strDate = (String) data.get("date");
  102.  
  103. nomor.setText(strID);
  104. textTitle.setText(strTextTitle);
  105. textInput.setText(strTextInput);
  106. date.setText(strDate);
  107.  
  108. btnDelete.setOnClickListener(new View.OnClickListener() {
  109. @Override
  110. public void onClick(View view) {
  111. Delete(strKey);
  112. }
  113. });
  114.  
  115. return vi;
  116. }
  117.  
  118. }
  119.  
  120. public void Delete(String keyID){
  121. Query applesQuery = demoRef.child(keyID);
  122.  
  123. applesQuery.addListenerForSingleValueEvent(new ValueEventListener() {
  124. @Override
  125. public void onDataChange(DataSnapshot dataSnapshot) {
  126. for (DataSnapshot dummySnapshot: dataSnapshot.getChildren()) {
  127. dummySnapshot.getRef().removeValue();
  128. }
  129. getDataValue();
  130. }
  131.  
  132. @Override
  133. public void onCancelled(DatabaseError databaseError) {
  134. Log.e("delete", "onCancelled", databaseError.toException());
  135. }
  136. });
  137. }
  138.  
  139.  
  140. @Override
  141. public void onBackPressed() {
  142. super.onBackPressed();
  143. }
  144. }
Add Comment
Please, Sign In to add comment