2017-03-10 18 views
1

私は、コーディングとAndroidの両方が比較的新しいです。今私はいくつかのリストを持っているアプリを作っています。私はListViewを使ってこれをTextDrawableと組み合わせていました。しかし、RecyclerViewの利点を読んだ後、私は、ListViewをRecyclerViewに置き換えることに決めました。私の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> 

ご迷惑をおかけして申し訳ございません。前もって感謝します。

+0

私はあなたが 'RecyclerView'に' LayoutManager'を設定することをどこにも表示されません。この 'LinearLayoutManager ll = new LinearLayoutManager(getContext());')を試してください。 'listview.setLayoutManager(ll)' – jlively

+0

@jlivelyこれを私のフラグメントに追加しようとしましたが、アプリケーションがlistView.setLayoutManager(ll)で 'java.lang.NullPointerException'でクラッシュしました。ライン。 – Kemo

+0

使用したコードを投稿できますか? – jlively

答えて

1

私のエラーは私のアダプターにありました。私はRestaurantsViewHolderにTextViewsとImageViewsを2回宣言しました。

正しい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); 
    } 
} 
関連する問題