Dynamically adding table rows to my TableLayout fragment



I am creating an application which consists of a (undetermined, typically 5-10) number of lists which are switched between using tabs, each list is composed of a number of items, so like a restaurant menu essentially.


So far I have scrollable tabs working to switch between these lists, which consist of the same fragment which is just a table layout. I want to dynamically add an undetermined number of items to each list, each item is different but will use the same fragment/layout (consists of a few text views and buttons). However I have no idea where to begin to create a new fragment for each item, I don't know where to put the code, or indeed how to code it.


Here are my classes so far:


MainMenu.java



package com.example.jp.tab;

public class MainMenu extends FragmentActivity {

ViewPager viewPager = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));

}
}

class MyAdapter extends FragmentStatePagerAdapter {

public MyAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
Fragment fragment = null;

if (position == 0) {
fragment = new ListFragment();
} else if (position == 1) {
fragment = new ListFragment();
} else if (position == 2) {
fragment = new ListFragment();
}
return fragment;
}

@Override
public int getCount() {
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
if (position==0) {
return "Tab 1";
} else if (position==1) {
return "Tab 2";
} else if (position==2) {
return "Tab 3";
}
return null;
}
}


activity_main_menu.xml



<android.support.v4.view.ViewPager xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main_menu">
<android.support.v4.view.PagerTitleStrip
android:textColor="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:background="#000000"
android:layout_gravity="top"
android:paddingTop="4dp"
android:paddingBottom="4dp">
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>


ListFragment.java


This is the java file for the fragment which is used for each tab, where I believe the code to add the list items dynamically should go.



package com.example.jp.tab;

/**
* Created by Jp on 04/08/2014.
*/
public class ListFragment extends Fragment {

private int numberOfItems = 3;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.item_table, container, false);
}
}


item_table.xml


This xml/fragment file is where I want to dynamically add each item, it's a table layout and I want to add each item as a new row. The tab on the main menu will be used to switch between lists which will all use this same layout, however they will have different items.



<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello hello hello hello hello hello"
android:id="@+id/textView"
android:layout_column="0" />

</TableRow>
</TableLayout>


item.xml


This file is a table row object with a few more components inside, basically I want to create a few of these item TableRows and add them dynamically to the item_table list shown above. I am thinking these items should be added in the ListFragment.java file, however as this is generated dynamically I have no idea where to put the code in this file.


Please let me know if anymore code or explanation is needed. Thanks in advance!


No comments:

Post a Comment