Example usage for android.widget TableLayout setOnLongClickListener

List of usage examples for android.widget TableLayout setOnLongClickListener

Introduction

In this page you can find the example usage for android.widget TableLayout setOnLongClickListener.

Prototype

public void setOnLongClickListener(@Nullable OnLongClickListener l) 

Source Link

Document

Register a callback to be invoked when this view is clicked and held.

Usage

From source file:com.mifos.utils.DataTableUIBuilder.java

public LinearLayout getDataTableLayout(final DataTable dataTable, JsonArray jsonElements,
        LinearLayout parentLayout, final Context context, final int entityId,
        DataTableActionListener mListener) {
    dataTableActionListener = mListener;

    /**/*  w ww. j  a  va 2 s  . c om*/
     * Create a Iterator with Json Elements to Iterate over the DataTable
     * Response.
     */
    Iterator<JsonElement> jsonElementIterator = jsonElements.iterator();
    /*
     * Each Row of the Data Table is Treated as a Table Here.
     * Creating the First Table for First Row
     */
    tableIndex = 0;
    while (jsonElementIterator.hasNext()) {
        TableLayout tableLayout = new TableLayout(context);
        tableLayout.setPadding(10, 10, 10, 10);

        final JsonElement jsonElement = jsonElementIterator.next();
        /*
        * Each Entry in a Data Table is Displayed in the
        * form of a table where each row contains one Key-Value Pair
        * i.e a Column Name - Column Value from the DataTable
        */
        int rowIndex = 0;
        while (rowIndex < dataTable.getColumnHeaderData().size()) {
            TableRow tableRow = new TableRow(context);
            tableRow.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            tableRow.setPadding(10, 10, 10, 10);
            if (rowIndex % 2 == 0) {
                tableRow.setBackgroundColor(Color.LTGRAY);
            } else {
                tableRow.setBackgroundColor(Color.WHITE);
            }

            TextView key = new TextView(context);
            key.setText(dataTable.getColumnHeaderData().get(rowIndex).getColumnName());
            key.setGravity(Gravity.LEFT);
            TextView value = new TextView(context);
            value.setGravity(Gravity.RIGHT);
            if (jsonElement.getAsJsonObject().get(dataTable.getColumnHeaderData().get(rowIndex).getColumnName())
                    .toString().contains("\"")) {
                value.setText(jsonElement.getAsJsonObject()
                        .get(dataTable.getColumnHeaderData().get(rowIndex).getColumnName()).toString()
                        .replace("\"", ""));
            } else {
                value.setText(jsonElement.getAsJsonObject()
                        .get(dataTable.getColumnHeaderData().get(rowIndex).getColumnName()).toString());
            }

            tableRow.addView(key, new TableRow.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
            tableRow.addView(value, new TableRow.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
            TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParams.setMargins(12, 16, 12, 16);
            tableLayout.addView(tableRow, layoutParams);

            rowIndex++;
        }

        tableLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context, "Update Row " + tableIndex, Toast.LENGTH_SHORT).show();
            }
        });

        tableLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                Toast.makeText(context, "Deleting Row " + tableIndex, Toast.LENGTH_SHORT).show();

                BaseApiManager baseApiManager = new BaseApiManager();
                DataManager dataManager = new DataManager(baseApiManager);
                Observable<GenericResponse> call = dataManager
                        .removeDataTableEntry(dataTable.getRegisteredTableName(), entityId,
                                Integer.parseInt(jsonElement.getAsJsonObject()
                                        .get(dataTable.getColumnHeaderData().get(0).getColumnName())
                                        .toString()));
                Subscription subscription = call.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<GenericResponse>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(GenericResponse genericResponse) {
                                Toast.makeText(context, "Deleted Row " + tableIndex, Toast.LENGTH_SHORT).show();
                                dataTableActionListener.onRowDeleted();
                            }
                        });

                return true;
            }
        });

        View v = new View(context);
        v.setBackgroundColor(ContextCompat.getColor(context, R.color.black));
        parentLayout.addView(tableLayout);
        parentLayout.addView(v, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 5));
        Log.i("TABLE INDEX", "" + tableIndex);
        tableIndex++;
    }
    return parentLayout;
}