Roger That
Roger That

Reputation: 95

onclicklistener for listview

I am aware that this question has been asked tons of times. But I am really a beginner and every case is slightly different so the answers I've seen couldn't help me out thus far.

I have a listview of images that is build up in an adapter. I want the user to be able to click an image, and then a dialog should show with some options. I have the dialog ready, but I can't make the onclicklistener work.

this is what I have in the activity, which are basically slightly edited excerpts from the android developers website:

public class ImageSelection extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_selection);
    ImageAdapter adapter = new ImageAdapter(this);

    ListView listView = (ListView) findViewById(R.id.my_list);
    listView.setAdapter(adapter);
    }

public void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(ImageSelection.this);
    builder.setTitle(R.string.goselect)
           .setItems(R.array.dif, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
           }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}
}

I figured I had to add something to the oncreate where the listView is created, but the things I've tried adding, only added errors.

Upvotes: 1

Views: 69

Answers (1)

Ravi
Ravi

Reputation: 35589

add this to onCreate:

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            showDialog();
        }
    });

Upvotes: 3

Related Questions