开发者

How to bind data in gridview found in fragment in android app?

开发者 https://www.devze.com 2023-03-25 06:04 出处:网络
I have a fragment class and its layout is in xml. Inside the xml fragment layout is the gridview. I wanted the gridview to show the data.

I have a fragment class and its layout is in xml. Inside the xml fragment layout is the gridview. I wanted the gridview to show the data.

What happens is that when I run the android app, the app failed to execute/run. It has stopped unexpectedly.

public class FrontPageFragment extends Fragment {

//private ArrayAdapter adapter;

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

    final String [] items=new String[]{"Item1","Item2","Item3","Item4"};
    ArrayAdapter ad=new ArrayAdapter<String>(this.getActivity().getApplicationContext(),R.layout.frontpage,items);

    View fragmentView=getView();    
    GridView grid=(GridView)fragmentView.findViewById(R.id.forApprovalOrders);
    grid.setAdapter(ad);

    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.frontpage, container, false);

}

=========================================================================

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

    <GridView android:id="@+id/forApprovalOrders" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:columnWidth="90dp"
                android:numColumns="auto_fit"
                android:verticalSpacing="10dp"
                android:horizontalSpacing="10dp"
                android:stretchMode="columnWidth"
                android:gravity="center"
    >
    </GridView>

   开发者_JAVA技巧 <ExpandableListView android:id="@+id/sampleExpandable" android:layout_height="wrap_content" android:layout_width="match_parent"></ExpandableListView>


</LinearLayout>


i think you want these ad.notifyDataSetChanged();


Might be wrong, but it seems like you are setting the adapter before inflating the layout. This will cause a problem because before you inflate the layout, the gridview doesn't exist and grid.setAdapter(...) will throw a NullPointerException. Try moving the following snippet from the onCreateView(...) to onActivityCreated(...), after calling super.onActivityCreated(...):

final String [] items=new String[]{"Item1","Item2","Item3","Item4"};
ArrayAdapter ad=new ArrayAdapter<String>
     (this.getActivity().getApplicationContext(),R.layout.frontpage,items);

View fragmentView=getView();    
GridView grid=(GridView)fragmentView.findViewById(R.id.forApprovalOrders);
grid.setAdapter(ad);


Have you tried this:

    View fragmentView= inflater.inflate(R.layout.frontpage, container, false); 
    GridView grid=(GridView)fragmentView.findViewById(R.id.forApprovalOrders);
    grid.setAdapter(ad);

    // Inflate the layout for this fragment
    return fragmentView;

If it's not solved, what is the error message and at what line did it stop while debugging?

0

精彩评论

暂无评论...
验证码 换一张
取 消