I want to display custom listview in navigation drawer's fragment page. However, in my fragment class, I'm getting an error which seems I cannot set a custom adapter.
package android_gcm_client.mynavigation;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
public class List_Fragment extends Fragment {
View rootview;
ArrayList prgmName;
public static int [] prgmImages= {R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
#Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview=inflater.inflate(R.layout.custom_layout,container,false);
CustomAdapter ca = new CustomAdapter(this,prgmNameList,prgmImages);
ListView listview=(ListView) getView().findViewById(R.id.listView);
listview.setAdapter(ca);
return rootview;
}
}
It seems the error happens in line below (custom adapter can not be applied).
CustomAdapter ca = new CustomAdapter(this,prgmNameList,prgmImages);
In MainActivity I call the fragment as follows:
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment objFragment=null;
switch(position) {
case 0:
objFragment=new ListFragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, objFragment)
.commit();
}
I tried different ways to display custom listview for navigation item selected method.
I tried to directly call activity instead of fragment but problem was navigation drawer not visible for all activity. So I tried to call CustomAdapter in activity as I'm doing in fragment.
I have struggle to solve this error. (Sorry for bad English).
You are using this in custom adapter. Usually adapters wants a Context in constructor call. But this constructor is called inside a fragment and this can't be used.
You can use getActivity() as Context inside a Fragment. But.
Sometimes it can return a null if it is called before onAttach() of this fragment.
CustomAdapter ca = new CustomAdapter(getActivity(), prgmNameList, prgmImages);
Also you can use Application context by creating static variable inside your Application class:
public class Application extends android.app.Application {
public static Context AppContext = null;
#Override
public void onCreate() {
super.onCreate();
AppContext = getApplicationContext();
// You can use this line to solve styling problems
// because Manifest android:theme is not working
AppContext.setTheme(R.style.AppTheme);
}
}
and create adapter like this:
CustomAdapter ca = new CustomAdapter(Application.AppContext, prgmNameList, prgmImages);
Related
In ViewPager, I have displayed the fragment which has custom ListView. Now I tried to display each item details of the custom list view on another dialog fragment if I clicked on any item. But I have faced the following issue
"The method show(android.app.FragmentManager, java.lang.String) in
the type DialogFragment is not applicable for the arguments
(android.support.v4.app.FragmentManager, java.lang.String)"
I have used the getSupportFragmentManager() instead of the getFragmentManager() but it does not work.
Can you please check if you're importing this:
import android.support.v4.app.DialogFragment;
And not this:
import android.app.DialogFragment;
to call another fragment from within a fragment you should use getChildFragmentManager(); //preferred or getActivity().getSupportFragmentManager()
This trick may work for you make Singleton object of your Activity class like this :
public class YourActivityclass extends AppCompatActivity{
static YourActivityclass yourActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
yourActivity=this;
}
public static YourActivityclass getInstance(){
return yourActivity;
}
}
then to open dialogfragment on a fragment call like this :
date.show(YourActivityclass.getInstance().getSupportFragmentManager(), "name");
AppIntro is an Android Library that helps you make a cool intro for your app.
AppIntro Library
// Add your slide's fragments here
// AppIntro will automatically generate the dots indicator and buttons.
addSlide(first_fragment);
addSlide(second_fragment);
addSlide(third_fragment);
addSlide(fourth_fragment);
But when I try to attach a fragment created by me, I get an error.
And this is the error:
How can i do to add my fragment1?
While addSlide() method needs android.supportv4.app.Fragment as parameter, so your custom Fragment1 should extend from android.supportv4.app.Fragment class.
1、
import android.support.v4.app.Fragment;
2、
public class Slide_First extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.slide_first, null);
return view;
}
}
first: create your fragments like always and add the xml elements you need to it, and in your activity just do this
Edit: It is not necessary to extend to AppIntro or AppIntro2
public class MyIntro extends AppIntro2 implements ISlidePolicy {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
Intro1 fragment1 = new Intro1 ();
Intro2 fragment2 = new Intro2 ();
Intro3 fragment3 = new Intro3 ();
addSlide (fragment1);
addSlide (fragment2);
addSlide (fragment3);
//Only personalization
showStatusBar (false);
showSkipButton (false);
setFadeAnimation ();
setBarColor (Color.TRANSPARENT);
}
}
Edit2: import these dependences in your fragment
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
I have been trying to build a Master-Detail f low in android but wanted to change one of the detail fragments to a different fragment. As this is one of my first Android applications, I was just trying to make a picture appear on this new fragment. For this, I built the following two classes
1) Fragment class ( displays the picture to be displayed )
package com.userName.miscapp;
import com.userName.miscapp.dummy.DummyContent;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PictureFragment extends Fragment {
// Default Copy Constructor for the fragment
public PictureFragment() {
}
#Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState )
{
View rootView = inflater.inflate(R.layout.fragment_simple_picture, container, false);
return rootView;
}
}
2) Activity to display the same
package com.userName.miscapp;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class SimplePicture extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
PictureFragment frag = new PictureFragment();
frag.setArguments(arguments);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.simple_picture_container,frag).commit();
//setContentView(R.layout.activity_simple_picture);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.simple_picture, menu);
return true;
}
}
On compilation, it does not recognize the PictureFragment to be an extension of Fragment class. This is inspite of it being clearly written in the first file. Searching for solutions on Stackoverflow said to extend FragmentActivity instead of Activity and trying to use getSupportFragmentManager() neither of which helped.
PS : Using 11 as the base for the current application.
Any help would be appreciated
Thanks
This is because you are using android.app.Fragment from new API in conjunction with android.support.v4.app.FragmentManager from support library, you should replace import in your PictureFragment from android.app.Fragment to android.support.v4.app.Fragment to make it work.
I am making a view pager to make a slide show for images. I took code from Android developers, but I was facing some issues, fragment was not recognized, I think it was because my android was 2.33. So to solve that I imported a jar file android.support.v4.jar
My issues were resolved but now I am getting this error that getfragmentmanager() is undefined
and another issue "The method invalidateOptionsMenu() is undefined for the type new ViewPager.SimpleOnPageChangeListener(){}"
Here is my code, can any one please help ??
My platform is 2.3.3 and api level is 10 and in manifest I have this
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
Code :
package com.example.profilemanagment;
import android.support.v4.app.Fragment;
import android.support.v4.app.*;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
public class ScreenSlideActivity extends FragmentActivity {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 5;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When changing pages, reset the action bar actions since they are dependent
// on which page is currently active. An alternative approach is to have each
// fragment expose actions itself (rather than the activity exposing actions),
// but for simplicity, the activity provides the actions in this sample.
invalidateOptionsMenu();
}
});
}
/**
* A simple pager adapter that represents 5 {#link ScreenSlidePageFragment} objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
Quoting from the docs.
When using this class (FragmentActivity) as opposed to new platform's built-in fragment and loader support, you must use the getSupportFragmentManager() and getSupportLoaderManager() methods respectively to access those features.
Since you are extending FragmentActivity use getSupportFragmentManager()
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
Check the docs
FragmentActivity does not have getFragmentManager()
I am implementing an app which has a gridview of images in one activity and one fragment for each image which contains the image in full screen. When i click on any of the images in the grid, it should open up the corresponding fragment. However, we cannot use intent to do this.
here is my code
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
if(position==0)
{
Intent i=new Intent(Gallery.this,ImageFrag1.class);
startActivity(i);
}
and the fragment is
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ImageFrag1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.imagefrag1, container, false);
}
}
This fragment is bound to an activity ImagesSwipe. SO how do i achieve the transition between grid view item and its corresponding fragment.
Thanks
You don't need one Fragment for one Image. Just reuse one Fragment with an ImageView in it's layout for every Image.
Fragments aren't invoked like Activities through an Intent. They can only exist as part off an Activity, that is what they are designed for. Think about them as a reusable UI-Modul for an Activity. To add a Fragment to an Activity you have to use the FragmentManager and the FragmentTransaction classes, which provide all interactions with Fragments.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(YourFragment.newInstance(), null);
ft.commit();
Have a look at this guide from the Google Docs where the basic things about GridViews are described. In Addition you should read about Fragments. And here is a Tutorial about your approach.
You may want to check out DialogFrament, here is an example.
Instead of using intent you use FramentManager:
if(position==0)
{
FragmentManager fm = getFragmentManager();
ImageFrag1 imageDialog = new ImageFrag1()
ImageFrag1.show(fm, "image_title");
}
And your dialogFrament becomes:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ImageFrag1 extends DialogFragment {
public ImageFrag1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_fragment, container, false);
}
}