我对编码和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();
}
}
我的片段:
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>
感谢任何帮助。提前谢谢。
答案 0 :(得分:1)
我的错误出现在我的适配器中。我在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);
}
}