在Android应用中,Recycler View显示用户缩略图和用户名时存在问题。

huangapple 未分类评论52阅读模式
英文:

Issues With Android App Recycler View Displaying User Thumb and User Name

问题

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.MyApp.Objects.ParticipantsObject;
import com.MyApp.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

public class ParticipantsActivity extends AppCompatActivity {

    // ... (other variables)

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

        // ... (other initialization)

        mSearchBtn.setOnClickListener(view -> {
            String searchText = mSearchField.getText().toString();
            firebaseUserSearch(searchText);
        });
    }

    private void firebaseUserSearch(String searchText) {
        Toast.makeText(ParticipantsActivity.this, "Started Search", Toast.LENGTH_LONG).show();
        Query firebaseSearchQuery = mUserDatabase.orderByChild("name").startAt(searchText);

        FirebaseRecyclerOptions<ParticipantsObject> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<ParticipantsObject>()
                .setQuery(firebaseSearchQuery, ParticipantsObject.class)
                .build();

        FirebaseRecyclerAdapter<ParticipantsObject, UserHolder> firebaseRecyclerAdapter;

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ParticipantsObject, UserHolder>(firebaseRecyclerOptions) {
            @Override
            protected void onBindViewHolder(@NonNull UserHolder userHolder, int position, @NonNull ParticipantsObject participantsObject) {
                userHolder.setUsers(participantsObject);
            }

            @Override
            public UserHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout, parent, false);
                return new UserHolder(view);
            }
        };

        mResultList.setAdapter(firebaseRecyclerAdapter);
        firebaseRecyclerAdapter.startListening();
    }

    // ... (other code)

    class UserHolder extends RecyclerView.ViewHolder {
        private TextView imageThumbTextView, nameTextView;

        UserHolder(View itemView) {
            super(itemView);
            imageThumbTextView = itemView.findViewById(R.id.profile_image);
            nameTextView = itemView.findViewById(R.id.name_text);
        }

        void setUsers(ParticipantsObject participantsObject) {
            String imageThumb = participantsObject.getThumb_image();
            imageThumbTextView.setText(imageThumb);
            String name = participantsObject.getName();
            nameTextView.setText(name);
        }
    }
}
<!-- activity_participants.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context="com.MyApp.ParticipantsActivity">

    <!-- ... (other views) -->

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/result_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/search_field"
        android:layout_marginTop="50dp">
    </androidx.recyclerview.widget.RecyclerView>

</RelativeLayout>
<!-- list_layout.xml -->
<RelativeLayout 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">

    <ImageView
        android:id="@+id/profile_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        app:srcCompat="@mipmap/ic_default_user" />

    <TextView
        android:id="@+id/name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="14dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="213dp"
        android:layout_marginRight="20dp"
        android:layout_toEndOf="@+id/profile_image"
        android:text="Username"
        android:textColor="#555555"
        android:textSize="16sp" />

</RelativeLayout>

Please note that I've removed the comments and the XML contents that were not part of the code translation request.

英文:

I am programming a simple search for users but my app is not displaying the results of the query and crashes about 40% of the time after I click search. Here is my code. Any advice would be appreciated.

My Activity:

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.MyApp.Objects.ParticipantsObject;
import com.MyApp.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;



public class ParticipantsActivity extends AppCompatActivity {

    private EditText mSearchField;
    private ImageButton mSearchBtn;
    private RecyclerView mResultList;
    private DatabaseReference mUserDatabase;


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

        mUserDatabase = FirebaseDatabase.getInstance().getReference().child(&quot;Users&quot;).child(&quot;Participants&quot;);
        mSearchField = (EditText) findViewById(R.id.search_field);
        mSearchBtn = (ImageButton) findViewById(R.id.search_btn);
        mResultList = (RecyclerView) findViewById(R.id.result_list);
        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));

        mSearchBtn.setOnClickListener(view -&gt; {

            String searchText = mSearchField.getText().toString();
            firebaseUserSearch(searchText);

        });

    }

    private void firebaseUserSearch(String searchText) {
        Toast.makeText(ParticipantsActivity.this, &quot;Started Search&quot;, Toast.LENGTH_LONG).show();
        Query firebaseSearchQuery = mUserDatabase.orderByChild(&quot;name&quot;).startAt(searchText);
             

        FirebaseRecyclerOptions&lt;ParticipantsObject&gt; firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder&lt;ParticipantsObject&gt;()
                .setQuery(firebaseSearchQuery, ParticipantsObject.class)
                .build();

        class UserHolder extends RecyclerView.ViewHolder {
            private TextView imageThumbTextView, nameTextView     
            UserHolder(View itemView) {
                super(itemView);
                imageThumbTextView = itemView.findViewById(R.id.profile_image);
                nameTextView = itemView.findViewById(R.id.name_text);
            }


            void setUsers(ParticipantsObject participantsObject) {
                String imageThumb = driverObject.getThumb_image();
                imageThumbTextView.setText(imageThumb);
                String name = participantsObject.getName();
                nameTextView.setText(name);
            }
        }

        FirebaseRecyclerAdapter&lt;ParticipantsObject, UserHolder&gt; firebaseRecyclerAdapter;

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter&lt;ParticipantsObject, UserHolder&gt;(firebaseRecyclerOptions) {
            @Override
            protected void onBindViewHolder(@NonNull UserHolder userHolder, int position, @NonNull ParticipantsObject participantsObject) {
                userHolder.setUsers(participantsObject);
            }

            @Override
            public UserHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout, parent, false);

                return new UserHolder(view);
            }
        };
        mResultList.setAdapter(firebaseRecyclerAdapter);
        firebaseRecyclerAdapter.startListening();

    }


}

My activity_participants.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
        xmlns:tools=&quot;http://schemas.android.com/tools&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:background=&quot;#ffffff&quot;
        tools:context=&quot;com.MyApp.ParticipantsActivity&quot;&gt;


        &lt;TextView
            android:id=&quot;@+id/heading_label&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_alignParentStart=&quot;true&quot;
            android:layout_alignParentTop=&quot;true&quot;
            android:layout_marginLeft=&quot;30dp&quot;
            android:layout_marginTop=&quot;30dp&quot;
            android:text=&quot;Firebase Search&quot;
            android:textColor=&quot;#555555&quot;
            android:textSize=&quot;24sp&quot; /&gt;

        &lt;EditText
            android:id=&quot;@+id/search_field&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_alignStart=&quot;@+id/heading_label&quot;
            android:layout_below=&quot;@+id/heading_label&quot;
            android:layout_marginRight=&quot;20dp&quot;
            android:layout_marginTop=&quot;20dp&quot;
            android:layout_toStartOf=&quot;@+id/search_btn&quot;
            android:background=&quot;@drawable/search_layout&quot;
            android:ems=&quot;10&quot;
            android:hint=&quot;Search here&quot;
            android:inputType=&quot;textPersonName&quot;
            android:paddingBottom=&quot;10dp&quot;
            android:paddingLeft=&quot;20dp&quot;
            android:paddingRight=&quot;20dp&quot;
            android:paddingTop=&quot;10dp&quot;
            android:textColor=&quot;#999999&quot;
            android:textSize=&quot;16sp&quot; /&gt;

        &lt;ImageButton
            android:id=&quot;@+id/search_btn&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_alignBottom=&quot;@+id/search_field&quot;
            android:layout_alignParentEnd=&quot;true&quot;
            android:layout_alignTop=&quot;@+id/search_field&quot;
            android:layout_marginRight=&quot;30dp&quot;
            android:background=&quot;@android:color/background_light&quot;
            app:srcCompat=&quot;@mipmap/search_button&quot; /&gt;


    &lt;androidx.recyclerview.widget.RecyclerView
            android:id=&quot;@+id/result_list&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:layout_below=&quot;@+id/search_field&quot;
            android:layout_marginTop=&quot;50dp&quot;&gt;
    &lt;/androidx.recyclerview.widget.RecyclerView&gt;

    &lt;/RelativeLayout&gt;

My list layout:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;&gt;

    &lt;ImageView
        android:id=&quot;@+id/profile_image&quot;
        android:layout_width=&quot;80dp&quot;
        android:layout_height=&quot;80dp&quot;
        android:layout_alignParentStart=&quot;true&quot;
        android:layout_alignParentTop=&quot;true&quot;
        android:layout_marginLeft=&quot;30dp&quot;
        android:layout_marginTop=&quot;20dp&quot;
        app:srcCompat=&quot;@mipmap/ic_default_user&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/name_text&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_alignParentTop=&quot;true&quot;
        android:layout_alignParentEnd=&quot;true&quot;
        android:layout_marginStart=&quot;14dp&quot;
        android:layout_marginLeft=&quot;20dp&quot;
        android:layout_marginTop=&quot;50dp&quot;

        android:layout_marginEnd=&quot;213dp&quot;
        android:layout_marginRight=&quot;20dp&quot;
        android:layout_toEndOf=&quot;@+id/profile_image&quot;
        android:text=&quot;Username&quot;
        android:textColor=&quot;#555555&quot;
        android:textSize=&quot;16sp&quot; /&gt;

&lt;/RelativeLayout&gt;

Anyone have any ideas as to why it's not displaying results?
My relevant Firebase DB basic paths are like so:
Users -> Participants -> UserIDs -> name, image, etc

Logcat isn't giving me much at the moment.

huangapple
  • 本文由 发表于 2020年3月15日 15:58:37
  • 转载请务必保留本文链接:https://java.coder-hub.com/60690810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定