How to have an onTouchListener and an OnItemLongClickListener on the same listview in Android Studio

107 views Asked by At

I'm trying to display a list of strings in a listView to represent a schedule. When someone long clicks on one of the strings, using an OnItemLongClickListener I bring up an alert dialog box for them to change the string at the position they clicked. When someone swipes left or right anywhere on the listview, using an onSwipeTouchListener, I call a function that changes what day is being looked at. I have each part working on its own separately, however, when I include both parts together, the long click no longer registers. How do I fix this?

Here's my code:

ArrayAdapter<String> arrayAdapter =
                new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, courseNames);
        listDays.setAdapter(arrayAdapter);

        listDays.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
        {
            // argument position gives the index of item which is clicked
            public boolean onItemLongClick(AdapterView<?> arg0, View v, final int position, long arg3) {
                if (dayCounter != 0) {
                    Toast.makeText(getApplicationContext(), courseNames.get(position) + " Selected", Toast.LENGTH_LONG).show();
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("What would you like to rename this class to?");
                    final EditText input = new EditText(MainActivity.this);
                    input.setInputType(InputType.TYPE_CLASS_TEXT/* | InputType.TYPE_TEXT_VARIATION_PASSWORD*/);
                    builder.setView(input);
                    builder.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            inputString = input.getText().toString();
                            try {
                                OutputStreamWriter writer = new OutputStreamWriter(getApplicationContext().openFileOutput(
                                        scheduleNames[dayCounter], Context.MODE_PRIVATE));
                                for(int i = 0; i<editNames.size();i++) {
                                    int testPos;
                                    if(!isTuesday) {
                                        if (position >= 2) {
                                            testPos = position - 1;
                                        } else {
                                            testPos = position;
                                        }
                                        if (i == testPos) {
                                            writer.write(inputString + '\n');
                                        } else {
                                            writer.write(editNames.get(i) + '\n');
                                        }
                                    }
                                    if(isTuesday) {

                                            testPos = position;

                                        if (i == testPos) {
                                            writer.write(inputString + '\n');
                                        } else {
                                            writer.write(editNames.get(i) + '\n');
                                        }
                                    }
                                }
                               // System.out.println("writing username" + inputString);
                                writer.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            setupListView();
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    builder.show();
                }

                    return true;
                }
        });
        listDays.setOnTouchListener(new OnSwipeTouchListener(this) {

            @Override
            public void onLongClick() {
                super.onLongClick();

            }
            @Override
            public void onSwipeLeft() {
                realNextDay();
            }
            @Override
            public void onSwipeRight() {
                realPreviousDay();
            }
        });
0

There are 0 answers