Search code examples
javaandroidlistviewdialogfragment

onItemClick() not working


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).


Solution

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