RecyclerView不更新视图或不调用onCreate视图持有者

时间:2018-12-17 03:08:41

标签: android android-recyclerview recycler-adapter

我有一个recyclerview,它不会更新recyclerview项内的文本。

初始化Recycler视图时,一切都很好。但是当您调用filter方法时。它根据目录为您提供正确的项目。物品的正确位置。 ArrayList中正确的项目数。但即使图像也正在通过毕加索进行更新。但这不会更新RecyclerView上OnBind上的textview。

public class HospitalAdapter extends RecyclerView.Adapter<HospitalAdapter.HospitalViewHolder> {

private ArrayList<Hospital> hospitalList;
private ArrayList<Hospital> originalList;
private LayoutInflater inflater;
private DatabaseReference db = FirebaseDatabase.getInstance().getReference();

private StorageReference storageRef = FirebaseStorage.getInstance().getReference();


//View FIELDS
private TextView HospitalName;
private TextView Phone;
private RatingBar Rating;
private ImageView Image;
private TextView Location;
private TextView Address;
private LinearLayout cardView;
private Context context;
private Activity activity;


public HospitalAdapter(Context context, Activity activity,String category,String search, ArrayList<Hospital> hospitalArrayList) {
    this.context = context;
    this.activity = activity;
    inflater = LayoutInflater.from(context);
    hospitalList = hospitalArrayList;


}



@NonNull
@Override
public HospitalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.hospital_list_item,parent,false);
    HospitalViewHolder holder = new HospitalViewHolder(view);
    Log.e("Holder","On CreateViewHolder getting called");
    return holder;
}

@Override
public void onBindViewHolder(@NonNull  HospitalViewHolder holder, int position) {
    Hospital hospital = hospitalList.get(position);

    HospitalViewHolder tempHolder = holder;

    HospitalName.setText(hospital.getName());
    Phone.setText(hospital.getContactNumber());
    Rating.setRating(hospital.getRating());
    Location.setText(hospital.getLocation());
    Address.setText(hospital.getAddress());
    final DatabaseReference dbrate = FirebaseDatabase.getInstance().getReference();





}

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


public class HospitalViewHolder extends RecyclerView.ViewHolder {
    public HospitalViewHolder(@NonNull View itemView) {
        super(itemView);
        HospitalName = itemView.findViewById(R.id.hospital_list_item_Name);
        Phone = itemView.findViewById(R.id.hospital_list_item_phone);
        Rating = itemView.findViewById(R.id.hospital_list_item_rating);
        Image = itemView.findViewById(R.id.hospital_list_item_imageview);
        Address = itemView.findViewById(R.id.hospital_list_item_address);
        Location = itemView.findViewById(R.id.hospital_list_item_city);
        cardView = itemView.findViewById(R.id.hospital_list_item_cardview);

    }
}

private void sharedElementTransition(View view,Uri uri,Hospital hospital){
    Pair[] pair = new Pair[5];
    pair[0] = new Pair<View,String>(view.findViewById(R.id.hospital_list_item_imageview),"hospital_image");
    pair[1] = new Pair<View,String>(view.findViewById(R.id.hospital_list_item_rating),"hospital_rating");
    pair[2] = new Pair<View,String>(view.findViewById(R.id.hospital_list_item_cardview),"hospital_cardview");
    pair[3] = new Pair<View,String>(view.findViewById(R.id.hospital_list_item_Name),"hospital_name");
    pair[4] = new Pair<View,String>(view.findViewById(R.id.hospital_list_item_phone),"hospita_contact");
    ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(activity,pair);
    Intent i = new Intent(context, HospitalTabbed.class);


    i.putExtra("THE_URI",uri.toString());
    i.putExtra("Key",hospital.getKey());
    i.putExtra("Details",hospital.getDetails());
    i.putExtra("Rating",hospital.getRating());
    i.putExtra("CityName",hospital.getLocation());
    i.putExtra("Contact",hospital.getContactNumber());
    i.putExtra("Address",hospital.getAddress());
    i.putExtra("Name",hospital.getName());
    context.startActivity(i,options.toBundle());
}



public void filterList(ArrayList<Hospital> filteredList){
    hospitalList = filteredList;
    notifyDataSetChanged();
}

}

这是我的RecyclerView。现在,我想知道我的布局文件是否有问题。由于回收商有时对不同的布局采取不同的行动。

<?xml version="1.0" encoding="utf-8"?>

                          

            android:orientation="vertical"
            android:layout_marginBottom="8dp">
            <Spinner
                android:id="@+id/lobby_spinner_category"
                android:layout_width="match_parent"
                android:layout_height="40dp"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:orientation="horizontal">
                <EditText
                    android:id="@+id/lobby_search_edit_text"

                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_weight="0.6"
                    />
                <Button
                    android:id="@+id/lobby_search_btn"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_weight="0.27"
                    android:text="search"/>
                <Button
                    android:id="@+id/lobby_speech"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_weight="0.13"
                    android:drawableLeft="@drawable/ic_mic_black_24dp"/>
            </LinearLayout>

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


    <android.support.v7.widget.RecyclerView
        android:id="@+id/lobby_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="500dp"


     />



</LinearLayout>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/lobby_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    app:fabSize="normal"
    android:src="@drawable/ic_assignment_white_24dp"
    app:layout_anchor="@id/lobby_bottom_appbar"/>

<android.support.design.bottomappbar.BottomAppBar
    android:id="@+id/lobby_bottom_appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/mtrl_bottomappbar_height"
    android:layout_gravity="bottom"
    android:backgroundTint="@color/colorPrimary"
    android:transitionName="lobby_constraint"
    app:fabAlignmentMode="end"
    app:fabAttached="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/lobby_recycler_view"
    app:menu="@menu/lobby_bar_menu">

</android.support.design.bottomappbar.BottomAppBar>

问题摘要。列表项的textview尚未更新。但是去另一个活动时的onclick很好。毕加索正在更新图像,并且该项目的目录信息也正确。只是在过滤Textview时不会更新。

3 个答案:

答案 0 :(得分:0)

全部

private TextView HospitalName;
private TextView Phone;
private TextView Location;
private TextView Address;

未更新?

答案 1 :(得分:0)

有几种概率:
1.您需要将LayoutManager设置为RecyclerView
2.您需要将适配器设置为Recyclerview
3.您需要确保getItemCount不为0

答案 2 :(得分:0)

我忘记了需要在较旧的字段中进行更改,而不是在适配器字段中进行更改。

OnBindViewHolder

    holder.HospitalName.setText(hospital.getName());
    holder.Phone.setText(hospital.getContactNumber());
    holder.Rating.setRating(hospital.getRating());
    holder.Location.setText(hospital.getLocation());
    holder.Address.setText(hospital.getAddress());
相关问题