0

I'm moving my first steps into android, so sorry if this is a stupid question.

I've followed this tutorial to implement a DialogFragment containing a ListView. This is the ColorDialogFragment.java:

public class ColorDialogFragment extends DialogFragment {
    String[] listItems = { "Red", "Blue", "Green"};
    ListView myList;
    String ipAddress;

    public static ColorDialogFragment newInstance(String ipAddress) {
        Bundle args = new Bundle();
        args.putString("ipAddress",ipAddress);
        ColorDialogFragment fragment = new ColorDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ipAddress = getArguments().getString("ipAddress");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment, null, false);
        myList = (ListView) view.findViewById(R.id.list);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, listItems);
        myList.setAdapter(adapter);
        myList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String colorHex;
                switch (i){
                    case 1:
                        colorHex = "#FF0000";
                        break;
                    case 2:
                        colorHex = "#0000FF";
                        break;
                    case 3:
                        colorHex = "#00FF00";
                        break;
                    default:
                        dismiss();
                        return;
                }
                ClientAsyncTask clientAsyncTask = new ClientAsyncTask(ipAddress, colorHex);
                clientAsyncTask.execute();
                dismiss();
            }
        });
    }
    private class ClientAsyncTask extends AsyncTask<Void, Void, Void> {
        ...
    }
}

And this is dialog_fragment.xml:

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

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>

    </LinearLayout>

</LinearLayout>

The problem is that when I click a list item onItemClick() is not called (and obviously I don't understand why).

justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
  • What do you mean by **it is not called**? What is the expected result? – Prahalad Deshpande Jan 31 '16 at 05:20
  • It means that if I place a breakpoint inside `onItemClick()`, the debugger should stop there (so the method is called), but this doesn't happen. BTW the expected result would be that another device background color is changed from the execution of `ClientAsyncTask` launched inside `onItemClick()` (and obviously it doesn't happen). – justHelloWorld Jan 31 '16 at 05:23
  • define actually you want?? – Vishal Patoliya ツ Jan 31 '16 at 05:43
  • I think that I already answered in the previous comment: I want that when I hit one list element the onItemClick() method is executed. And (again) this doesn't happen at all! – justHelloWorld Jan 31 '16 at 05:57
  • I had the same problem and solved it by creating custom adapter to set the item listener in its `getView()` method. – Pankaj Jan 31 '16 at 06:00
  • Does it highlight the item when you pressed on? – kopikaokao Jan 31 '16 at 10:09

1 Answers1

0

I found out by myself that the problem were the wrong indexes in switch statement (starting from 0 and not from 1, obviously).

justHelloWorld
  • 6,478
  • 8
  • 58
  • 138