Recycler View not showing in fragment - java

I have three fragments: Fragment user, Fragment Home and Fragment Progress.
I'm trying to implement a recylcer view in the homefragment that can recycle images in the recycler view nothing is happening and no error is showing in logcat, i don't know what i'm doing wrong.
1.This is my Custom Adapter:
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder>{
#NonNull
private LayoutInflater inflator;
List<Information> data= Collections.emptyList();
public CustomAdapter(Context context, List<Information> data){
inflator= LayoutInflater.from(context);
}
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view=inflator.inflate(R.layout.customlayout, parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
Information current=data.get(position);
holder.icon.setImageResource(current.images);
holder.title.setText(current.title);
}
#Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
ImageView icon;
TextView title;
public MyViewHolder(View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.listicon);
title = (TextView) itemView.findViewById(R.id.textView);
}
}
}
This is my Fragment
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class HomeFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private CustomAdapter adapter;
int[] IMAGES = {R.drawable.abs,R.drawable.arms,R.drawable.back,R.drawable.chest,R.drawable.full,R.drawable.legs};
private RecyclerView recyclerView;
ArrayList<HashMap<String, String>> data;
private String mParam1;
private String mParam2;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView =(RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new CustomAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<Information> getData(){
List<Information> data = new ArrayList<>();
int [] icons = {R.drawable.abs,R.drawable.arms,R.drawable.back,R.drawable.chest,R.drawable.full,R.drawable.legs};
String[] titles={"Abs","Arms","Back","Chest","full","legs"};
for (int i=0; i<icons.length && i<titles.length;i++){
Information current = new Information();
current.images=icons[i];
current.title=titles[i];
data.add(current);
}
return data;
}
}
I can't see to find what I'm doing wrong.
fragmenthome.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".HomeFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
4.Custom_layout.xml
<RelativeLayout
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">
<ImageView
android:id="#+id/listicon"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:text="TextView" />

you didnt assign the data in your adapter's constructor
this.data = data;
then your item count should be
data.size();
should not be 0.

Related

Fragment holding a RecyclerView not showing in the MainActivity

I'm trying to show a RecyclerView, in a fragment, in my main activity.
I'm not getting any errors, but the fragment remains empty, and I can't find the solution for this problem.
I simply have a fragment that has a recyclerview in it, I configure the recyclerview in HomeFragment.java, then in the MainParkActivity, I have a FrameLayout, which is replaced with the HomeFragment.
MainParkActivity
package com.example.ipark;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;;
public class MainParkActivity extends AppCompatActivity {
private static final String TAG = "MainParkActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started.");
Toolbar toolbar = findViewById(R.id.toolbar);
Fragment homeFragment = new HomeFragment();
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("iParc");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, homeFragment).commit();
}
}
HomeFragment
package com.example.ipark;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
private ArrayList<String> mNames = new ArrayList<>();
private ArrayList<String> mImageUrls = new ArrayList<>();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
giveInformation();
RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), mNames, mImageUrls);
recyclerView.setAdapter(adapter);
}
private void giveInformation() {
mImageUrls.add("https://loremflickr.com/300/300/bmw");
mNames.add("BMW");
mImageUrls.add("https://loremflickr.com/300/300/bmw");
mNames.add("Audi");
mImageUrls.add("https://loremflickr.com/300/300/bmw");
mNames.add("Hyundai");
mImageUrls.add("https://loremflickr.com/300/300/bmw");
mNames.add("Mercedes");
mImageUrls.add("https://loremflickr.com/300/300/bmw");
mNames.add("BMW");
mImageUrls.add("https://loremflickr.com/300/300/bmw");
mNames.add("Audi");
mImageUrls.add("https://loremflickr.com/300/300/bmw");
mNames.add("Hyundai");
mImageUrls.add("https://loremflickr.com/300/300/bmw");
mNames.add("Mercedes");
}
}
RecyclerViewAdapter
package com.example.ipark;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mImageNames;
private ArrayList<String> mImages;
private Context mContext;
public RecyclerViewAdapter(Context mContext, ArrayList<String> mImageNames, ArrayList<String> mImages) {
this.mImageNames = mImageNames;
this.mImages = mImages;
this.mContext = mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
ViewHolder holder;
holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
Glide.with(mContext)
.asBitmap()
.load(mImages.get(position))
.into(holder.image);
holder.imageName.setText(mImageNames.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked on: " + mImageNames.get(position));
Toast.makeText(mContext, mImageNames.get(position), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return mImageNames.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView image;
TextView imageName;
RelativeLayout parentLayout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
imageName = itemView.findViewById(R.id.image_name);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
}
XML for activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainParkActivity"
android:clipToPadding="false">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
XML for fragment_home
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
In your fragment_home.xml, your Recyclerview has layout_height and layout_width 0dp. Change it to match_parent. Everything else in your code is correct. I'd have commented it, but I currently do not have commenting privileges! Accept it if you found it helpful!
In your activity_main layout, you have app:layout_constraintBottom_toTopOf="parent"
It should be app:layout_constraintBottom_toBottomOf="parent"
Your layout is currently displaying above the screen in your fragment.
use
adapter.notifyDataSetChanged();
after setting the adapter for RecycleView.

RecyclerView onCreateViewHolder not being called

i want to add data inside RecyclerView.After done compiled, RecyclerView not showed inside virtual mobile.When i check again,onCreateViewHolder is not being called.Hope you guys can help me solve this problem.
activity_main.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="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.mycustomvolley.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/recyclerview"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
row_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="#BBDEFB"
>
<com.android.volley.toolbox.NetworkImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_margin="8dp"
android:id="#+id/networkImage"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textName"
android:text="#string/title"
android:layout_toRightOf="#+id/networkImage"
android:padding="8dp"
android:textColor="#color/colorText"
android:textSize="18dp"
android:textStyle="bold"/>
</RelativeLayout>
MainActivity.java
package com.example.user.mycustomvolley;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<ArtInformation> arrayList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
BackgroundTask backgroundTask = new BackgroundTask(MainActivity.this);
arrayList = backgroundTask.getArrayList();
adapter = new RecyclerAdapter(arrayList,this);
recyclerView.setAdapter(adapter);
}
}
BackgroundTask.java
package com.example.user.mycustomvolley;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
/**
* Created by User on 9/17/2016.
*/
public class BackgroundTask {
Context context;
ArrayList<ArtInformation> arrayList = new ArrayList<>();
private static final String json_url = "http://192.168.1.7/volley/imagetext/getData.php";
public BackgroundTask(Context context){
this.context = context;
}
public ArrayList<ArtInformation> getArrayList(){
//make json request.
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST,json_url,(String) null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
int count = 0;
while (count < response.length()) {
try{
JSONObject jsonObject = response.getJSONObject(count);
String artTemp = jsonObject.getString("art");
String imageTemp = jsonObject.getString("image");
ArtInformation artInformation = new ArtInformation(artTemp,imageTemp);
arrayList.add(artInformation);
count++;
}catch (JSONException ex){
ex.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context,"Error:[JSON Process",Toast.LENGTH_LONG).show();
//progressDialog.hide();
error.printStackTrace();
}
});
MySingleton.getmInstances(context).addToRequestQueue(jsonArrayRequest);
return arrayList;
}
}
RecyclerAdapter.java
package com.example.user.mycustomvolley;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by User on 9/17/2016.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
ArrayList<ArtInformation> arrayList = new ArrayList<>();
Context context;
private ImageLoader imageLoader;
public RecyclerAdapter(ArrayList<ArtInformation> list, Context context){
super();
this.arrayList = list;
this.context = context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d("","call onCreateViewHolder");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_list,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ArtInformation artInformation = arrayList.get(position);
// Log.d("Check:","art:"+artInformation.getArt_name()+" / path:"+artInformation.getImage_path());
imageLoader = MySingleton.getmInstances(context).getImageLoader();
imageLoader.get(artInformation.getImage_path(), ImageLoader.getImageListener(holder.image, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
holder.image.setImageUrl(artInformation.getImage_path(),imageLoader);
holder.artName.setText(artInformation.getArt_name());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView artName;
NetworkImageView image;
public MyViewHolder(View itemView){
super(itemView);
artName = (TextView) itemView.findViewById(R.id.textName);
image = (NetworkImageView) itemView.findViewById(R.id.networkImage);
}
}
}
If getItemCount method returns 0 i.e arrayList.size() == 0 then RecyclerView never tries to instantiate a view.

custom listview with images

Hello guys im new to android dev and im having an unknown error with my code,i finished the code but when i run it sends me back to "dataprovider". can anyone please help me?
My fragment.
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class Places_Fragment extends ListFragment {
ListView listView;
int[] images ={R.drawable.cake_1,R.drawable.cake_2,R.drawable.cake_3,R.drawable.cake_4,R.drawable.cake_5,R.drawable.cake_6};
String[] places;
PlaceAdapter adapter;
public static Places_Fragment newInstance() {
Places_Fragment fragment = new Places_Fragment();
return fragment;
}
public Places_Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_places_, container, false);
listView =(ListView)rootView .findViewById(R.id.listView);
places = getResources().getStringArray(R.array.places);
int i=0;
adapter = new PlaceAdapter(getActivity(),R.layout.row_layout);
listView.setAdapter(adapter);
for(String item: places)
{
PlaceDataProvider dataProvider = new PlaceDataProvider(images[i],places[i]);
adapter.add(dataProvider);
i++;
}
return rootView;
}
}
My custom Adapter.
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Bonfire-ADv on 4/23/2016.
*/
public class PlaceAdapter extends ArrayAdapter {
List list =new ArrayList();
public PlaceAdapter(Context context, int resource) {
super(context, resource);
}
static class DataHandler
{
ImageView images;
TextView Places;
}
#Override
public void add(Object object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return this.list.size();
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
DataHandler handler;
if (convertView== null)
{
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate (R.layout.row_layout, parent, false);
handler = new DataHandler();
handler.images = (ImageView) row.findViewById(R.id.image_view);
handler.Places = (TextView) row.findViewById(R.id.place_title);
row.setTag(handler);
}
else
{
handler = (DataHandler) row.getTag();
}
PlaceDataProvider dataProvider;
dataProvider = (PlaceDataProvider) this.getItem(position);
handler.images.setImageResource(dataProvider.getImages());
handler.Places.setText(dataProvider.getPlaces());
return row;
}
}
My dataprovider.
package com.selfstudios.cakeplanet;
public class PlaceDataProvider {
private int images;
private String places;
public PlaceDataProvider(int images,String places)
{
this.setImages(images);
this.setPlaces(places);
}
public int getImages() {
return images;
}
public void setImages(int images) {
this.images = images;
}
public String getPlaces() {
return places;
}
public void setPlaces(String places) {
this.places = places;
}
}
my frag xml.
<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="com.selfstudios.cakeplanet.Places_Fragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_weight="1" />
</LinearLayout>
my row xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="80dp">
<ImageView
android:layout_width="100dp"
android:layout_height="75dp"
android:id="#+id/image_view"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:paddingStart="2dp"
android:paddingLeft="2dp"
android:paddingEnd="2dp"
android:src="#drawable/cake_1"
/>
<TextView
android:id="#+id/place_title"
android:layout_width="175dp"
android:layout_height="75dp"
android:text="place name"
android:gravity="center"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="true"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/colorPrimaryDark"
android:layout_alignBottom="#+id/image_view">
</View>
</RelativeLayout>
Try replacing
for(String item: places)
with the more traditional
for(int i; i < places.size(); i++

StaggeredGrid RecyclerView not showing anything

I'm trying to inflate a RecyclerView which has as StaggeredGrid Layout, but it is not showing anything. I've pretty much copied previous code I've used before for the RecyclerView so I'm kind of stumped.
In MuseumStoriesViewHolder.onCreateViewHolder() the return of holder has the following value ViewHolder{337ec22b position=-1 id=-1, oldPos=-1, pLpos:-1 unboundundefined adapter position no parent} I'm not sure if this is realated, but it was something that seemed off to me.
It also might help to know that the fragment I'm inflating this RecyclerView is a nested Fragment.
Any help would be greatly appreciated.
MuseumFragment
package com.example.android.radiobuttontestproject.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.radiobuttontestproject.R;
import com.example.android.radiobuttontestproject.adapters.MuseumStoriesAdapter;
import com.example.android.radiobuttontestproject.helpers.pojo.StoryObject;
import com.example.android.radiobuttontestproject.test.SampleDataFactory;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MuseumFragment extends Fragment {
private List<StoryObject> storyObjectList;
private StaggeredGridLayoutManager storyGridLayoutManager;
private MuseumStoriesAdapter storyAdapter;
#Bind(R.id.stories_recycler_view) RecyclerView storiesRecyclerView;
public static MuseumFragment newInstance() {
MuseumFragment fragment = new MuseumFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
public MuseumFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_museum, container, false);
ButterKnife.bind(this, view);
//Sets up the stories
SampleDataFactory sampleDataFactory = new SampleDataFactory();
storyObjectList = sampleDataFactory.getSampleStories(
getResources().getStringArray(R.array.test_titles_for_grid_museum1),
getResources().getStringArray(R.array.test_desc_for_grid_museum1));
storyGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
storiesRecyclerView.setLayoutManager(storyGridLayoutManager);
storyAdapter = new MuseumStoriesAdapter(getActivity().getApplicationContext(), storyObjectList);
storiesRecyclerView.setAdapter(storyAdapter);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
}
MuseumStoriesAdapter
package com.example.android.radiobuttontestproject.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.radiobuttontestproject.R;
import com.example.android.radiobuttontestproject.helpers.pojo.StoryObject;
import java.util.List;
public class MuseumStoriesAdapter extends RecyclerView.Adapter<MuseumStoriesAdapter.MuseumStoriesViewHolder> {
private List<StoryObject> itemList;
private LayoutInflater inflater;
private Context context;
public MuseumStoriesAdapter(Context context, List<StoryObject> itemList) {
this.itemList = itemList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
#Override
public MuseumStoriesViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = inflater.inflate(R.layout.view_box_small, viewGroup, false);
MuseumStoriesViewHolder holder = new MuseumStoriesViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MuseumStoriesViewHolder holder, int position) {
holder.title.setText(itemList.get(position).getTitle());
holder.desc.setText(itemList.get(position).getDescription());
}
#Override
public int getItemCount() {
return itemList.size();
}
class MuseumStoriesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView type,title,desc;
public MuseumStoriesViewHolder(View itemView) {
super(itemView);
//Tried Butterknife, but it doesn't seem like it was working in the view holder. - Peter
type = (TextView) itemView.findViewById(R.id.small_box_type);
title = (TextView) itemView.findViewById(R.id.small_box_title);
desc = (TextView) itemView.findViewById(R.id.small_box_desc);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked Position = " + getPosition(), Toast.LENGTH_SHORT).show();
}
}
}
fragment_museum.xml
<ScrollView
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="com.example.android.radiobuttontestproject.fragments.MuseumFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:id="#+id/museum_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/header_margin"
android:gravity="center"
android:textSize="#dimen/font_larger"
android:text="#string/museum_header" />
<android.support.v7.widget.RecyclerView
android:id="#+id/stories_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
view_box_small.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/small_box_margin"
android:background="#color/small_box_background_color">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--TODO Make layout_height wrap contenet -->
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#color/test_color2"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/abc_btn_rating_star_off_mtrl_alpha"
/>
</FrameLayout>
<TextView
android:id="#+id/small_box_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/font_small"
android:textColor="#color/font_red"
android:text="Object"
/>
<TextView
android:id="#+id/small_box_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/font_large"
android:textColor="#color/font_black"
android:text="Sample Text Here"
/>
<TextView
android:id="#+id/small_box_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/font_normal"
android:textColor="#color/font_black"
android:textStyle="italic"
android:text="Sample Text Here"
/>
</LinearLayout>

Error while using ListFragement

I am developing an android app for online payment for which I want to display Lists of restaurants under the tab named Restaurants. Same as this some other tabs will have list below them and a few tabs will have form under them (if possible and not difficult otherwise I will stay with the lists only)
Here is the code I have written. It contains an error in the Adapter class and may be some logical errors which I am not sure about as this is my first ever android app.
Main.java File
package com.example.tabswithswipe;
import com.example.tabsswipe.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "Retaurants", "Super Store", "Fuel Stations"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
acivity_main.xml File
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
items_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name" />
</LinearLayout>
TabPagerAdapter.java Adapter File (Error in this file)
package com.example.tabsswipe.adapter;
import com.example.tabsswipe.*;
import android.app.ListFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new SuperStoreFragment();
case 1:
return new FuelStationsFragment();
case 2:
return new RestaurantsFragment(); //Error Here
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
RestaurantsFragment.java
package com.example.tabsswipe.adapter;
//import com.example.MainActivity.MyAdapter;
import com.example.tabswithswipe.R;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class RestaurantsFragment extends ListFragment {
LayoutInflater inflater;
ViewGroup container;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
this.inflater = inflater;
View rootView = inflater.inflate(R.layout.list_items, container, false);
setListAdapter(new MyAdapter(getActivity(), android.R.layout.simple_list_item_1, R.id.textView1, getResources().getStringArray(R.array.items)));
return rootView;
}
private class MyAdapter extends ArrayAdapter<String>
{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_items, container, false);
String [] items = getResources().getStringArray(R.array.items);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
TextView tv = (TextView) row.findViewById(R.id.textView1);
tv.setText(items[position]);
if(items[position].equals("kfc"))
{
iv.setImageResource(R.drawable.kfc);
}
else if(items[position].equals("pizzaHut"))
{
iv.setImageResource(R.drawable.pizzahut);
}
else if(items[position].equals("Domino"))
{
iv.setImageResource(R.drawable.dominos);
}
else if(items[position].equals("hardees") )
{
iv.setImageResource(R.drawable.hardees);
}
else if(items[position].equals("TuttiFrutti"))
{
iv.setImageResource(R.drawable.tuttifrutti);
}
else if(items[position].equals("McDonalds"))
{
iv.setImageResource(R.drawable.mcdonalds);
}
else if(items[position].equals("21 Street"))
{
iv.setImageResource(R.drawable.ic_launcher);
}
return row;
}
}
// View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
// return rootView;
// }
}
SuperStoreFragment.java
package com.example.tabsswipe.adapter;
import com.example.tabswithswipe.R;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SuperStoreFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_super_stores, container, false);
return rootView;
}
}
fragment_super_stores.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fa6a6a" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Design Super Stores Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
FuelStationsFragment.java
package com.example.tabsswipe.adapter;
import com.example.tabswithswipe.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FuelStationsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_fuel_stations, container, false);
return rootView;
}
}
fragment_fuel_stations.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff8400" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Design Fuel Stations Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
Please tell me how, where and what error occurred. Any kind of help will be really appreciated e.g. Tutorial, code example or even the error resolution of this code. Please ignore my silliness if any. Thank You.

Categories

Resources