0

I am Trying to insert GridView in fragment. I have developed custom adapter to populate the gridview. Now everything is working fine but the gridview is not displaying in the fragment. I have check the getCount() method it is returning total length. So that means data is there but it is not displaying. I am pretty new with gridview so have no idea why it is happening. I tried the following answer but they didn't help me either. Android: getView in adapter never called

Custom Adapter getView() method is not called

Gridview not displaying in fragment

Here is my code:

Tile Fragment Class:

public class TileFragment extends Fragment
{

public TileFragment(){
}

GridView gv;
Context context;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.mipmap.dicover_disable,R.mipmap.discover_active, R.mipmap.home_disable,
                                 R.mipmap.home_active,R.mipmap.lists_disable,R.mipmap.lists_active,
                                 R.mipmap.profile_disable,R.mipmap.profile_active,R.mipmap.ic_search };

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    @SuppressLint("InflateParams")
 //   View v = LayoutInflater.from(getActivity()).inflate(R.layout.tile_fragment, null);
    View v = inflater.inflate(R.layout.tile_fragment, container, false);
    gv = (GridView) v.findViewById(R.id.gridView1);
    gv.setAdapter(new CustomAdapter(getActivity(), prgmNameList,prgmImages));

    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
}
  }

Custom Adapter:

public class CustomAdapter extends BaseAdapter {

String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;

public CustomAdapter(FragmentActivity tileFragment, String[] prgmNameList, int[] prgmImages) {

    result=prgmNameList;
    imageId=prgmImages;
    context = tileFragment;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {

    return imageId.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {

    return position;
}

public class Holder
{
    TextView tv;
    ImageView img;
}
@SuppressLint("ViewHolder")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    Holder holder=new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.programlist, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);

    rowView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
        }
    });

    return rowView;
}
    }

tile_fragment:

<?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" >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:numColumns="3"
    android:visibility="visible">

</GridView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textStyle="bold"
    android:text=" Computer Languages..." />

   </RelativeLayout>

prgramlist

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_gravity="center"
    android:layout_width="88dp"
    android:layout_height="88dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:text="TextView" />

  </LinearLayout>

This is how I am adding the fragment:

HomeFragment:

public class HomeFragment extends Fragment
  {
public HomeFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{

    FragmentTabHost mTabHost = new FragmentTabHost(getActivity());
    mTabHost.setup(getActivity(), getChildFragmentManager());

    Bundle b = new Bundle();
    b.putString("key", "Tile");
    mTabHost.addTab(mTabHost.newTabSpec("tile").setIndicator("Tile"), TileFragment.class, b);
    //
    b = new Bundle();
    b.putString("key", "Map");
    mTabHost.addTab(mTabHost.newTabSpec("map").setIndicator("Map"), ProfileFragment.class, b);
    return mTabHost;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}
//
   }

Any Help will be appreciated ...

Community
  • 1
  • 1
Umair
  • 5,756
  • 15
  • 39
  • 47

3 Answers3

2

Try this:

mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment); 

where R.layout.my_parent_fragment is your layout fragment where you have the tabhost.

Once you have verified that the GridView is displayed correctly in an Activity, we could deduce that this is not a problem with the Gridview neither the Adapter, the problem come from your FragmentTabHost definition or setup. You are returning your tabHost from your onCreateView(), for that you must set your layout or container in your tabHost, if you don't do this, how the fragment know where find your layout?

For more info, api here

juanhl
  • 1,120
  • 2
  • 11
  • 15
0

Try using this layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3"
  >

</GridView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textStyle="bold"
    android:text=" Computer Languages..." />

</LinearLayout>
Linh
  • 43,513
  • 18
  • 206
  • 227
Mightian
  • 6,853
  • 3
  • 37
  • 50
0

Can't figure out the root of the problem but try this:

View rowView = convertView;
if(rowView != null) {
    rowView = inflater.inflate(R.layout.programlist, null);
}
Amit Tiwari
  • 3,478
  • 6
  • 26
  • 69
  • 1
    I am sure if the method is not called, that will not be the problem – juanhl Oct 08 '15 at 08:26
  • Ok. Try logging some messages at start and end of every function to trace where exactly the flow is being stopped. It might give you insight into where the actual problem may lie. – Amit Tiwari Oct 08 '15 at 08:29
  • @AmitTiwari debugger doesn't even go to getView() method – Umair Oct 08 '15 at 08:31
  • Well, then the problem is with some other code. Check if the adapter is being created by placing a log statement in it's constructor. That way you will know whether only getView() is not called or the whole adapter is not created. – Amit Tiwari Oct 08 '15 at 08:42
  • @AmitTiwari I tried that adapter is created infact. But on the side note I tried to enter some random values in textview and display it as a fragment layout. I didn't displayed either. So I think the problem lies somewhere else – Umair Oct 08 '15 at 08:52