RecyclerView onCreateViewHolder 和 onBindViewHolder 没有被调用

RecyclerView onCreateViewHolder and onBindViewHolder not getting called

我正在尝试通过从 SQLite 数据库获取数据来创建动态列表视图。 我创建了断点,发现适配器文件的 onCreateViewHolder 和 onBindViewHolder 没有被调用。无论如何,在尝试在 getItemCount() 中打印计数时,我得到了正确的计数。谁能帮我解决一下?

列表View.java

package com.hacker.wanderlust;

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

import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;

import com.hacker.wanderlust.adapter.TravelListAdapter;
import com.hacker.wanderlust.bean.Travel;
import com.hacker.wanderlust.dao.TravelDAO;
import com.hacker.wanderlust.logic.Conversion;

import java.text.ParseException;
import java.util.ArrayList;

public class TravelViewList extends AppCompatActivity {

    TravelDAO travelDAO = new TravelDAO(this);
    Conversion conversion = new Conversion();

    ArrayList<Travel> travels=new ArrayList<>();

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

        try {

            RecyclerView travelList = (RecyclerView) findViewById(R.id.travelListView);

            travelList.setLayoutManager(new LinearLayoutManager(this));
            travelList.setHasFixedSize(true);

            Cursor cursor = travelDAO.getTravelData();

            Log.d("message","Cursor got data");

            if(cursor!=null && cursor.getCount()>0) {

                if(cursor.moveToFirst()) {

                    do {

                        Travel travel = new Travel();

                        travel.setName(cursor.getString(0));
                        travel.setLocation(cursor.getString(1));
                        travel.setDateOfTravel(conversion.toSQLDate(cursor.getString(2)));

                        Log.d("message",travel.getName());

                        travels.add(travel);

                    } while(cursor.moveToNext());

                }

            }

            travelList.setAdapter(new TravelListAdapter(conversion.travelArrayListToArray(travels)));

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

适配器文件

package com.hacker.wanderlust.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hacker.wanderlust.R;
import com.hacker.wanderlust.bean.Travel;

public class TravelListAdapter extends RecyclerView.Adapter<TravelListAdapter.TravelViewHolder> {

    private Travel[] data;

    public TravelListAdapter(Travel[] data) {
        this.data = data;
    }

    @NonNull
    @Override
    public TravelViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        Log.d("message","onCreate");

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.travel_view, parent, false);
        return new TravelViewHolder(view);
    }

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

        Travel travel=data[position];
        holder.tripName.setText(travel.getName());
        holder.tripDetails.setText(travel.getLocation());

    }

    @Override
    public int getItemCount() {
        Log.d("message","count: "+data.length);
        return data.length;
    }

    public class TravelViewHolder extends RecyclerView.ViewHolder {

        TextView tripName, tripDetails;

        public TravelViewHolder(@NonNull View itemView) {
            super(itemView);
            tripName = itemView.findViewById(R.id.tripName);
            tripDetails = itemView.findViewById(R.id.tripDetails);
        }
    }

}

travel_view.xml

<LinearLayout 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="match_parent"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingTop="10dp">

    <TextView
        android:id="@+id/tripName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="34sp"></TextView>

    <TextView
        android:id="@+id/tripDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="16sp"></TextView>

</LinearLayout>

activity_travel_view_list.xml

<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/travelListView"
    android:visibility="visible">

</androidx.recyclerview.widget.RecyclerView>

Dropbox link 到整个项目代码:https://www.dropbox.com/s/5qs7ixiylrxzv41/Wanderlust.zip?dl=0

我测试了我这边的代码(更改了从数据库获取数据的部分),它工作正常。我的代码:

public class TestRecyclerview02 extends AppCompatActivity {
    ArrayList<Travel> travels=new ArrayList<>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_recycleview02);

        try {
            RecyclerView travelList = (RecyclerView) findViewById(R.id.travelListView);

            travelList.setLayoutManager(new LinearLayoutManager(this));
            travelList.setHasFixedSize(true);

            // debug mock, not using the cursor.
            travels.add(new Travel("AAAA", "123"));
            travels.add(new Travel("BBBB", "1234"));
            travels.add(new Travel("CCCC", "123456"));
            travels.add(new Travel("DDDD", "1234567"));
            travels.add(new Travel("EEEE", "1234567"));
            travels.add(new Travel("FFFF", "12345678"));

            travelList.setAdapter(new TravelListAdapter( travels.toArray(new Travel[0])));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

关于Adapter,我没改,只是在onBindViewHolder

的方法里加了一行log
 Log.d("message","onBindViewHolder");

问候 travel_view.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" // ===> pay attention to this 
    android:orientation="vertical">

    <TextView
        android:id="@+id/tripName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="tripName" />

    <TextView
        android:id="@+id/tripDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="tripDetails" />

</LinearLayout>

结果还可以。

问题是您正试图从主线程访问数据库。你的适配器没问题。

问题是在回收站视图 (activity_travel_view_list.xml) 中,高度和宽度设置为 0dp。我将其更改为 android:layout_width="match_parent" & android:layout_height="match_parent" 并且有效。感谢大家的贡献。