I am trying to list some user detail in a Listview
, in each row show 4 String.
The String is got from Firebase
.
I always get some error. Any idea about the reason of the error or any other solution?
This is my code:
MainActivity
public class SearchActivity extends AppCompatActivity {
private ListView mListView;
//public ArrayList<String> mUser = new ArrayList<>();
private String userID;
private String Lat;
private String Lng;
private Button mRefresh;
private String Name;
private String Birthday;
private String Gender;
private String Email;
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
private DatabaseReference DatabaseReference = firebaseDatabase.getReference();
//private DatabaseReference DatabaseReference = firebaseDatabase.getReference().child("Users/");
//private DatabaseReference CoordenateReference = firebaseDatabase.getReference().child("Coordenate/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
mListView = findViewById(R.id.listViewSearch);
FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
mRefresh = findViewById(R.id.buttonRefresh);
mRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listClean();
FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
Lat = dataSnapshot.child("Users").child(userID).child("Coordenates/Rute/Lat").getValue(String.class);
Lng = dataSnapshot.child("Users").child(userID).child("Coordenates/Rute/Lng").getValue(String.class);
DataSnapshot dS = dataSnapshot.child("Coordenate").child(Lat).child(Lng);
for(DataSnapshot Ds : dS.getChildren()) {
Object key = Ds.getKey();
String StringKey = String.valueOf(key);
getname(StringKey);
//mUser.add(getName(StringKey));
//mUser.add(Name);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SearchActivity.this,"Error 404",Toast.LENGTH_SHORT).show();
}
});
}
});
}
private void getname(String ID){
final String IDfinal = ID;
Toast.makeText(SearchActivity.this,ID,Toast.LENGTH_SHORT).show();
DatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Name = dataSnapshot.child("Users").child(IDfinal).child("name").getValue(String.class);
Email = dataSnapshot.child("Users").child(IDfinal).child("email").getValue(String.class);
Gender = dataSnapshot.child("Users").child(IDfinal).child("gender").getValue(String.class);
Birthday = dataSnapshot.child("Users").child(IDfinal).child("data_birth").getValue(String.class);
list();
Toast.makeText(SearchActivity.this,Name,Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SearchActivity.this,"Error 404",Toast.LENGTH_SHORT).show();
}
});
}
private void list(){
//final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mUser);
//mListView.setAdapter(new customAdapter(this,new String[]{Name}));
//mUser.add(Name);
//arrayAdapter.notifyDataSetChanged();
setContentView(R.layout.activity_search);
List<UserInformation> userInformationsList = new ArrayList<>();
UserInformation userInformation = new UserInformation(Name,Email,Gender,Birthday);
userInformationsList.add(userInformation);
customAdapter customAdapterIntent= new customAdapter((ValueEventListener) this,R.layout.userslist,userInformationsList);
mListView.setAdapter(customAdapterIntent);
}
}
This is userslist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textNameList"
android:textSize="25dp"
android:textAlignment="textStart"
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/textEmailList"
android:textSize="15dp"
android:textAlignment="textStart"
android:text="Age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/textBirthdayList"
android:textSize="15dp"
android:textAlignment="textStart"
android:text="Birthday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/textGenderList"
android:textSize="15dp"
android:textAlignment="textStart"
android:text="gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:layout_margin="10dp"/>
This is Userinformation.java
public class UserInformation {
private static String Name;
private static String Gender;
private static String Email;
private static String Birthday;
public UserInformation(String Name,String Gender,String Email,String Birthday){
this.Birthday=Birthday;
this.Email=Email;
this.Gender=Gender;
this.Name=Name;
}
public static String getName(){return Name;}
public static String getEmail(){return Email;}
public static String getGender(){return Gender;}
public static String getBirthday(){return Birthday;}
}
This is the adapter
public class customAdapter extends ArrayAdapter<UserInformation> {
public customAdapter(ValueEventListener context, int layoutResource,
List<UserInformation> userInformationsList) {
super((Context) context, layoutResource, userInformationsList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(R.layout.userslist, null);
}
UserInformation userInformation = getItem(position);
if (userInformation != null) {
TextView Name = (TextView) view.findViewById(R.id.textNameList);
//name.setText(name[position]);
TextView email = (TextView) view.findViewById(R.id.textEmailList);
//email.setText(Email[position]);
TextView gender = (TextView) view.findViewById(R.id.textGenderList);
//gender.setText(Gender[position]);
TextView birthday = (TextView) view.findViewById(R.id.textBirthdayList);
//birthday.setText(Birthday[position]);
if (Name != null) {
Name.setText(UserInformation.getName());
}
if (email != null) {
email.setText(UserInformation.getEmail());
}
if (gender != null) {
gender.setText(UserInformation.getGender());
}
if (birthday != null) {
birthday.setText(UserInformation.getBirthday());
}
}
return view;
}
}
This is the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.enrik.afrodita, PID: 2629
java.lang.ClassCastException:
com.example.enrik.afrodita.SearchActivity cannot be cast to
com.google.firebase.database.ValueEventListener
at
com.example.enrik.afrodita.SearchActivity.list(SearchActivity.java:130)
at
com.example.enrik.afodita.SearchActivity.access$1100(SearchActivity.java:24)
at
com.example.enrik.adita.Searchivity$2.onDataChange(SearchActivity.java:106)
at com.google.android.gms.internal.zzdtn.zza(Unknown Source)
at com.google.android.gms.internal.zzdvi.zzbur(Unknown Source)
at com.google.android.gms.internal.zzdvo.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
You need to pass Context
not ValueEventListener
so change adapter as
public customAdapter(/*ValueEventListener*/ Context context, int layoutResource,
List<UserInformation> userInformationsList) {
super(context, layoutResource, userInformationsList);
}
and remove the cast as well
customAdapter customAdapterIntent= new customAdapter(/*(ValueEventListener)*/ this,R.layout.userslist,userInformationsList);
where context
is required by the layoutinflater
or adapter to access the resources when you call getContext()
and also remove setContentView(R.layout.activity_search);
from list
method, you are doing it twice , so no need
Thank so much, I did as you said and It works but it only list the last user's information
You need to fetch all the users and add them in list
because currently you are storing information of last user in as string values and using those values to create list of single user object.
// outside onCreate
List<UserInformation> users = new ArrayList<>();
mRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listClean();
FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
Lat = dataSnapshot.child("Users").child(userID).child("Coordenates/Rute/Lat").getValue(String.class);
Lng = dataSnapshot.child("Users").child(userID).child("Coordenates/Rute/Lng").getValue(String.class);
DataSnapshot dS = dataSnapshot.child("Coordenate").child(Lat).child(Lng);
for(DataSnapshot Ds : dS.getChildren()) {
Object key = Ds.getKey();
String StringKey = String.valueOf(key);
// fetch the user Object based on key and add it to list
users.add(lidataSnapshot.child("Users").child(StringKey).getValue(UserInformation.class));
}
// now display list with all user information
customAdapter customAdapterIntent= new customAdapter(SearchActivity.this,
R.layout.userslist,
users);
mListView.setAdapter(customAdapterIntent);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SearchActivity.this,"Error 404",Toast.LENGTH_SHORT).show();
}
});
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.