1
我對編碼和Android都比較陌生。現在我正在製作一個有幾個列表的應用程序。我爲此使用了ListView,並將其與TextDrawable結合使用。但在閱讀了RecyclerView的好處之後,我決定用RecyclerView替換ListView。我的listView工作沒有任何問題,所以我修改它,以便它與recyclerView兼容。列表與Android中的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("[email protected]");
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>
任何幫助表示讚賞。提前致謝。
我沒有看到任何地方,你設置了'LayoutManager'到'RecyclerView'。試試這個'LinearLayoutManager ll = new LinearLayoutManager(getContext());'。 'listview.setLayoutManager(ll)' – jlively
@jlively我試着把它添加到我的片段中,但是我的應用程序在listView.setLayoutManager(ll)上使用'java.lang.NullPointerException'崩潰;線。 – Kemo
你可以發佈你使用的代碼嗎? – jlively