有人可以帮助我了解问题所在吗?它没有在RecyclerView中正确显示数据

问题描述 投票:-1回答:3

运行该应用程序后,一切都很好,只不过数据(文本)没有显示其应有的功能。第一次使用此代码运行应用程序时,它运行良好,但是当我对代码进行修改时,它以某种方式停止了按我想要的方式显示数据,而我不知道为什么会这样。我什至撤消了所有修正案,但仍然行不通。

这是我的主,以防万一]

公共类MainActivity扩展了AppCompatActivity {

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("DefaultWords");

EditText engText, araText;
Button submitBtn, display, deletebtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    writeToData();
    disButton();



}


private void writeToData() {


    engText = findViewById(R.id.english);
    araText = findViewById(R.id.arabic);
    submitBtn = findViewById(R.id.button);

    submitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String engWord = engText.getText().toString().trim();
            String araWord = araText.getText().toString().trim();


            MyWords words = new MyWords(engWord, araWord);


            myRef.child(engWord).setValue(words);


        }
    });

}

}

我的适配器

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {


    private LayoutInflater layoutInflater;

    private List<MyWords> words;


    public Adapter(Context context, ArrayList<MyWords> words) {
        this.layoutInflater = LayoutInflater.from(context);
        this.words = words;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.new_custom_cards, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        MyWords words1 = words.get(position);

        holder.englishText.setText(words1.getEnglish());
        holder.arabicText.setText(words1.getArabic());


    }

    @Override
    public int getItemCount() {
        return words.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView englishText, arabicText;


        ViewHolder(@NonNull View itemView) {
            super(itemView);
            englishText = itemView.findViewById(R.id.cardTextView);
            arabicText = itemView.findViewById(R.id.cardTextView2);
        }
    }

我的RecyclerView

public class RecylePractice extends AppCompatActivity {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("DefaultWords");



    RecyclerView recyclerView;
    Adapter adapter;
    ArrayList<MyWords> wordsArrayList = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_recyle_view);


        recyclerView = findViewById(R.id.recyle);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new Adapter(this, wordsArrayList);
        recyclerView.setAdapter(adapter);
        recylerReadData();


    }


    private void recylerReadData() {


        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    MyWords words = postSnapshot.getValue(MyWords.class);


                    wordsArrayList.add(words);
                    adapter.notifyDataSetChanged();

                }
            }


            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d("TAG", databaseError.getMessage());


            }
        });
    }
}

这是运行显示的结果

this is what happen the display is run

我希望英语单词显示在左侧,而阿拉伯语单词显示在右侧。

我的new_custom_cards xml

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCDFA6A6"
android:padding="10dp"

>

<androidx.cardview.widget.CardView
    android:id="@+id/cardView_custom"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.13">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">

        <TextView
            android:id="@+id/cardTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="@string/english"
            android:textAlignment="viewStart"
            android:textColor="@color/red"

            android:textSize="25sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <TextView
            android:id="@+id/cardTextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="@string/arabic"
            android:textAlignment="viewStart"
            android:textColor="@color/red"
            android:textSize="25sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/eqauls123"
            android:textAlignment="center"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="25sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/cardTextView2"
            app:layout_constraintStart_toEndOf="@+id/cardTextView"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

运行该应用程序后,一切都很好,只不过数据(文本)没有显示其应有的功能。第一次使用此代码运行应用程序时,但当我对...

java android firebase-realtime-database recycler-adapter
3个回答
0
投票

您确实在itemview布局宽度中设置了wrap_content。


0
投票
myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                MyWords words = postSnapshot.getValue(MyWords.class);


                wordsArrayList.add(words);
                adapter.notifyDataSetChanged();

            }
        }

0
投票

您在wrap_content中将layout_width设置为RecyclerView。尝试将其更改为match_parent,如下所示

© www.soinside.com 2019 - 2024. All rights reserved.