forked from MLSA-MUET-SZAB-Club-Khairpur-Mir-s/Learn-to-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Listview
67 lines (59 loc) · 2.39 KB
/
Listview
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.example.listview;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
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;
public class MainActivity extends AppCompatActivity {
private EditText edit;
private Button add;
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=findViewById(R.id.edit);
add=findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textedit=edit.getText().toString();
if(TextUtils.isEmpty(textedit)){
Toast.makeText(MainActivity.this, "Please Insert Text ", Toast.LENGTH_SHORT).show();
}
else {
FirebaseDatabase.getInstance().getReference().child("Programming Knowledge").child("language").push().setValue(textedit);
}
}
});
listview=findViewById(R.id.list);
ArrayList<String>list=new ArrayList<>();
ArrayAdapter adapter=new ArrayAdapter<String>(this, R.layout.activity_main,list);
listview.setAdapter(adapter);
DatabaseReference reference=FirebaseDatabase.getInstance().getReference().child("language");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot:snapshot.getChildren()){
list.clear();
list.add(dataSnapshot.getValue().toString());
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}