0

Note: English is not my first language. Sorry for any mistake on my text.

Good morning everyone.

I'm populating a cardview with data from a firebase realtime database. I caught a working code example of image and name. But I am unable to populate the rating from firebase data. Below are my classes:

cards XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="40sp"
    android:paddingRight="40sp"
    android:paddingTop="30sp"
    android:paddingBottom="200sp"
    android:outlineProvider="bounds"
    android:clipToPadding="false">

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        app:cardCornerRadius="4dp"
        android:elevation="2dp"
        android:id="@+id/cardView">


        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="300dp"
            android:layout_height="402dp"
            android:layout_gravity="center"
            android:orientation="vertical">


            <LinearLayout
                android:id="@+id/layout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                >

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="250dp"
                    android:layout_height="300dp"
                    android:layout_gravity="center" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/layout3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/name"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center|left"
                    android:paddingLeft="20sp"
                    android:textColor="@android:color/black"
                    android:textSize="30sp"
                    tools:text="Nome do Técnico" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layout4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center">

                <RatingBar
                    android:id="@+id/ratingBar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center|bottom"
                    android:numStars="5"
                    android:stepSize="0.1"
                    android:layout_below="@+id/layout3"/>
            </LinearLayout>
        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

Card Model

public class cards {


    private String userId;
    private String name;
    private String profileImageUrl;
    private String ratingBar;

    public cards (String userId, String name, String profileImageUrl, String ratingBar){

        this.usuarioId = userId;
        this.name = name;
        this.profileImageUrl = profileImageUrl;
        this.ratingBar = ratingBar;

    }

    public String getUserId(){
        return userId;
    }

    public void setUserId(String userId){
        this.userId = userId;
    }

    public String getName(){
        return name;
    }

    public void setName(String name){

        this.name = name;
    }

    public String getProfileImageUrl() {
        return profileImageUrl;
    }

    public void setProfileImageUrl(String profileImageUrl){
        this.profileImageUrl = profileImageUrl;
    }

    public String getRatingBar() {
        return ratingBar;
    }

    public void setRatingBar(String ratingBar) {
        this.ratingBar = ratingBar;
    }
}

Adapter

public class arrayAdapter extends ArrayAdapter<cards> {

    Context context;

    public arrayAdapter (Context context, int resourceId, List<cards> itens){
        super(context, resourceId, itens);
    }

    public View getView (int position, View convertView, ViewGroup parent){
        cards card_item = getItem(position);

        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);

        }

        TextView name = (TextView) convertView.findViewById(R.id.name);
        ImageView image = (ImageView) convertView.findViewById(R.id.image);
        RatingBar ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar);

        name.setText(card_item.getName());
        card_item.getRatingBar();
        ratingBar.setRating(Float.parseFloat(card_item.getRatingBar()));

        switch (card_item.getProfileImageUrl()){
            case "default":
                Glide.with(convertView.getContext()).load(R.drawable.icone_imagem_perfil_padrao).into(image);
                break;

            default:
                Glide.clear(image);
                Glide.with(convertView.getContext()).load(card_item.getProfileImageUrl()).into(image);
                break;

        }

        return convertView;

    }
}

The method of cardview:

    public class CardActivity extends AppCompatActivity {

        private cards cards_data [];
        private br.edu.iftm.getservicer.Cards.arrayAdapter arrayAdapter;

    //[...]


        private String userSearched;

        private void getUserProfession() {

            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();
            userSearched= bundle.getString("userSearched");


            usersDb.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
               if (dataSnapshot.child("profession").getValue()!=null){
                            if (dataSnapshot.exists() && !dataSnapshot.child("conexoes").child("nope").hasChild(usuarioAtualId) && !dataSnapshot.child("conexoes").child("yeps").hasChild(usuarioAtualId) &&
                                    dataSnapshot.child("profession").getValue().toString().equals(userSearched) && !dataSnapshot.getKey().equals(currentUserId)){

                               String profileImageUrl = "default";

                                if (dataSnapshot.child("imagePerfilUrl").getValue().equals("default")){
                                    profileImageUrl = dataSnapshot.child("imagePerfilUrl").getValue().toString();
                                }

                                String rate = "";

                                for (DataSnapshot child : dataSnapshot.child("rating").getChildren()){
                                    rate = dataSnapshot.child("rating").getValue().toString();

                                }


                                cards item = new cards(dataSnapshot.getKey(), dataSnapshot.child("name").getValue().toString(), profileImageUrl, rate);
                                rowItens.add(item);
                                arrayAdapter.notifyDataSetChanged();

                            }
               }


                }

                @Override
                public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                }

                @Override
                public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }

//[...]

}

I believe the problem is here (on Adapter class):

ratingBar.setRating(Float.parseFloat(card_item.getRatingBar()));

I don't know how to set the ratingBar. Anyone have any suggestions? Thanks.

Alucard
  • 3
  • 1
  • 3
  • Do you see the ratingbar empty? Or is it invisible? – forpas Oct 17 '18 at 14:37
  • Empty. The data from firebase is not populating the ratingbar. – Alucard Oct 17 '18 at 14:43
  • 1)remove this `card_item.getRatingBar();` from the Adapter class, it does nothing, 2)since this line `ratingBar.setRating(Float.parseFloat(card_item.getRatingBar()));` does not throw an error this means that the values are valid floats. So put a breakpoint in this line and watch for the exact values that are assigned to the rating bar. – forpas Oct 17 '18 at 14:59
  • Now, I'm receiving this error: E/AndroidRuntime: FATAL EXCEPTION: main Process: br.edu.iftm.getservicer, PID: 29635 java.lang.NumberFormatException: For input string: "{wZjSZ2bL9ONeseFT0tmXKGVriI83=4}" at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306) at java.lang.Float.parseFloat(Float.java:459) at br.edu.iftm.getservicer.Cards.arrayAdapter.getView(arrayAdapter.java:37) – Alucard Oct 17 '18 at 16:29
  • This means that the values are not valid floats. Set a breakpoint and see what these values are. But why the error appeared now? What did you change? – forpas Oct 17 '18 at 16:34
  • " But why the error appeared now? What did you change? " I didn't change anything. The ratingbar is empty when this line "ratingBar.setRating(Float.parseFloat(card_item.getRatingBar()));" is removed. On the breakpoint, the ratingbar is receiving the Id + the value of rating (in this example "4"): ratingBar = {wZjSZ2bL9ONeseFT0tmXKGVriI83=4} – Alucard Oct 17 '18 at 17:04
  • Set a breakpoint to a line like `String s = card_item.getRatingBar();` and check the value of s. – forpas Oct 17 '18 at 17:12
  • I received the same value. – Alucard Oct 17 '18 at 22:16

1 Answers1

0

Change your TextView with id "name" height to wrap_content.

Two notes:
1- Use match_parent instead of wrap_content for your outer LinearLayout's width and height.
2- Use dp instead of sp for padding and margin.

Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
  • Thanks for your suggestions. But the text and image show on the cardview. The problem is only the ratingbar. But, I will change the layouts as your recommended. Thanks. – Alucard Oct 17 '18 at 14:32