java - 在 Android 中使用 RecyclerView 不显示列表

标签 java android listview android-recyclerview android-adapter

我对编码和 Android 都比较陌生。现在我正在制作一个有几个列表的应用程序。为此,我使用了 ListView,并将它与 TextDrawable 相结合。但是在阅读了 RecyclerView 的好处后,我决定用 RecyclerView 替换我的 ListView。我的 listView 没有任何问题,所以我对其进行了修改,使其与 recyclerView 兼容。

我遇到的问题是列表现在没有显示,不是单个项目,但我没有显示任何错误。

我的适配器:

public class RestaurantsAdapter extends RecyclerView.Adapter<RestaurantsAdapter.RestaurantsViewHolder> {

    private List<Restaurant> RestaurantItems;

    public RestaurantsAdapter(List<Restaurant> RestaurantList) {
        this.RestaurantItems = RestaurantList;
    }

    public class RestaurantsViewHolder extends RecyclerView.ViewHolder {
    public TextView RestaurantName, date, time;
    public ImageLoader imageLoader;

    public RestaurantsViewHolder(View itemView) {
        super(itemView);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        ImageView thumbNail = (ImageView) itemView.findViewById(R.id.thumbnail);
        TextView RestaurantName = (TextView) itemView.findViewById(R.id.RestaurantName);
        TextView date = (TextView) itemView.findViewById(R.id.date);
        TextView time = (TextView) itemView.findViewById(R.id.time);

        ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
//       generate random color
        int color1 = generator.getRandomColor();
//       generate color based on a key (same key returns the same color), useful for list/grid views
        int color2 = generator.getColor("Restaurant@gmail.com");
        char firstCharacter = RestaurantName.getText().toString().charAt(0);
        TextDrawable textDrawable = TextDrawable.builder().beginConfig().withBorder(1).endConfig().buildRoundRect(String.valueOf(firstCharacter), color1, 10);
//        TextDrawable textDrawable = TextDrawable.builder().buildRect(String.valueOf(firstCharacter), color1);
        thumbNail.setBackground(textDrawable);
    }
}

    @Override
    public RestaurantsAdapter.RestaurantsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row_Restaurant, parent, false);

        return new RestaurantsViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RestaurantsAdapter.RestaurantsViewHolder holder, int position) {
        // getting Restaurant data for the row
        Restaurant Restaurant = RestaurantItems.get(position);
        holder.RestaurantName.setText(Restaurant.getRestaurantName());
        holder.date.setText(Restaurant.getDate());
        holder.time.setText(Restaurant.getTime());
//        holder.imageLoader.setText(Restaurant.getThumbnailUrl());
    }

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

}

我的 fragment :

public class RestaurantsFragment extends Fragment {

    private static final String TAG = RestaurantsFragment.class.getSimpleName();

    // Restaurants json url
    private ProgressDialog pDialog;
    private List<Restaurant> RestaurantList = new ArrayList<>();
    private RecyclerView listView;
    private RestaurantsAdapter adapter;
    private SQLiteHandler db;

    @Override
    public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_Restaurants, container, false);

        listView = (RecyclerView) view.findViewById(R.id.Restaurants_list);
        adapter = new RestaurantsAdapter(RestaurantList);
        listView.setAdapter(adapter);

        db = new SQLiteHandler(getActivity().getApplicationContext());
        HashMap<String, String> Restaurant = db.getRestaurantDetails();
        final String RestaurantId = Restaurant.get("uid");

        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

        Call<RestaurantsResponse> call = apiService.getOtherRestaurants(RestaurantId);
        call.enqueue(new Callback<RestaurantsResponse>() {
            @Override
            public void onResponse(Call<RestaurantsResponse> call, retrofit2.Response<RestaurantsResponse>response) {
                List<Restaurant> Restaurants = response.body().getResults();

                RestaurantList.addAll(Restaurants);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<RestaurantsResponse>call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
            }
        });

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu, inflater);
    }
}

我的 restarurant_fragment.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"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".sliderfragments.RestaurantsFragment">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/Restaurants_list"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:divider="@color/list_divider"
       android:scrollbars="vertical"
       android:dividerHeight="1dp"
       android:listSelector="@drawable/list_row_selector" />      
</LinearLayout>

我的 list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector"
android:padding="8dp">

<ImageView
    android:id="@+id/thumbnailRestaurantPhoto"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp"
    android:background="@drawable/default_profile"
    tools:ignore="RtlHardcoded" />

<TextView
    android:id="@+id/RestaurantName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnailRestaurantPhoto"
    android:layout_toRightOf="@+id/thumbnailRestaurantPhoto"
    android:textStyle="bold"
    tools:ignore="RtlHardcoded,SpUsage" />

<TextView
    android:id="@+id/date_opened"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/RestaurantName"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnailRestaurantPhoto"
    tools:ignore="RtlHardcoded,SpUsage" />

<TextView
    android:id="@+id/time_opened"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/date"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@+id/thumbnailRestaurantPhoto"
    android:textColor="@color/time"
    tools:ignore="RtlHardcoded,SpUsage" />

</RelativeLayout>

感谢任何帮助。提前致谢。

最佳答案

我的错误出在我的适配器中。我在我的 RestaurantsViewHolder 中声明了两次 TextViews 和 ImageViews。

正确的 RestaurantViewHolder:

public class RestaurantsViewHolder extends RecyclerView.ViewHolder {
public TextView RestaurantName, date, time;
public ImageLoader imageLoader;

public RestaurantsViewHolder(View itemView) {
    super(itemView);

    thumbNail = (ImageView) itemView.findViewById(R.id.thumbnail);
    RestaurantName = (TextView) itemView.findViewById(R.id.RestaurantName);
    date = (TextView) itemView.findViewById(R.id.date);
    time = (TextView) itemView.findViewById(R.id.time);
    }
}

关于java - 在 Android 中使用 RecyclerView 不显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42728989/

相关文章:

java - 如何在 Gradle 中发布以编程方式操作(例如检测)的 jar?

java - 如何写出更优雅的代码? (阶乘、BigDecimals、除以 BigIntegers)

android - 在 Android 的任何布局中使用 colors.xml

android - Android 3.2 的安全异常下载管理器

android - 如何在 android 中使用 ViewFlipper 显示来自 sqlite 的内容

android - 在 Android 中迭代 ListView 项目

wpf - 如何从 WPF/XAML ListView 读取子项值?

java - 比较 SQL 中的元素(房间预订 WebApp)Java

java - 如何用java ASM生成class文件?

android - 在 ListView 中显示 EditText 时如何隐藏多个光标?