I have a viewPager which uses FragmentStatePagerAdapter as adapter and in fragment class which is used in getItem() function of adapter I add some other fragments at runtime using fragment manager. Fragments are shown correctly for page zero (starting page) but for any other page I should swipe over it an then swipe back to it to see contents.(as I checked each swipe causes inner fragments and fragment managers behave correctly even the first time but nothing is shown at first)
Any solution is appreciated
Thanks in advance
adapter code in main class
class FragmntProducer extends FragmentStatePagerAdapter{
public FragmntProducer(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return new PagerFragment(pager.getCurrentItem());
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
}
fragment class used for viewpager
public class PagerFragment extends Fragment {
WeekView rowOne , rowTwo , rowThree , rowFour , rowFive , rowSix;
int cis;
public PagerFragment(int ci) {
// TODO Auto-generated constructor stub
cis = ci;
// Toast.makeText(getContext(), ci+"under construct", Toast.LENGTH_SHORT).show();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.pager_fragment_layout, container, false);
Toast.makeText(getContext(),"page: "+cis,Toast.LENGTH_SHORT).show();
// rowOne = new WeekView();
// getFragmentManager().beginTransaction().add(R.id.week_one, rowOne).commit();
rowTwo = new WeekView();
getFragmentManager().beginTransaction().add(R.id.week_two, rowTwo).commit();
// rowThree = new WeekView();
// getFragmentManager().beginTransaction().add(R.id.week_three, rowThree).commit();
rowFour = new WeekView();
getFragmentManager().beginTransaction().add(R.id.week_four, rowFour).commit();
rowFive = new WeekView();
getFragmentManager().beginTransaction().add(R.id.week_five, rowFive).commit();
rowSix = new WeekView();
getFragmentManager().beginTransaction().add(R.id.week_six, rowSix).commit();
super.onCreateView(inflater, container, savedInstanceState);
return layout;
}
}
xml layout for fragment of viewPager
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<fragment
android:id="#+id/week_one"
android:layout_width="match_parent"
android:layout_height="40dp"
android:name="com.cal.calendar.WeekView"/>
<FrameLayout
android:id="#+id/week_two"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<fragment
android:id="#+id/week_three"
android:layout_width="match_parent"
android:layout_height="40dp"
android:name="com.cal.calendar.WeekView"/>
<FrameLayout
android:id="#+id/week_four"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<FrameLayout
android:id="#+id/week_five"
android:layout_width="match_parent"
android:layout_height="40dp"
android:name="com.cal.calendar.WeekView"/>
<FrameLayout
android:id="#+id/week_six"
android:layout_width="match_parent"
android:layout_height="40dp"/>
Related
I have a SQLite Database with 3 records for example:
1 - Name1 - Do wash
2 - Name2 - Do this
3 - Name3 - Do that
Now I want to start one activity after another (for each record) when user presses a button. The Activity is always the same with other content.
So what is the best way to do this?
My thoughts are following:
- Make a for loop and use StartActivityForResult(...)
- Pass data throug all activities using intent
- Create a temporary table and read data in each activity
- Change only the content of the layout, but don't know how i could do this
So any suggestions on this?
I think for showing same type of view on multiple content you should use fragment with viewpager.
I am just suggesting code to you for viewpager and fragment -
layout for content (pages.xml)-
<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"
tools:context=".MainActivity"
android:id="#+id/rel" >
<TextView
android:id="#+id/pageContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Main activity content(activity_main.xml) -
<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"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/relativeTextview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/header"
android:padding="5dp" >
<android.support.v4.view.ViewPager
android:id="#+id/reviewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</RelativeLayout>
Pager adapter for setting on viewpager(ViewPagerAdapter.java)-
public class ViewPagerAdapter extends PagerAdapter {
int size;
Activity act;
View layout;
TextView pagecontent;
Button click;
public ViewPagerAdapter(MainActivity mainActivity, int noofsize) {
// TODO Auto-generated constructor stub
size = noofsize;
act = mainActivity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return size;
}
#Override
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.pages, null);
pagecontent = (TextView) layout.findViewById(R.id.pageContent);
int pagenumberTxt=position + 1;
pagecontent.setText("Now your in Page No " +pagenumberTxt );
((ViewPager) container).addView(layout, 0);
return layout;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
// }
}
Now in main activity((MainActivity.java) -
public class MainActivity extends Activity {
int noofsize = 6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,
noofsize);
ViewPager myPager = (ViewPager) findViewById(R.id.reviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and do not forget to mention activity in manufest.xml.
In pages.xml you can add the button on on click of this button you can change the view for next and previous(mPager.setCurrentItem(position)) using listener.
Using activities for this will take a toll on RAM. Use fragments instead.
Answering your original question, as you have cited, make use of the onActivityResult() and as for your concern in this:
-Change only the content of the layout, but don't know how i could do this
Make use of the passed data from intent and handle accordingly.
In my project i am using actionbar with swipeable threefragments(tabs with viewpager), i am trying to replace one of the fragment with fourth fragment, but iam not able to get it..
**Below is my code**
public class GetRideFragment extends Fragment implements ICustomDateTimeListener, SelectedLocationListener{
public GetRideFragment() {
}
#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_getride, container, false);
return rootView;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
fromac.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/* Intent newintent = new Intent(getActivity(), AutoSearchActivity.class);
startActivity(newintent);
getActivity().overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );*/
PlaceSearchFragment fh = new PlaceSearchFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragcontainer, fh);
ft.addToBackStack("fh");
ft.commit();
}
});
Below is my layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragcontainer">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/fragcontainer"
>
<TextView
android:id="#+id/ridetitile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get a Ride"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:textColor="#33b5e5"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:background="#drawable/gray_btn_disabled_normal" >
...............
...........
My adpater
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new GetRideFragment();
case 1:
// Games fragment activity
return new PostRideFragment();
case 2:
// Movies fragment activity
return new SearchRidesFragment();
}
return null;
}
#Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
nNot sure where i am going wrong.Any help is appreciated.
I had the same problem. I found the solution after a lot of searches. I would see 2 problems in your code: (sorry for my bad english)
1) to change an item inside the adapter (connected to your viewpager) for a given position, you have to change the items returned by the method public Fragment getItem(int index) and call the method notifyDataSetChanged() to notify to the adapter that the content is changed.
2) the implementation of the FragmentStatePagerAdapter contains a cache based on the position and it doesn't change even if you call the notifyDataSetChanged() and you return the PagerAdapter.POSITION_NONE (there is an issue reported here: https://code.google.com/p/android/issues/detail?id=37990)
what I would suggest you is:
use a FragmentPagerAdapter, implement properly the methods getItem(int index) (with the correct item at the given position), getItemPosition(Object object), and getItemId(int position)
if you want to use a FragmentStatePagerAdapter, consider the implementation suggested in the issue I liked above (NewFragmentStatePagerAdapter).
I hope it helps!
I am trying to create a master/detail flow application on Android 4.2.
I have created a master detail flow project, but I want to implement an ExpandableListView instead of the ListView that is provided.
The master/detail uses fragments, and here’s where I am stuck… I have successfully created an expandable list view in a separate project. How can I implement it inside a master/detail flow?
I assume you used the IDE wizard to create the Master/Detail example project. If this is so, then you probably saw that the wizard created an ItemListFragment class which by default extends ListFragment.
If you need to replace the simple list with an expandable one, then you'll have to:
Extend from Fragment, instead of ListFragment
Create an xml layout file where you declare an ExpandableListView
Override onCreateView() and inflate the layout that contains the ExpandableListView
Get a reference to the ExpandableListView, and then use it as you did before.
Something like this:
// extend from Fragment
public class ItemListFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate the layout that contains the ExpandableListView
View view = inflater.inflate(R.layout.fragment_items_list, container, false);
// get a reference to ExpandableListView
ExpandableListView list = (ExpandableListView)view.findViewById(R.id.my_list);
// set the adapter
// set listeners
return view;
}
}
I have no idea how to create ExpandableListView, but you can achieve with custom listview like I implemented in my Project
1. Create Custom list row
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000" >
<RelativeLayout
android:id="#+id/rel_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:layout_marginTop="17dp"
android:layout_toLeftOf="#+id/imageView1"
android:paddingBottom="15dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#685f56" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/text1"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:src="#drawable/highlight_icon" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rel_main"
android:visibility="gone" >
</RelativeLayout>
<View
android:id="#+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1"
android:background="#685f56" />
</RelativeLayout>
**2. Create Custom Adapter in "ItemListFragment.java" **
public class CustomAdepeterNewJob extends BaseAdapter{
//String[] tablecontent;
int Click=0;
Map<String, DummyItem> tablecontent = new HashMap<String, DummyItem>();
Context context;
public CustomAdepeterNewJob(Context context, Map<String, DummyItem> titles)
{
this.context = context;
this.tablecontent = titles;
}
public class ViewHolder {
public TextView txt;
public ImageView img;
public RelativeLayout relativeLayout1,rel_main;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return tablecontent.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(R.layout.c_simple_list_item, parent, false);
holder = new ViewHolder();
holder.txt = (TextView) view.findViewById(R.id.text1);
holder.img = (ImageView) view.findViewById(R.id.imageView1);
holder.rel_main=(RelativeLayout) view.findViewById(R.id.rel_main);
holder.relativeLayout1=(RelativeLayout) view.findViewById(R.id.relativeLayout1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
String s=DummyContent.ITEMS.get(position).content;
System.out.println("==dummy "+s);
holder.txt.setText(s);
if (s.equalsIgnoreCase("All"))
{
holder.img.setImageResource(R.drawable.all_icon);
}else if (s.equalsIgnoreCase("Note")) {
holder.img.setImageResource(R.drawable.notes_icon);
}else if (s.equalsIgnoreCase("Highlight")) {
holder.img.setImageResource(R.drawable.highlight_icon);
}else if (s.equalsIgnoreCase("Snapshots")) {
holder.img.setImageResource(R.drawable.snapshot_icon);
}else if (s.equalsIgnoreCase("Draw")) {
holder.img.setImageResource(R.drawable.draw_icon);
}else if (s.equalsIgnoreCase("Record Sounde")) {
holder.img.setImageResource(R.drawable.sound_recorder_icon);
}
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
if (holder.relativeLayout1.getVisibility()==View.GONE)
{
holder.rel_main.setBackgroundResource(R.drawable.list_item_bg_pressed);
holder.relativeLayout1.setVisibility(View.VISIBLE);
}else {
holder.relativeLayout1.setVisibility(View.GONE);
holder.rel_main.setBackgroundColor(Color.TRANSPARENT);
}
}
});
return view;
}
}
3. And ad Custom Adapter in "onCreate" in ItemListFregment
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: replace with a real list adapter.
/*setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
R.layout.c_simple_list_item, R.id.text1, DummyContent.ITEMS));*/
setListAdapter(new CustomAdepeterNewJob(getActivity(), DummyContent.ITEM_MAP));
}
I've got a FragmentActivity which show the correct fragment, using a ViewPager. I added a Fragment StatePagerAdapter to the ViewPager. If I push a button at a Fragment, it should be recreated (onCreateView should be called again). The fragments are articles and if the user is offline, I'll show him "please check your networkconnection" and a button to reload. The button called a method in the FragmentActivity, but I don't know how implement it. Maybe I can destroy and recreate the current Fragment?
here the important part of the FragmentActivity:
private ArrayList<Fragment> fragments;
protected void onCreate(Bundle savedInstanceState)
{
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article_page_slider);
...
pages = datahandler.getCount(...);
viewPager = (ViewPager) findViewById(R.id.articlePager);
PagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(startPosition);
fragments = new ArrayList<Fragment>(pages);
}
public void reloadData(View v)
{
// TODO
((ArticlePageFragment)fragments.get(viewPager.getCurrentItem())).refresh();
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
public ScreenSlidePagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
Fragment fragment = new ArticlePageFragment();
Bundle args = new Bundle();
...
fragment.setArguments(args);
fragments.add(position, fragment);
return fragment;
}
public int getCount()
{
return pages;
}
}
and the Fragment itself:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
activity = getActivity();
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_article_page, container, false);
CacheDataHandler cdh = new CacheDataHandler(activity);
cdh.open();
String htmlData = cdh.loadData();
rootView.findViewById(R.id.btn_reload_article_data).setVisibility(View.GONE);
webView = ((WebView) rootView.findViewById(R.id.articleFragment));
refresh();
...
return rootView;
}
public void refresh()
{
...
if (htmlData == null)
{
rootView.findViewById(R.id.btn_reload_article_data).setVisibility(View.VISIBLE);
webView.loadData(defaultdata,"text/html", "UTF-8");
}
else
{
webView.loadData(htmlData,"text/html", "UTF-8");
}
}
and the fragment_article_page.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/articleFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/btn_reload_article_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="reloadData"
android:text="#string/btn_txt_reload_data"
android:visibility="gone" />
</RelativeLayout>
EDIT
added a private ArrayList<Fragment> fragments in FragmentActivity and transfer the code for manipulating the view in an extra refreshMethod, look ahead. All works fine, if I only click on the first or second Fragment (index 0 and 1). Otherwise I get an IndexOutOfBoundsException. How to initialize and fill the ArrayList?
i am targeting a tablet 3.2 version. i am displaying a listactivity in the left side of the screen and when user clicks on a listitem the details get displayed on the right side of the screen. as i have different details layout for each item clicked i am displaying a fragment on the right side of the screen. i have a requirement to display on a tabbed layout on the detail section. i extended a fragment and in oncreateview inflated an xml with tabhost as root element. but somehow the fragment is not being inflated. for each tab i have a list view to display.below is the code section...the error i get is NullPointerException.
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class Projects extends Fragment implements OnTabChangeListener {
private int mCurrentTab;
private TabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View tablayout = inflater.inflate(R.layout.projects, null);
mTabHost = (TabHost)tablayout.findViewById(android.R.id.tabhost);
final View v1 = tablayout.findViewById(R.id.webprojects);
final View v2 = tablayout.findViewById(R.id.androidprojects);
final View v3 = tablayout.findViewById(R.id.iphoneprojects);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("WEB").setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return v1;
}
}));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("ANDROID").setContent(new TabHost.TabContentFactory() {
#Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return v2;
}
}));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("IPHONE").setContent(new TabHost.TabContentFactory() {
#Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return v3;
}
}));
return tablayout;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setRetainInstance(true);
mTabHost.setOnTabChangedListener(this);
mTabHost.setCurrentTab(mCurrentTab);
}
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
FragmentTransaction FrgTra = getFragmentManager().beginTransaction();
if(tabId.equals("WEB"))
{
if(getFragmentManager().findFragmentByTag(tabId) == null)
{
FrgTra.replace(R.id.webprojects, new ProjectsListFragment("WEB"), tabId).commit();
}
}
if(tabId.equals("ANDROID"))
{
if(getFragmentManager().findFragmentByTag(tabId) == null)
{
FrgTra.replace(R.id.androidprojects, new ProjectsListFragment("ANDROID"), tabId).commit();
}
}
if(tabId.equals("IPHONE"))
{
if(getFragmentManager().findFragmentByTag(tabId) == null)
{
FrgTra.replace(R.id.iphoneprojects, new ProjectsListFragment("IPHONE"), tabId).commit();
}
}
}
}
and the xml i am using is :
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0">
<FrameLayout android:id="#+id/webprojects"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<FrameLayout
android:id="#+id/androidprojects"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<FrameLayout
android:id="#+id/iphoneprojects"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
please help me resolve this issue....
Unsure your content is initialised correctly with the
new TabHost.TabContentFactory()
call.
I've found that populating a Tab requires an activity that might need extras to extract your information eg the cursor id. You might then need to set the text displayed on the tab and an icon or icon selector so that tab selection is more tailored to your user's experience.
Resources _res = app.getResources();
// Create an Intent to launch an Activity for the tab (to be reused)
Intent _intent = new Intent().setClass(this, WEB_TabActivity.class);
_intent.putExtra("whenNeeded", whenNeeded);
// Initialize a TabSpec for each tab and add it to the TabHost
tabHost.addTab(tabHost.newTabSpec("WEB")
.setIndicator(_res.getString(R.string.WEB_Displayed_Tab_Text),
_res.getDrawable(R.drawable.WEB_icon_selector))
.setContent(_intent));
Example selector is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use color-->
<item android:drawable="#drawable/WEB_ICON_color"
android:state_selected="true" />
<!-- When not selected, use grey -->
<item android:drawable="#drawable/WEB_ICON_Grey" />
</selector>
you then code WEB_TabActivity as any Activity class that would display your desired data.
** CURSOR ADAPTER AND VIEW HOLDER PATTERN
/**
* Adapter that allows us to match product items to layout fragment<p/>
*
* #author The Solar Sheriff
*/
public class MyItemListAdapter extends CursorAdapter {
public final static String TAG = "MyItemListAdapter";
/**
* Constructor from a list of items
*/
private LayoutInflater _li;
private Context _context;
private Cursor _in;
private int _productName,
_count;
public OrderItemListAdapter(Context _Context, Cursor _In) {
super(_Context, _In, true);
_context = _Context;
_li = LayoutInflater.from(_context);
_in = _In;
_productName = _in.getColumnIndex("product_name");
_count = _in.getCount();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public int getCount() {
return _in.getCount();
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View _view = _li.inflate(R.layout.list_item_layout, null);
return _view;
}
private ViewHolder mapItemViewHolder(View _v){
ViewHolder _itemHolder = new ViewHolder();
//Map the views to the ViewHolder
_itemHolder.productName = (TextView) _v.findViewById(R.id.itemProductName);
return _itemHolder;
}
#Override
public void bindView(View _cView, final Context _context, final Cursor _c) {
ViewHolder _itemHolder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (_cView == null) {
_cView = _li.inflate(R.layout.list_item_layout, null);
_itemHolder = mapItemViewHolder(_cView);
_cView.setTag(_itemHolder);
}
else {
// Get the ViewHolder back to get fast access
// Can also get a view with no tag so check for null
if ((_itemHolder = (ViewHolder) _cView.getTag()) == null) {
_itemHolder = mapItemViewHolder(_cView);
_cView.setTag(_itemHolder);
}
}
//*** TEXT VIEWS ***
// Bind the data using the holder.
_itemHolder.productName.setText(_c.getString(_productName));
for(String _display : TabLayouts ) {
if ( _display.equals("WEB") ) _itemHolder.productName.setTag(Integer.toString(R.layout.WEB_layout));
if ( _display.equals("IPHONE") ) _itemHolder.productName.setTag(Integer.toString(R.layout.IPHONE_layout));
if ( _display.equals("ANDROID") ) _itemHolder.productName.setTag(Integer.toString(R.layout.ANDROID_layout));
}
}
static String TabLayouts = { "WEB","IPHONE","ANDROID"};
static class ViewHolder
{
TextView productName;
}
}
// YOUR onSelectedItem LISTENER DOES
MyList.setOnItemSelectedListener( new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> _item, View _View, int _selection, long _id) {
ViewGroup _parent = (ViewGroup)_item.getParent();
TextView _productName = (TextView)_parent.findViewById(R.id.itemProductName);
int _showLayout = Integer.valueOf(_productName.getTag());
//*** e.g. Inflate your fragment layout ***
}
});
Hope this gives you a successful path to follow :)