ISchwarz23 / SortableTableView

An Android library containing a simple TableView and an advanced SortableTableView providing a lot of customisation possibilities to fit all needs.
Apache License 2.0
1.05k stars 237 forks source link

How to use table view in vertical nested scroll view with horizontal scroll view #180

Closed rajam1215 closed 5 years ago

rajam1215 commented 5 years ago

Hello

@ISchwarz23 i need to show the total of all item list in table view with different layout, it is possible with nested scroll view, can you please help how to do with nested scroll view. please give some solution

NestedScollView LinearLayout HorizontalScrollView SortableTableView SortableTableView TextView HorizontalScrollView LinearLayout NestedScollView

Thanks

ISchwarz23 commented 5 years ago

Hi @rajam1215,

to put the TableView inside a horizontal scroll view have a look at issue #14. In case you want to have the total column always on bottom of a SortableTableView have a look at issue #161. To have a different layout for the cells in the total column simply check inside your TableDataAdapter if the current row is the total row and return the layout/view you want.

Best regards, Ingo

rajam1215 commented 5 years ago
public class TotalAwareComparator implements Comparator<YourObject> {

    private final SortableTableView tableView;

    public TotalAwareComparator(SortableTableView tableView) {
        this.tableView = tableView;
    }

    public int compare(YourObject obj1, YourObject obj2) {
        if(obj1.isTotal()) {
            return tableView.getSortState().getSortedOrder() == SortingOrder.ASCENDING ? 1 : -1;
        } else if(obj2.isTotal()) {
            return tableView.getSortState().getSortedOrder() == SortingOrder.ASCENDING ? -1 : 1;
        } else {
            // do normal sorting
        }
    }

}

can you tell me .isTotal method need to declare in model (object class).

i add all value in sortable table view with this way

double stkTotal = 0.0, resTotal = 0.0, netTotal = 0.0;
                                    // parse json data
                                    JSONArray json = new JSONArray(result);
                                    for (int i = 0; i < json.length(); i++) {
                                        JSONObject jsonObject = json.getJSONObject(i);
                                        String matnrt = jsonObject.getString("matnrt");
                                        String modelt = jsonObject.getString("modelt");
                                        String uom = jsonObject.getString("uom");
                                        double stkqt = jsonObject.getDouble("stkqt");
                                        double resqt;
                                        if (jsonObject.has("resqt")) {
                                            resqt = jsonObject.getDouble("resqt");
                                        } else {
                                            resqt = 0.00;
                                        }
                                        double netqt;
                                        if (jsonObject.has("netqt")) {
                                            netqt = jsonObject.getDouble("netqt");
                                        } else {
                                            netqt = 0.00;
                                        }
                                        stockList.add(new Stock(matnrt, modelt, uom, stkqt, resqt, netqt));

                                        stkTotal += stkqt;
                                        resTotal += resqt;
                                        netTotal += netqt;
                                    }
                                    stockList.add(new Stock("Total", "", "", stkTotal, resTotal, netTotal));
                                    mAdapter.notifyDataSetChanged();
                                }

i make total with own and show in last row.

ISchwarz23 commented 5 years ago

Hi rajam1215,

you are free to do so. The Comparator you mentioned was only an example. Instead of the obj.isTotal() you can also do something like obj.matnrt.equals("Total").

ISchwarz23 commented 5 years ago

Yes, so you have now multiple possibilities: 1) add the described logic (TotalAwareComparator) to all your comparators. 2) create a base Comparator that contains the described logic and uses a injected Comparator for the column specific comparison. 3) use the pro version, as it has a AlwaysOnBottonComparator that can be used like new AlwaysOnBottomComparator(tableView, sale -> sale.getMatNrt().equals("Total"), yourColumnComparator) 4) you boy support so I can create this AlwaysOnBottom pro feature for the free TableView especially for you

rajam1215 commented 5 years ago

private static class SaleNameComparator implements Comparator {

    private final SortableSaleTableView tableView;

    private SaleNameComparator(SortableSaleTableView tableView) {
        this.tableView = tableView;
    }

    @Override
    public int compare(final SaleOne car1, final SaleOne car2) {
        if (car1.getName().equals("Total")) {
            return tableView.getSortingStatus().getSortedOrder() == SortingOrder.ASCENDING ? 1 : -1;
        } else if (car2.getName().equals("Total")) {
            return tableView.getSortingStatus().getSortedOrder() == SortingOrder.ASCENDING ? -1 : 1;
        } else {
            return car1.getName().compareTo(car2.getName());
        }
    }
}

but it gives error

2019-05-20 11:29:59.630 3272-3272/com.royalways.skbikes W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'de.codecrafters.tableview.SortingStatus com.royalways.skbikes.utils.SortableSaleTableView.getSortingStatus()' on a null object reference 2019-05-20 11:29:59.635 3272-3272/com.royalways.skbikes W/System.err: at com.royalways.skbikes.model.Comparators$SaleInvvlComparator.compare(Comparators.java:273) 2019-05-20 11:29:59.636 3272-3272/com.royalways.skbikes W/System.err: at com.royalways.skbikes.model.Comparators$SaleInvvlComparator.compare(Comparators.java:262) 2019-05-20 11:29:59.636 3272-3272/com.royalways.skbikes W/System.err: at java.util.TimSort.countRunAndMakeAscending(TimSort.java:352)

ISchwarz23 commented 5 years ago

Your tableView is null...

ISchwarz23 commented 5 years ago

public static SortableTableView tableView; It is null.

The getSaleNameComparator() needs to get the TableView passed: getSaleNameComparator(tableView).

rajam1215 commented 5 years ago

'de.codecrafters.tableview.SortingStatus de.codecrafters.tableview.SortableTableView.getSortingStatus()

sorting status is null

ISchwarz23 commented 5 years ago

It would be helpful to see the error.

rajam1215 commented 5 years ago

Same error it gives

2019-05-20 11:29:59.630 3272-3272/com.royalways.skbikes W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'de.codecrafters.tableview.SortingStatus

if i have solution on signal column i will do on my all column , please look on. i will show you my code also. you can check. for both sotable table view class and comprator

ISchwarz23 commented 5 years ago

Seems like you have cut off the exception message. It says which method was invoked but not on which instance.

rajam1215 commented 5 years ago

this is full error that show logcat.

2019-05-20 13:03:58.409 8252-8252/com.royalways.skbikes W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'de.codecrafters.tableview.SortingStatus com.royalways.skbikes.utils.SortableSaleTableView.getSortingStatus()' on a null object reference 2019-05-20 13:03:58.410 8252-8252/com.royalways.skbikes W/System.err: at com.royalways.skbikes.model.Comparators$SaleNameComparator.compare(Comparators.java:78) 2019-05-20 13:03:58.410 8252-8252/com.royalways.skbikes W/System.err: at com.royalways.skbikes.model.Comparators$SaleNameComparator.compare(Comparators.java:67) 2019-05-20 13:03:58.410 8252-8252/com.royalways.skbikes W/System.err: at java.util.TimSort.countRunAndMakeAscending(TimSort.java:356) 2019-05-20 13:03:58.410 8252-8252/com.royalways.skbikes W/System.err: at java.util.TimSort.sort(TimSort.java:216) 2019-05-20 13:03:58.410 8252-8252/com.royalways.skbikes W/System.err: at java.util.Arrays.sort(Arrays.java:1523) 2019-05-20 13:03:58.410 8252-8252/com.royalways.skbikes W/System.err: at java.util.Collections.sort(Collections.java:238) 2019-05-20 13:03:58.410 8252-8252/com.royalways.skbikes W/System.err: at de.codecrafters.tableview.SortableTableView$SortingController.sortDataSFCT(SortableTableView.java:305) 2019-05-20 13:03:58.410 8252-8252/com.royalways.skbikes W/System.err: at de.codecrafters.tableview.SortableTableView$SortingController.onHeaderClicked(SortableTableView.java:251) 2019-05-20 13:03:58.410 8252-8252/com.royalways.skbikes W/System.err: at de.codecrafters.tableview.InternalHeaderClickListener.informHeaderListeners(InternalHeaderClickListener.java:33) 2019-05-20 13:03:58.410 8252-8252/com.royalways.skbikes W/System.err: at de.codecrafters.tableview.InternalHeaderClickListener.onClick(InternalHeaderClickListener.java:27) 2019-05-20 13:03:58.411 8252-8252/com.royalways.skbikes W/System.err: at android.view.View.performClick(View.java:5675) 2019-05-20 13:03:58.411 8252-8252/com.royalways.skbikes W/System.err: at android.view.View$PerformClick.run(View.java:22646) 2019-05-20 13:03:58.411 8252-8252/com.royalways.skbikes W/System.err: at android.os.Handler.handleCallback(Handler.java:836) 2019-05-20 13:03:58.411 8252-8252/com.royalways.skbikes W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103) 2019-05-20 13:03:58.411 8252-8252/com.royalways.skbikes W/System.err: at android.os.Looper.loop(Looper.java:203) 2019-05-20 13:03:58.411 8252-8252/com.royalways.skbikes W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6251) 2019-05-20 13:03:58.411 8252-8252/com.royalways.skbikes W/System.err: at java.lang.reflect.Method.invoke(Native Method) 2019-05-20 13:03:58.411 8252-8252/com.royalways.skbikes W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075) 2019-05-20 13:03:58.411 8252-8252/com.royalways.skbikes W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

this code i use for only name column.

public static SortableSaleTableView tableView;

/* comparators for SaleOne objects class data */
public static Comparator<SaleOne> getSaleNameComparator() {
    return new SaleNameComparator(tableView);
}

private static class SaleNameComparator implements Comparator {

    private final SortableSaleTableView tableView;

    private SaleNameComparator(SortableSaleTableView tableView) {
        this.tableView = tableView;
    }

    @Override
    public int compare(final SaleOne car1, final SaleOne car2) {
        if (car1.getName().equals("Total")) {
            return tableView.getSortingStatus().getSortedOrder() == SortingOrder.ASCENDING ? 1 : -1;
        } else if (car2.getName().equals("Total")) {
            return tableView.getSortingStatus().getSortedOrder() == SortingOrder.ASCENDING ? -1 : 1;
        } else {
            return car1.getName().compareTo(car2.getName());
        }
    }
}
ISchwarz23 commented 5 years ago

public static SortableTableView tableView; It is null.

The getSaleNameComparator() needs to get the TableView passed: getSaleNameComparator(tableView).

Please apply my suggestion.

ISchwarz23 commented 5 years ago
public final class Comparators {

    private Comparators() {

    }

    public static Comparator<SaleOne> getSaleNameComparator(SortableTableView tableView) {
        return new SaleNameComparator(tableView);
    )

    // ...

}

And inside the SaleNameComparator you use the logic from above.

rajam1215 commented 5 years ago

i use this code that according to you

/ comparators for SaleOne objects class data / public static Comparator getSaleNameComparator(SortableSaleTableView tableView) { return new SaleNameComparator(tableView); }

// Comparator methods for SaleOne private static class SaleNameComparator implements Comparator {

    private SortableSaleTableView tableView;

    private SaleNameComparator(SortableSaleTableView tableView) {
        this.tableView = tableView;
    }

    @Override
    public int compare(final SaleOne car1, final SaleOne car2) {
        if(car1.getName().equals("Total")) {
            return tableView.getSortingStatus().getSortedOrder() == SortingOrder.ASCENDING ? 1 : -1;
        } else if(car2.getName().equals("Total")) {
            return tableView.getSortingStatus().getSortedOrder() == SortingOrder.ASCENDING ? -1 : 1;
        }else {
            return car1.getName().compareTo(car2.getName());
        }
    }
}

in SortableSaleTableView class i use. according you.

public SortableSaleTableView tableView; setColumnComparator(0, Comparators.getSaleNameComparator(tableView));

but still get same error

2019-05-21 10:00:35.080 24299-24299/com.royalways.skbikes W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'de.codecrafters.tableview.SortingStatus com.royalways.skbikes.utils.SortableSaleTableView.getSortingStatus()' on a null object reference 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at com.royalways.skbikes.model.Comparators$SaleNameComparator.compare(Comparators.java:78) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at com.royalways.skbikes.model.Comparators$SaleNameComparator.compare(Comparators.java:65) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at java.util.Collections$ReverseComparator2.compare(Collections.java:4377) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at java.util.TimSort.countRunAndMakeAscending(TimSort.java:352) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at java.util.TimSort.sort(TimSort.java:216) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at java.util.Arrays.sort(Arrays.java:1523) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at java.util.Collections.sort(Collections.java:238) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at de.codecrafters.tableview.SortableTableView$SortingController.sortDataSFCT(SortableTableView.java:305) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at de.codecrafters.tableview.SortableTableView$SortingController.onHeaderClicked(SortableTableView.java:251) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at de.codecrafters.tableview.InternalHeaderClickListener.informHeaderListeners(InternalHeaderClickListener.java:33) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at de.codecrafters.tableview.InternalHeaderClickListener.onClick(InternalHeaderClickListener.java:27) 2019-05-21 10:00:35.081 24299-24299/com.royalways.skbikes W/System.err: at android.view.View.performClick(View.java:5675) 2019-05-21 10:00:35.082 24299-24299/com.royalways.skbikes W/System.err: at android.view.View$PerformClick.run(View.java:22646) 2019-05-21 10:00:35.082 24299-24299/com.royalways.skbikes W/System.err: at android.os.Handler.handleCallback(Handler.java:836) 2019-05-21 10:00:35.082 24299-24299/com.royalways.skbikes W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103) 2019-05-21 10:00:35.082 24299-24299/com.royalways.skbikes W/System.err: at android.os.Looper.loop(Looper.java:203) 2019-05-21 10:00:35.082 24299-24299/com.royalways.skbikes W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6251) 2019-05-21 10:00:35.082 24299-24299/com.royalways.skbikes W/System.err: at java.lang.reflect.Method.invoke(Native Method) 2019-05-21 10:00:35.082 24299-24299/com.royalways.skbikes W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075) 2019-05-21 10:00:35.082 24299-24299/com.royalways.skbikes W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

without your help i can not solve this issue, it is issue regarding library

please solve that ASAP, or give Total feature in free library i support for the feature, it is necessary

thanks

ISchwarz23 commented 5 years ago

I think the issue is obvious. Still the SortableSaleTableView you pass is null. To solve the issue simply not pass a null reference to the Comparators.getSaleNameComparator(tableView) method.

This is not an issue with the library. It is a very basic programming issue.

The library is already for free, if you want pro features you should get the pro version. If you want me to write code for your app, you should purchase support.

rajam1215 commented 5 years ago

I think the issue is obvious. Still the SortableSaleTableView you pass is null. To solve the issue simply not pass a null reference to the Comparators.getSaleNameComparator(tableView) method.

This is not an issue with the library. It is a very basic programming issue.

The library is already for free, if you want pro features you should get the pro version. If you want me to write code for your app, you should purchase support.

Thanks @ISchwarz23
issue solve in my code, all sorting done without touch last row. thanks again. i close that issue with this solution it is very thankful if you add predefined functionality in free version of sortable table view