0

注意:英语不是我的第一语言。对不起,我的文字有任何错误。

大家,早安。

我正在cardview使用来自 a 的数据填充 a firebase realtime database。我抓住了图像和名称的工作代码示例。但我无法从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。有人有什么建议吗?谢谢。

4

1 回答 1

0

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

两个注意事项:
1- 使用match_parent代替wrap_content您的外部LinearLayout宽度和高度。
2- 使用dp而不是sp填充和边距。

于 2018-10-17T14:18:20.257 回答