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

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

Related

ParseQueryAdapter for ListFragment

As ParseQueryAdapter is meant to be used with an Activity - what is the general design idea for using a ParseQueryAdapter for a ListFragment? I want to display a custom list view.
I've tried to use a ParseQueryAdapter with a ListFragment, but the images are not displayed properly. If I use the exact code with an Activity, the images load correctly.
How can you use a ParseQueryAdapterin conjunction with a ListFragment?
I've posted the general code, but the text from the query loads wonderfully in the list, the images however don't always load or when they do..take a LONG time to show up.
<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" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:divider="#null"
android:dividerHeight="0px"
/>
</RelativeLayout>
public class PlacesTab extends ListFragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.places_tab, container, false);
mainAdapter = new ParseQueryAdapter<ParseObject>(getActivity(), “Places”);
mainAdapter.setTextKey("name");
mainAdapter.setImageKey(“placeImage");
listView = (ListView) rootView.findViewById(android.R.id.list);
listView.setAdapter(mainAdapter);
mainAdapter.loadObjects();
return rootView;
}
}//end class

GraphView in Fragment

I want to display a graph in a fragment, but I didn't found many information about how to use GraphView in general. Can someone please help me to display a simple graph?
I started with this: http://android-graphview.org/#doc_createsimplegraph but don't get it working in a fragment.
What I did was to create a fragment_graph.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:id="#+id/graph1"
android:background="#102f65" />
</LinearLayout>
and add the relevant code in the Fragment class:
public class GraphFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_graph, container, false);
// add the data here if necessary
// ...
LinearLayout layout = (LinearLayout) view.findViewById(R.id.graph1);
layout.addView(graphView);
return view;
}
// ...
}
Then you can use the fragment "as is".
Most credit actually goes to vogella.
define a empty LinearLayout with fixed width and height in your layout.xml file and then add the graphview view to it.
Layout
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:id="#+id/graph1" />
Java
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
Just for the record:
You cannot use it in connection with ScrollViews. If a ScrollView is wrapping your LinearLayout the lib doesn't show anything.

Why does onScrollListener think ListView is not created?

So the error that is thrown is
java.lang.IllegalStateException: Content view not yet created
This is thrown when I uncomment the getListView().setOnScrollListener(this); line.
public class MyCategoryFragment extends ListFragment implements OnScrollListener {
private ArrayList<Article> m_articles = new ArrayList<Article>();
public ArticleAdapter m_artadapter;
public MyCategoryFragment() {
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_category, container, false);
m_articles = manager.loadCategory(getActionBar().getTitle().toString(), 10);
m_artadapter = new ArticleAdapter(this.getActivity(), R.layout.article_button, m_articles);
this.setListAdapter(m_artadapter);
//getListView().setOnScrollListener(this);
return rootView;
}
Here is the layout just so you can see it is the correct listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#android:id/empty"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="It's not you it's us..."/>
</LinearLayout>
I have researched this extensively and I believe in must be something so simple but it is taking more than a day to fix this. Thanks
as the onCreateView doc stays:
creates and returns the view hierarchy associated with the fragment
so since the method does not return, you will are not able to access the ListView through getListView(). You can obtain a valid reference in the OnActivityCreate callback.

ListView not appearing in fragment

So I have two Fragments inside an Activity using a PageAdapter.
One of the Fragments is a Listview and the other is a GridView of images.
The fragments are created successfully but the ListView is empty
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = inflater.inflate(R.layout.eventlist ,container, false);
String[] x = new String[]{"AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC"};
ListView listView = (ListView) view.findViewById(R.id.listView);
ArrayAdapter<String> test = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1,x);
listView.setAdapter(test);
return view;
}
XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
You should not check null for listview. But rather check if the list is empty.
Change it to listView.getChildCount()==0.
But I think you dont need to check because it will always be empty because it's a new created view. And no items on the list. If you want to check correctly, put the condition check after you set the adapter to the list.
Check if this works.

Using screen fragments on android

I am working on android app and am trying to get fragments working but I've run into a problem.
When the app launches it loads an activity which is supposed to house the two fragments. Fragment1 contains a list of stored logins and fragment 2 will show the details associated with that login.
The list fragment is supposed to retrieve data from the SQLite database and display on the screen and once the item is clicked, it loads the second fragment, but I am having problem getting the first stage working.
In my main activity class I have the following.
public class PasswordListMain extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.password_management);
}
}
The password_management XML file contains the following
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/passwordList"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.BoardiesITSolutions.PasswordManager.PasswordListFrag">
</fragment>
</LinearLayout>
It just contains the fragment as this is the portrait screen, I thought I'd get this bit working before getting it working side by side.
The PasswordListFrag contains the following
public class PasswordListFrag extends ListFragment{
Common common;
com.BoardiesITSolutions.logic.ManagePasswordList managePasswordList;
ArrayList<String> savedPassword;
TextView txtNoRecords;
ListView myListView;
ArrayAdapter<Spanned> passwordArrayAdapter;
AdView adView;
#Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
{
View view = inflator.inflate(R.layout.password_list, container, false);
myListView = getListView();
common = new Common(getActivity().getApplicationContext());
//AdView adView = (AdView)findViewById(R.id.adView);
common.requestAdvert(adView);
managePasswordList = new ManagePasswordList(getActivity().getApplicationContext());
//txtNoRecords = (TextView)findViewById(R.id.password_noRecords);
populateListArray();
common.showToastMessage("Adapter updated", Toast.LENGTH_LONG);
//myListView.setOnItemClickListener(mListView);
return view;
}
private void populateListArray()
{
ArrayList<Spanned> passwords = managePasswordList.getPasswordList();
if (passwords != null && passwords.size() > 0)
{
passwordArrayAdapter = new ArrayAdapter<Spanned>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, passwords);
setListAdapter(passwordArrayAdapter);
passwordArrayAdapter.setNotifyOnChange(true);
myListView.setTextFilterEnabled(true);
txtNoRecords.setVisibility(View.GONE);
}
else
{
txtNoRecords.setVisibility(View.VISIBLE);
}
}
}
Below is the XML for the list fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/password_noRecords"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="There are currently\nno saved logins"
android:textSize="20dp"
android:textStyle="bold|italic"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/adView">
</ListView>
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="5555555"
ads:adSize="BANNER"
android:layout_alignParentBottom="true">
</com.google.ads.AdView>
</RelativeLayout>
When the app loads it instantly crashes when it tries to load this screen.
The error is
java.lang.RuntimeException: Unable to start activity ComponentInfo
PasswordListMain: android.view.InflatException: Binary XML file line
#7 Error inflating class fragment
I have no idea what's causing this or how to fix the problem.
Thanks for any help you can provide.
My best guess for the issue is this line (but admittedly I'm unsure):
myListView = getListView();
You're calling it before the fragment has a view - and I don't think that will work too well. I would recommend doing your initialization work of the views involved in onActivityCreated() instead of in onCreateView() and the other components (like your data store thing and Common) in onCreate()

Categories

Resources