【问题标题】:Ratingbar on Cardview not ShowingCardview 上的评分栏不显示
【发布时间】:2018-10-17 14:04:41
【问题描述】:

注意:英语不是我的第一语言。很抱歉我的文字有任何错误。

大家早上好。

我正在用来自firebase realtime database 的数据填充cardview。我抓住了图像和名称的工作代码示例。但我无法从firebase 数据中填充评级。以下是我的课程:

卡片 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>

卡片模型

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;
    }
}

适配器

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;

    }
}

卡片查看方法:

    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) {

                }
            });
        }

//[...]

}

我相信问题出在这里(在适配器类上):

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

我不知道如何设置ratingBar。有人有什么建议吗?谢谢。

【问题讨论】:

  • 您看到评级栏是空的吗?还是看不见?
  • 空。来自 firebase 的数据未填充评级栏。
  • 1) 从 Adapter 类中删除这个 card_item.getRatingBar();,它什么都不做,2) 因为这行 ratingBar.setRating(Float.parseFloat(card_item.getRatingBar())); 没有抛出错误,这意味着这些值是有效的浮点数。因此,在这一行中放置一个断点并注意分配给评分栏的确切值。
  • 现在,我收到此错误:E/AndroidRuntime: FATAL EXCEPTION: main Process: br.edu.iftm.getservicer, PID: 29635 java.lang.NumberFormatException: For input string: "{ wZjSZ2bL9ONeseFT0tmXKGVriI83=4}" 在 java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306) 在 java.lang.Float.parseFloat(Float.java:459) 在 br.edu.iftm.getservicer.Cards.arrayAdapter.getView( arrayAdapter.java:37)
  • 这意味着这些值不是有效的浮点数。设置断点并查看这些值是什么。但是为什么现在出现了错误?你改变了什么?

标签: android firebase firebase-realtime-database android-cardview swipecardview


【解决方案1】:

将 ID 为“名称”高度的 TextView 更改为 wrap_content

两个音符:
1- 使用match_parent 而不是wrap_content 作为外部LinearLayout 的宽度和高度。
2- 使用dp 而不是sp 填充和边距。

【讨论】:

  • 感谢您的建议。但文字和图像显示在卡片视图上。问题只是评级栏。但是,我会按照您的建议更改布局。谢谢。
猜你喜欢
  • 2016-09-23
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
相关资源
最近更新 更多