Searching in AlertDialog with single choice item list

41 Views Asked by At

I want to achieve the following.

I have an AlertDialog.Builder, an ArrayAdapter with a select_dialog_singlechoice.xml and 3 buttons on the bottom of it.

Now I want to add an EditText field with search functionality (it can be on top of it or on the bottom, I do not care for the moment).

However, all my tries have not worked out so far.

I created a custom xml with an EditText and then tried it that way:

    DatabaseHandler db = new DatabaseHandler(this);
    List<Run> allEntries = db.getAllEntriesOrderedByRunNumber();
    int checkedItem = 0;
    final int[] whichItemChecked = new int[1];

    if(mPolylinePoints.size()>0) {
        mPolylinePoints.clear();
    }

    AlertDialog.Builder builderSingle = new AlertDialog.Builder(MapsActivity.this);

    if(allEntries.size() == 0) {
        builderSingle.setTitle(getResources().getString(R.string.no_run_available));
    } else {
        builderSingle.setTitle(getResources().getString(R.string.select_one_run));
    }
    builderSingle.setIcon(R.drawable.icon_notification);

    // prevents closing alertdialog when clicking outside of it
    builderSingle.setCancelable(true);

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MapsActivity.this, android.R.layout.select_dialog_singlechoice);
    for(int i = 0; i<allEntries.size(); i++) {
        int count = db.countDataOfRun(allEntries.get(i).getNumber_of_run());
        arrayAdapter.add(allEntries.get(i).getNumber_of_run() + ": " + allEntries.get(i).getDateTime() + "\n"
        + count + " points to load");
    }
    if(allEntries.size()>0) {
        builderSingle.setNegativeButton(getResources().getString(R.string.buttonWithoutColoring), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int numberOfRun = allEntries.get(whichItemChecked[0]).getNumber_of_run();

                final LatLng[] latLng = new LatLng[1];

                ExecutorService executor = Executors.newSingleThreadExecutor();
                Handler handler = new Handler(Looper.getMainLooper());

                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        //Background work here
                        List<Run> allEntries = db.getSingleEntryOrderedByDateTime(numberOfRun);
                        for (int i = 0; i < allEntries.size(); i++) {
                            latLng[0] = new LatLng(allEntries.get(i).getLat(), allEntries.get(i).getLng());
                            mPolylinePoints.add(latLng[0]);
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //UI Thread work here
                                mMap.clear();
                                drawView.setVisibility(View.INVISIBLE);
                                textViewSlow.setVisibility(View.INVISIBLE);
                                textViewFast.setVisibility(View.INVISIBLE);
                                mMap.addMarker(new MarkerOptions()
                                        .position(new LatLng(mPolylinePoints.get(0).latitude, mPolylinePoints.get(0).longitude))
                                        .title(getString(R.string.starting_position)));
                                polyline = mMap.addPolyline(new PolylineOptions().addAll(mPolylinePoints).color(Color.MAGENTA).jointType(JointType.ROUND).width(15.0f));
                                createCheckerFlag(mPolylinePoints);
                            }
                        });
                    }
                });
            }
        });
        builderSingle.setNeutralButton("Show", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                int numberOfRun = allEntries.get(whichItemChecked[0]).getNumber_of_run();

                final LatLng[] latLng = new LatLng[1];

                ExecutorService executor = Executors.newSingleThreadExecutor();
                Handler handler = new Handler(Looper.getMainLooper());

                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        //Background work here
                        List<Run> allEntries = db.getSingleEntryOrderedByDateTime(numberOfRun);
                        List<ColoredPoint> sourcePoints = new ArrayList<>();

                        for(int i = 0; i<allEntries.size(); i++) {
                            latLng[0] = new LatLng(allEntries.get(i).getLat(), allEntries.get(i).getLng());
                            //FIXME make speed adjustable
                            if(allEntries.get(i).getSpeed()>8) { //running is over 8-10 km/h
                                sourcePoints.add(new ColoredPoint(latLng[0], Color.GREEN));
                            } else if(allEntries.get(i).getSpeed()>6 && // jogging is 6-8 km/h
                                    allEntries.get(i).getSpeed()<8){
                                sourcePoints.add(new ColoredPoint(latLng[0], Color.YELLOW));
                            } else { // walking is around 5.5-6 km/h
                                sourcePoints.add(new ColoredPoint(latLng[0], Color.RED));
                            }
                            mPolylinePoints.add(latLng[0]);
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //UI Thread work here
                                showPolyline(sourcePoints);
                            }
                        });
                    }
                });
            }
        });
        builderSingle.setPositiveButton("Update", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);
                builder.setCancelable(true);

                String numberOfRun = arrayAdapter.getItem(whichItemChecked[0]);
                String[] splittedString = numberOfRun.split(":");
                int intNumberOfRun = Integer.parseInt(splittedString[0]);
                List<Run> run = db.getLastCommentEntryOfSelectedRun(intNumberOfRun);

                final EditText edittext = new EditText(MapsActivity.this);
                edittext.setHint(run.get(0).getComment());

                builder.setTitle("Enter your new comment here");
                builder.setView(edittext);
                builder.setPositiveButton(
                        "Update",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                db.updateCommentById(intNumberOfRun, edittext.getText().toString());
                                Toast.makeText(getApplicationContext(), getResources().getString(R.string.update_entry), Toast.LENGTH_LONG).show();
                            }
                        });
                builder.setNegativeButton(
                        "Delete",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                db.deleteSingleEntry(intNumberOfRun);
                                Toast.makeText(getApplicationContext(), getResources().getString(R.string.single_entry_deleted), Toast.LENGTH_LONG).show();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }
        });
    } else {
        builderSingle.setNegativeButton(getResources().getString(R.string.buttonCloseDialog), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
    }

    builderSingle.setSingleChoiceItems(arrayAdapter, checkedItem, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // user checked an item
            whichItemChecked[0] = which;
        }
    });

    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.customtrackview, null);
    builderSingle.setView(dialogView);

    EditText editText = (EditText) dialogView.findViewById(R.id.editText);
    editText.setText("test label");

    AlertDialog alertDialog = builderSingle.create();
    alertDialog.show();

This is my xml for the edittext

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Test"/>
</LinearLayout>

Here is the MapsActivity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relLayout"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MapsActivity">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="match_parent" />

    <com.github.pengrad.mapscaleview.MapScaleView
        android:id="@+id/scaleView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginEnd="181dp"
        android:layout_marginBottom="90dp"
        app:scale_color="#009"
        app:scale_expandRtl="true"
        app:scale_maxWidth="100dp"
        app:scale_outline="true"
        app:scale_strokeWidth="3dp"
        app:scale_textSize="12sp" />

    <TextView
        android:id="@+id/textViewSlow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|left"
        android:layout_marginLeft="125dp"
        android:layout_marginEnd="326dp"
        android:layout_marginBottom="90dp"
        android:text="Slow"
        android:textStyle="bold"
        android:textFontWeight="950"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/textViewFast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|left"
        android:layout_marginLeft="125dp"
        android:layout_marginEnd="326dp"
        android:layout_marginBottom="350dp"
        android:text="Fast"
        android:textStyle="bold"
        android:textFontWeight="950"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabRecording"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="168dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|right"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@android:drawable/ic_notification_overlay"
        android:onClick="onClickRecording"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabStopRecording"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="65dp"
        android:layout_marginBottom="168dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|right"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@android:drawable/ic_delete"
        android:onClick="onClickRecording"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabStatistics"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="225dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|right"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@drawable/ic_bar_chart"
        android:onClick="onClickRecording"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabTracks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="339dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|right"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@drawable/icon_notification"
        android:onClick="onClickRecording"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabSettings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="282dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|right"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@drawable/ic_settings"
        android:onClick="onClickRecording"/>

    <at.co.netconsulting.runningtracker.view.DrawView
        android:id="@+id/rectView"
        android:layout_width="16dp"
        android:layout_height="173dp"
        android:visibility="invisible" />
</RelativeLayout>

By doing it that way all my 3 buttons on the bottom are gone and the EditText is not visible at all.

Maybe, someone can tell me how to achieve what I am trying to do?

Thanks for any help in advance

0

There are 0 best solutions below