Java Android - Dynamic load fragment (ViewPager) - java

In settngs existed checkbox, if it is checked, then a specific fragment must not be loaded. I have just 4 fragment and I use FragmentStatePagerAdapter to show them.
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Fragment_One();
case 1:
return new Fragment_Two();
case 2:
return new Fragment_Three();
case 3:
return new Fragment_Four();
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
As show fragments, which are not only check in the settings? I get value (true of false (Check or Uncheck) fragment, but how do not show this fragment, i dont know.

You have to adapt your getItem() method as well as the getCount() method.
Let's assume you have a method shouldShowFragment(int fragmentNumber) that tells me for a given fragment number from 0 to 3, whether it should be shown or not (depending on the settings).
Now, implement getCount() like so to return the number of fragments that should be shown:
public int getCount() {
int cnt = 0;
for (int i = 0; i < 4; i++) {
if (shouldShowFragment(i)) cnt++;
}
return cnt;
}
And implement getItem() like so to take the not showing fragments into account:
public Fragment getItem(int position) {
int cnt = -1;
for (int i = 0; i < 4; i++) {
if (shouldShowFragment(i)) cnt++;
if (cnt == position) {
switch(i) {
case 0 : return new Fragment_One();
case 1 : return new Fragment_Two();
case 2 : return new Fragment_Three();
case 3 : return new Fragment_Four();
}
}
}
return null;
}

First of all Save the all check buttons state globally(i.e. in shared preferences like btn1.setChecked == true/false whatever) and in above code do like below:-
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
if(btn0.isChecked == true)
return new Fragment_One();
case 1:
if(btn1.isChecked == true)
return new Fragment_Two();
case 2:
if(btn2.isChecked == true)
return new Fragment_Three();
case 3:
if(btn3.isChecked == true)
return new Fragment_Four();
}
return null;
}

Related

A strange number appears to me in FragmentPagerAdapter when I call a translation R.string

I'm trying to put a text translation into a FragmentPagerAdapter
With all attempts and use
return (R.string.Chats) + "";
A strange number appeared to me in three tab ,
photo : https://i.stack.imgur.com/znIB2.png
public class SectionPagerAdapter extends FragmentPagerAdapter {
private Context context;
public SectionPagerAdapter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int position) {
if(position==0){
ChatsFragment chatsFragment =new ChatsFragment();
return chatsFragment;
}
else if(position==1){
FriendsFragment firendsFragment =new FriendsFragment();
return firendsFragment;
}
else if(position==2){
RequestsFragment requestsFragment =new RequestsFragment();
return requestsFragment;
}
else return null;
}
#Override
public int getCount() {
return 3;
}
public CharSequence getPageTitle(int position){
if(position==0){
return (R.string.Chats) + "";
}
else if(position==1){
return (R.string.Friends) + "";
}
else if(position==2){
return (R.string.Requests) + "";
}
else return null;
}
}
The problem in particular here is that I want to, in short, add the text through R.string !
public CharSequence getPageTitle(int position){
if(position==0){
return (R.string.Chats) + "";
}
else if(position==1){
return (R.string.Friends) + "";
}
else if(position==2){
return (R.string.Requests) + "";
}
else return null;
}
R.string.something return the String resource id that string have. You can pass the context to constructor of view and initialize the property context that you have in your adapter class.
Then you can get strings like this:
context.getString(R.string.something)
R.string.Chats - it's not a String it's resource identifier(has an integer type). If you want to get String from resources you should use:
context.getString(R.string.Chats) instead of R.string.Chats. The same for another page titles.
just re-write you getPageTitle method and it will be ok

TabLayout fragments are instantiated as the same when debugger is not connected

I have a TabLayout that uses a custom adaptor (TabPagerAdapter.java), it's initialised with this function (I previously had the problem that things didn't update when replaced so it removes everything before initializing):
public boolean setPagerViewContents(int mode, ArrayList<String> titles) {
try {
mTabLayout.removeAllTabs();
mViewPager.setAdapter(null);
mViewPager.removeAllViews();
for (int i = 0; i < titles.size(); i++) {
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
}
mAdapter = new TabPagerAdapter(getSupportFragmentManager(), titles.size(), mode);
mViewPager.setAdapter(mAdapter);
mViewPager.invalidate();
return true;
} catch (Exception e) {
mErrorReporter.reportError(e);
return false;
}
}
Custom adapter (TabPageAdapter.java):
public class TabPagerAdapter extends FragmentStatePagerAdapter {
int mTabCount;
int mLayoutType;
public TabPagerAdapter(FragmentManager fm, int numberOfTabs, int layoutType) {
super(fm);
this.mTabCount = numberOfTabs;
this.mLayoutType = layoutType;
}
#Override
public Fragment getItem(int position) {
switch (mLayoutType) {
case 0:
switch (position) {
case 0:
return new Fragment15();
case 1:
return new Fragment1();
default:
return null;
}
case 1:
switch (position) {
case 0:
return new Fragment3();
case 1:
return new Fragment2();
default:
return null;
}
default:
return null;
}
}
#Override
public int getCount() {
return mTabCount;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
When the app starts the function setPagerViewContents (with the right mode and titles) is called, after that mViewPager.setCurrentItem(number) is called to display the right tab. But now the fragment displayed in mViewPager is correct but when clicking on a tab title the fragment is the same displayed at start (the one with the index number, not the one that should have been displayed), when tapping the first (number) tab again and then again to some other tab (not number) the shown tab is correct.
The most annoying thing here is that it's NOT consistent, it sometimes happens, sometimes doesn't and it doesn't happen when the debugger is attached so I can't debug it properly. If some other code is needed please do tell, I'll update this post as quick as possible because I'd really love to see this solved, for my own sanity and for the happiness of my few users.
Change your following method:
public boolean setPagerViewContents(int mode, ArrayList<String> titles) {
try {
mAdapter = new TabPagerAdapter(getSupportFragmentManager(), titles.size(), mode);
mViewPager.setAdapter(mAdapter);
mTabLayout.setupWithViewPager(mViewPager);
return true;
} catch (Exception e) {
mErrorReporter.reportError(e);
return false;
}
}
and set text on tabs in TabPagerAdapter by overriding following method:
#Override
public CharSequence getPageTitle(int position) {
return context.getString(tabTitles[position]);
}

Master-Detail View using ViewPager

I'd like to change the master-detail implementation of my Android phone app. Currently, users can select items from a ListView, opening a new activity. To select a different activity, the user must return to the list. Instead of this pogo-sticking, I'd like the user to swipe left and right to page through the documents using a ViewPager. There can be many documents, so I'd like to load at most 3 pages at a time - the current page, the previous, and the next. Paging back and forth should then add and remove pages left and right. I've created an adapter implementing FragmentStatePagerAdapter that handles static content (e.g. TextViews) nicely. Also deleting pages seems to work OK (not included here). But when I add e.g. an EditText content is copied over from one page to the next when paging.
Below is the code for the adapter and for the activity. There are two questions I have:
What is wrong with my adapter that causes the undesired copying of EditText from one fragment to the next?
This is my first shot at this, and it's probably far from an optimal implementation. But I find this to be such a common use case that I almost feel like there would be a ready made framework for it. Could this be achieved much easier?
Pager Adapter:
public class DetailPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragments;
private final static String TAG = "DetailPagerAdapter";
public DetailPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
mFragments = fragments;
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
public void addItem(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
public void removeItem(int position) {
mFragments.remove(position);
notifyDataSetChanged();
}
public void insertItem(int position, Fragment fragment) {
mFragments.add(position, fragment);
notifyDataSetChanged();
}
}
PagingActivity Base Class:
public abstract class PagingActivity
extends AppCompatActivity
implements ViewPager.OnPageChangeListener {
protected ViewPager mViewPager;
DetailPagerAdapter mViewPagerAdapter;
protected ArrayList<String> mAllItemIds;
private String mPreviousItemId;
private String mCurrentItemId;
private String mNextItemId;
private boolean mMuteOnPageSelected = false;
protected abstract Fragment getNewPageFragment(String id);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Fragment> initialFragments = new ArrayList<>();
int currentItemIndex = mAllItemIds.indexOf(mCurrentItemId);
int pageSelection = 1;
// Add previous view.
if (currentItemIndex > 0) {
mPreviousItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) - 1);
initialFragments.add(getNewPageFragment(mPreviousItemId));
} else {
pageSelection = 0;
mPreviousItemId = null;
}
// Add current view.
initialFragments.add(getNewPageFragment(mCurrentItemId));
// Add next view.
if (currentItemIndex < mAllItemIds.size() - 1) {
mNextItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) + 1);
initialFragments.add(getNewPageFragment(mNextItemId));
} else {
mNextItemId = null;
}
mViewPagerAdapter = new DetailPagerAdapter(getSupportFragmentManager(), initialFragments);
mViewPager.setAdapter(mViewPagerAdapter);
mViewPager.setCurrentItem(pageSelection);
mViewPager.addOnPageChangeListener(this);
}
#Override
public void onPageSelected(int position) {
if (!mMuteOnPageSelected) {
mCurrentItemId = ((PagingFragment) (mViewPagerAdapter.getItem(mViewPager.getCurrentItem()))).getItemId();
int currentItemIndex = mAllItemIds.indexOf(mCurrentItemId);
// Navigated to the right.
if (position == mViewPagerAdapter.getCount() - 1) {
// Add next if not already pointing at the last available item.
if (currentItemIndex < mAllItemIds.size() - 1) {
mNextItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) + 1);
mViewPagerAdapter.addItem(getNewPageFragment(mNextItemId));
} else {
mNextItemId = null;
}
// If it succeeds remove first item.
int itemCount = mViewPagerAdapter.getCount();
if ((itemCount > 3) || ((itemCount == 3) && (currentItemIndex == mAllItemIds.size() - 1))) {
mMuteOnPageSelected = true;
mViewPagerAdapter.removeItem(0);
mViewPager.setCurrentItem(1);
mMuteOnPageSelected = false;
}
}
// Navigated to the left.
else if (position == 0) {
// Add item on the left if not already pointing at the first available item.
if (currentItemIndex > 0) {
mPreviousItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) - 1);
mViewPagerAdapter.insertItem(0, getNewPageFragment(mPreviousItemId));
} else {
mPreviousItemId = null;
}
// Check if last item needs to be removed and selection updated.
int itemCount = mViewPagerAdapter.getCount();
if (itemCount == 3) {
if (currentItemIndex == 0) {
// Points to the first of two items.
// -> do not change selection
// -> remove rightmost item.
mViewPagerAdapter.removeItem(itemCount - 1);
} else if (currentItemIndex == mAllItemIds.size() - 2) {
// Will point to the middle of 3 items.
// -> nothing to remove
// -> select middle page.
mMuteOnPageSelected = true;
mViewPager.setCurrentItem(1);
mMuteOnPageSelected = false;
}
} else if (itemCount > 3) {
// Pager contains 4 items, first item selected.
// -> remove rightmost item
// -> select middle page.
mMuteOnPageSelected = true;
mViewPagerAdapter.removeItem(itemCount - 1);
mViewPager.setCurrentItem(1);
mMuteOnPageSelected = false;
}
}
mViewPagerAdapter.notifyDataSetChanged();
}
}
}
The second question was the key: Yes, at least the current state can be achieved much easier by letting the adapter handle the full array of items. FragmentStatePagerAdapter only loads as many fragments at a time as needed, so it can handle all the manual work I had done in the activity.
Pager Adapter
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private List<String> mAllItemIds;
public MyPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mAllItemIds = ...
}
#Override
public int getCount() {
return mAllItemIds.size();
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
return MyFragment.newInstance(mAllItemIds.get(position));
}
public void removeItem(int position) {
// add needed code here to remove item also from source
// ...
mAllItemIds.remove(position);
notifyDataSetChanged();
}
}
Activity
public abstract class PagingActivity extends AppCompatActivity {
protected ViewPager mViewPager;
MyPagerAdapter mViewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = (ViewPager)findViewById(R.id.viewPager);
mViewPagerAdapter = new MyPagerAdapter(this, getSupportFragmentManager());
mViewPager.setAdapter(mViewPagerAdapter);
}
private void deleteItem() {
mViewPagerAdapter.removeItem(mViewPager.getCurrentItem());
}
}

How to remove the fragment from its calling fragment?

How to remove the fragment from his calling fragment ?
i have search this thing but i didn't get solution according to my requirement. In my application, there is one FragmentActivity which has the viewPager.
This viewPager contains 3 Fragments. For fragment I am using FragmentStatePagerAdapter.
Suppose there is 3 fragment: A, B, C; and D fragment C also contain a child fragment E.
I have call fragment E in the onCreateView() method and in the onDestroy() method of fragment C, I have remove the child fragment E.
So what is happening when I slid viewPager from C to B, and B to A - when I come back from A to B, now current displaying fragment is B. Now if I slid fragment C should be display, but in place of fragment C the child fragment of C, fragment E is displaying after then fragment E then fragment C is displaying now in this condition i have not seen Fragment E over the fragment C, the child fragment E is not interacting with viewpager but why it is added in viewPager,
i have try to destroy the child fragment E in onPause() and in onDestroy() method of C, but nothing is happening. Please any one help me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MYDataManager.sharedHandler().changeLanguageConfiguration();
overridePendingTransition(R.anim.activity_open_translate, R.anim.activity_close_scale);
setContentView(R.layout.activity_myscanner);
appFlow = new ArrayList(Arrays.asList(MYConstant.kProfileMenuSettings, MYConstant.kScanner, MYConstant.kRestaurantListing));
viewpager = (ViewPager) findViewById(R.id.view_pager);
viewpager.addOnPageChangeListener(this);
viewpager.setOffscreenPageLimit(0);
reloadViewPager(1);
}
public void reloadViewPager(int currentItem) {
adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
#Override
public Fragment getItem(int position) {
return getFragmentWithPosition(position);
}
#Override
public int getCount() {
return appFlow.size();
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
};
viewpager.setAdapter(adapter);
viewpager.setCurrentItem(currentItem);
}
private MYBaseFragment getFragmentWithPosition(int position) {
String screen = appFlow.get(position);
MYBaseFragment fragment = null;
if(screen.equals(MYConstant.kProfileMenuSettings)) {
myProfileSettingFragment = new MYProfileSettingFragment();
fragment = myProfileSettingFragment;
} else if(screen.equals(MYConstant.kScanner)) {
fragment = new MYScannerParentFragment();
} else if(screen.equals(MYConstant.kRestaurantListing)) {
myRestaurantListFragment = new MYRestaurantListFragment();
fragment = myRestaurantListFragment;
} else if(screen.equals(MYConstant.kRestaurantDetails)) {
myResraurantDetailsFragment = new MYResraurantDetailsFragment();
fragment = myResraurantDetailsFragment;
} else if(screen.equals(MYConstant.kCurrentRestaurantDetails)) {
MYQRCode qrData = MYDataManager.sharedHandler().getQRData();
if(MYUitilities.checkQRValidation(qrData)) {
fragment = new MYCurResDetails();
}
} else if(screen.equals(MYConstant.kSettings)) {
fragment = new MYSettingFragment();
} else if(screen.equals(MYConstant.kViewMenu)) {
fragment = new MYResCatListFragment();
} else if(screen.equals(MYConstant.kMenuCategoryListing)) {
fragment = new MYResCatListFragment();
} else if(screen.equals(MYConstant.kViewMenuViewPage)) {
// myResMenuViewPagerFragment = new MYResMenuViewPagerFragment();
// fragment = myResMenuViewPagerFragment;
fragment = new MYResMenuViewPagerFragment();
} else if(screen.equals(MYConstant.kReviewOrder)) {
fragment = new MYReviewOrderFragment();
} else if(screen.equals(MYConstant.kYourOrder)) {
fragment = new MYYourOrderFragment();
} else if(screen.equals(MYConstant.kSettings)) {
fragment = new MYSettingFragment();
} else if(screen.equals(MYConstant.kOrderHistory)) {
fragment = new MYOrderHistoryFragment();
} else if(screen.equals(MYConstant.kCheckout)) {
fragment = new MYCheckoutFragment();
} else if(screen.equals(MYConstant.kCallWaiter)) {
fragment = new MYCallWaiterFragment();
} else if(screen.equals(MYConstant.kYourProfile)) {
myYourProfileFragment = new MYYourProfileFragment();
fragment = myYourProfileFragment;
}
fragment.setFragmentInteractionListener(MYScannerActivity.this);
return fragment;
}
#Override
public void onPageSelected(final int position) {
if(currentPosition >= 0) {
final String previousScreen = appFlow.get(currentPosition);
final String currentScreen = appFlow.get(position);
System.out.println("previousScreen-" + previousScreen);
System.out.println("currentScreen-" + currentScreen);
if(previousScreen.equals(MYConstant.kYourProfile) && currentScreen.equals(MYConstant.kProfileMenuSettings)) {
appFlow.remove(MYConstant.kYourProfile);
adapter.notifyDataSetChanged();
viewpager.setCurrentItem(0);
}
if(previousScreen.equals(MYConstant.kRestaurantDetails) && currentScreen.equals(MYConstant.kRestaurantListing)) {
appFlow.remove(MYConstant.kRestaurantDetails);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kViewMenu) && currentScreen.equals(MYConstant.kRestaurantDetails)) {
appFlow.remove(MYConstant.kViewMenu);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kViewMenuViewPage) && currentScreen.equals(MYConstant.kViewMenu)) {
appFlow.remove(MYConstant.kViewMenuViewPage);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kViewMenuViewPage) && currentScreen.equals(MYConstant.kMenuCategoryListing)) {
appFlow.remove(MYConstant.kViewMenuViewPage);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kYourOrder) && currentScreen.equals(MYConstant.kViewMenuViewPage)) {
appFlow.remove(MYConstant.kYourOrder);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kReviewOrder) && currentScreen.equals(MYConstant.kViewMenuViewPage)) {
appFlow.remove(MYConstant.kReviewOrder);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kYourOrder) && currentScreen.equals(MYConstant.kReviewOrder)) {
appFlow.remove(MYConstant.kYourOrder);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kYourOrder) && currentScreen.equals(MYConstant.kViewMenuViewPage)) {
appFlow.remove(MYConstant.kReviewOrder);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kSettings) && currentScreen.equals(MYConstant.kProfileMenuSettings)) {
appFlow.remove(MYConstant.kSettings);
adapter.notifyDataSetChanged();
viewpager.setCurrentItem(0);
} else if(previousScreen.equals(MYConstant.kOrderHistory) && currentScreen.equals(MYConstant.kProfileMenuSettings)) {
appFlow.remove(MYConstant.kOrderHistory);
adapter.notifyDataSetChanged();
viewpager.setCurrentItem(0);
} else if(previousScreen.equals(MYConstant.kCheckout) && currentScreen.equals(MYConstant.kViewMenuViewPage)) {
appFlow.remove(MYConstant.kCheckout);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kCheckout) && currentScreen.equals(MYConstant.kReviewOrder)) {
appFlow.remove(MYConstant.kCheckout);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kCheckout) && currentScreen.equals(MYConstant.kYourOrder)) {
appFlow.remove(MYConstant.kCheckout);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kCallWaiter) && currentScreen.equals(MYConstant.kCheckout)) {
appFlow.remove(MYConstant.kCallWaiter);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kRestaurantListing) && currentScreen.equals(MYConstant.kScanner)) {
if(MYDataManager.sharedHandler().isQRCodeScanned()) {
appFlow.remove(MYConstant.kRestaurantListing);
if(!appFlow.contains(MYConstant.kCurrentRestaurantDetails)) {
appFlow.add(MYConstant.kCurrentRestaurantDetails);
}
adapter.notifyDataSetChanged();
}
} else if(previousScreen.equals(MYConstant.kCurrentRestaurantDetails) && currentScreen.equals(MYConstant.kScanner)) {
} else if(previousScreen.equals(MYConstant.kMenuCategoryListing) && currentScreen.equals(MYConstant.kCurrentRestaurantDetails)) {
appFlow.remove(MYConstant.kMenuCategoryListing);
adapter.notifyDataSetChanged();
}
}
Fragment fragment = ((FragmentStatePagerAdapter) viewpager.getAdapter()).getItem(position);
if(fragment instanceof MYProfileSettingFragment) {
}
currentPosition = position;
}
You Please try these.
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment currentFragment = fragmentManager.findFragmentById(R.id.frame_container);
fragmentTransaction.remove(currentFragment);//remove current fragment
So what is happening when I slid viewPager from C to B, and B to A - when I come back from A to B, now current displaying fragment is B.
I had faced something before like you have now. I was registering and unregistereing a listener on fragments life cycle events. But in viewpager, even i switched to another fragment, the previously fragment never be destroyed, you can check
Before dive into your case, i wanna mention about 'off page limit'. So you cant set to zero. Check this link. So when you set 0, it defaults to 1
viewpager.setOffscreenPageLimit(0);
If im not missing something, in this case, when you switch from C to B
If you had visited D fragment before C, D will be destroyed, B will be created, C still will be living.
Assuming you coming from first case (C to B, and then B to A)
C will be destroyed, B lives, A will be created
Bottom line, with ViewPager you have min two fragments. One is you are currently interacting and second (screen off)

checkbox in table with tableviewer in eclipse rcp

I'd like to add a checkbox to a column. I am using a tableViewer. The user shouldn't be able to edit the checkbox.
Google wasn't helpful so far, so I came here.
my labelprovider looks like this:
this.tableViewer2 = new TableViewer(table1);
this.tableViewer2.setContentProvider(new ArrayContentProvider());
this.tableViewer2.setLabelProvider(new ITableLabelProvider() {
#Override
public String getColumnText(Object element, int columnIndex) {
Platz p = (Platz) element;
switch (columnIndex) {
case 0:
return p.getReihe().getReihenfolge().toString();
case 1:
return p.getNummer().toString();
case 2:
return p.getKategorie().getPreisstd().toString();
}
return null;
}
});
I'd like to add a fourth column with a checkbox but I don't know how.
Thanks in advance!
ITableLableProvider has a method getColumnImage().
Just override it and return an image of the checkbox.
this.tableViewer2 = new TableViewer(table1);
this.tableViewer2.setContentProvider(new ArrayContentProvider());
this.tableViewer2.setLabelProvider(new ITableLabelProvider() {
#Override
public Image getColumnImage(Object element, int columnIndex) {
//do magic here and return an image :)
}
#Override
public String getColumnText(Object element, int columnIndex) {
Platz p = (Platz) element;
switch (columnIndex) {
case 0:
return p.getReihe().getReihenfolge().toString();
case 1:
return p.getNummer().toString();
case 2:
return p.getKategorie().getPreisstd().toString();
}
return null;
}
});

Categories