Showing posts with label Error in updating the main content by replacing fragments In my sample project. Show all posts
Showing posts with label Error in updating the main content by replacing fragments In my sample project. Show all posts
on Monday, July 7, 2014


In my sample project, I implemented the code using activity, running fine.


After then I implemented that code in my main App which is using Fragment.


For that I extends Fragment, made a default constructor, made onCreateView & inside that inflated the layout, then changed getApplicationContext() to getActivity(). Thats how I managed to remove errors in my main app.


An in my MainActivity this is how we update the main content by replacing fragments by calling constructor & assigning it to fragments:



/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new IntroductionFragment();
break;
case 1:
fragment = new PrefaceFragment();
break;
case 2:
fragment = new PreambleFragment();
break;
case 3:
fragment = new ContentsFragment();//ERROR here: because I modified this class
break;

case 4:
fragment = new SchedulesFragment();
break;
case 5:
fragment = new AppendixFragment();
break;
case 6:
fragment = new AmendmentFragment();
break;
case 7:
fragment = new PhotoFragment();
break;
default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}


That line shows me Type mismatch: cannot convert from ContentsFragment to Fragment


I hope the problem is clear. Please tell me how can I avoid this error...


This is ContentsFragment.java



public class ContentsFragment extends Fragment// implements OnTouchListener
{
public ContentsFragment() {
// TODO Auto-generated constructor stub
}

public static ArrayList<GS> q = new ArrayList<GS>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBAdapter db = DBAdapter.getDBAdapter(getActivity());

if (!db.checkDatabase())
db.createDatabase(getActivity());

db.openDatabase();

q = db.getData();

// setContentView(R.layout.sample_activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v =inflater.inflate(R.layout.sample_activity, container, false);


final FloatingGroupExpandableListView list = (FloatingGroupExpandableListView) v.findViewById(R.id.sample_activity_list);

// final LayoutInflater inflater = getLayoutInflater();



// Even though the child divider has already been set on the layout file, we have to set it again here
// This prevents a bug where the background turns to the color of the child divider when the list is expanded
list.setChildDivider(new ColorDrawable(Color.BLACK));

final ArticlesAdapter adapter = new ArticlesAdapter(getActivity());
final WrapperExpandableListAdapter wrapperAdapter = new WrapperExpandableListAdapter(adapter);
list.setAdapter(wrapperAdapter);

for(int i = 0; i < wrapperAdapter.getGroupCount(); i++) {
list.collapseGroup(i);//expandGroup(i);
}

list.setOnScrollFloatingGroupListener(new FloatingGroupExpandableListView.OnScrollFloatingGroupListener() {

@Override
public void onScrollFloatingGroupListener(View floatingGroupView, int scrollY) {
float interpolation = - scrollY / (float) floatingGroupView.getHeight();

// Changing from RGB(162,201,85) to RGB(255,255,255)
final int greenToWhiteRed = (int) (0 + 3 * interpolation);
final int greenToWhiteGreen = (int) (0 + 6* interpolation);
final int greenToWhiteBlue = (int) ( 0+ 20 * interpolation);
final int greenToWhiteColor = Color.argb(200, greenToWhiteRed, greenToWhiteGreen, greenToWhiteBlue);

// Changing from RGB(255,255,255) to RGB(0,0,0)
final int whiteToBlackRed = (int) (255 - 1 * interpolation);
final int whiteToBlackGreen = (int) (255 - 1 * interpolation);
final int whiteToBlackBlue = (int) (255 - 1 * interpolation);
final int whiteToBlackColor = Color.argb(255, whiteToBlackRed, whiteToBlackGreen, whiteToBlackBlue);


final View background = floatingGroupView.findViewById(R.id.sample_activity_list_group_item_background);
background.setBackgroundColor(greenToWhiteColor);

final TextView text = (TextView) floatingGroupView.findViewById(R.id.sample_activity_list_group_item_text);
text.setTextColor(whiteToBlackColor);

final ImageView expanded = (ImageView) floatingGroupView.findViewById(R.id.sample_activity_list_group_expanded_image);
final Drawable expandedDrawable = expanded.getDrawable().mutate();
expandedDrawable.setColorFilter(whiteToBlackColor, PorterDuff.Mode.SRC_ATOP);
}
});
return v;
}
}