-2

I have a class that retrieves ArrayList from Database. And I need to show this items in Alert Dialog builder. But im getting an error in this. Please Help!

Here is my error:

Error: The method setSingleChoiceItems(int, int, DialogInterface.OnClickListener) in the type AlertDialog.Builder is not applicable for the arguments (Name[], int, new DialogInterface.OnClickListener(){})

ArrayList<Name> n_names = null;


n_names = db.getAllNames();

AlertDialog.Builder builder = new AlertDialog.Builder(Name.this);
    builder.setTitle("Choose Name");
    builder.setSingleChoiceItems(n_names.toArray(new Name[n_names.size()]), -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

    }});

    builder.setPositiveButton("Ok",
     new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {

    }});


    builder.setNegativeButton("Cancel",
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {


    }});
    AlertDialog alert = builder.create();
    alert.show();
nKn
  • 13,691
  • 9
  • 45
  • 62
belladonna
  • 175
  • 2
  • 11
  • 2
    what is the error can you specify?. – Raghunandan Jan 15 '14 at 08:16
  • how you initialize your n_name here ? – Ranjit Jan 15 '14 at 08:19
  • 1
    i think it still null. – Ranjit Jan 15 '14 at 08:21
  • Error: The method setSingleChoiceItems(int, int, DialogInterface.OnClickListener) in the type AlertDialog.Builder is not applicable for the arguments (Name[], int, new DialogInterface.OnClickListener(){}) – belladonna Jan 15 '14 at 08:29
  • This is how I initialized n_names. This was retrieved from the database. n_names = db.getAllNames(); – belladonna Jan 15 '14 at 08:30
  • @belladonna you have wrong parameters for the method that is your mistake. its int int and DialogInterface.OnClickListener – Raghunandan Jan 15 '14 at 08:40
  • You just need to return String ArrayList from your db.getAllNames() method and convert that into String Array and bind to builder.setSingleChoiceItems(Here your String Array,checkedItem,listener) – M D Jan 15 '14 at 08:46
  • @M D --yes I already did that--converted Arraylist to String Array But its giving me an error: java.lang.ArrayStoreException: source[0] of type com.example.name cannot be stored in destination array of type java.lang.CharSequence[] – belladonna Jan 15 '14 at 08:57
  • Hi!Can anyone help me on how to display custom arraylist/arrayadapter in listview?Here's my code ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, n_names); lv.setAdapter(adapter); I have no error. But it displays nothing. please help – belladonna Jan 20 '14 at 07:51

1 Answers1

0

I think the error is that you're providing a customized class object to the setSingleChoiceItems method. The first parameter has to be a a CharSequence[], a ListAdapter or a Cursor. If you're not intended to put one of them, you'll have to extend the Dialog class an implement your own setSingleChoiceItems method.

---- EDIT ----

In your case, I'd recommend extending the ArrayAdapter class, as you have some customized structures to show (in your case, a CheckBox). To help you do that, you may see my answer to a similar question and also a little basic explaination on how to extend the ArrayAdapter class, here. Hope this helps you!

Community
  • 1
  • 1
nKn
  • 13,691
  • 9
  • 45
  • 62
  • but how would i do that? or can you help me implement a listview with checkbox? so that i could display the arraylist? – belladonna Jan 16 '14 at 01:08
  • Hi!Can anyone help me on how to display custom arraylist/arrayadapter in listview?Here's my code ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, n_names); lv.setAdapter(adapter); I have no error. But it displays nothing. please help – belladonna Jan 20 '14 at 07:55
  • Seems that the datatype you're using on your ArrayAdapter is not standard (Name), so the ArrayAdapter doesn't know what to display. You should extend the ArrayAdapter class and override the getView() method in order to control what is displayed (the base class is just for standard data types). – nKn Jan 20 '14 at 08:40
  • just did! Thanks NKN! – belladonna Jan 23 '14 at 03:25
  • You're welcome! if my answer helped, you should accept it so other users may find it helpful too. – nKn Jan 23 '14 at 07:16