Show alertDialog with radiobuttons when click a listViewItem

11,592

The error is clearly explanatory, you are missing a semicolon. Your code should be like

listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
        {
           AlertDialogView();
        }//ERROR I'm GETTING HERE is Syntax error, insert ";" to complete Statement
     };

and the above code should be inside the onCreate(). In the code that you provided, its floating in the middle of nowhere!

Share:
11,592
mXX
Author by

mXX

Updated on June 18, 2022

Comments

  • mXX
    mXX almost 2 years

    I have a listView with 2 items in it, the 2 items are "seconds" and "minutes" When I press "seconds" I'd like a alertDialogBox to open en show 5,10,15,... seconds. Same when I press minutes

    Something like this:

    enter image description here

    But I'm having trouble implementing it, because I don't understand very well how it is working. Here is the code I have:

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    
    public class SettingsActivity extends Activity {
    
        ListView listView = (ListView) findViewById(R.id.settingsList);
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.settings);
    
            String[] values = new String[] { "Seconds", "Minutes" };
    
            // Define a new Adapter
            // First parameter - Context
            // Second parameter - Layout for the row
            // Third parameter - ID of the TextView to which the data is written
            // Forth - the Array of data
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, android.R.id.text1, values);
    
            // Assign adapter to ListView
            listView.setAdapter(adapter);
    
            listView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    AlertDialogView();
                }
            });
    
        }
    
        private void AlertDialogView() {
            final CharSequence[] items = { "15 secs", "30 secs", "1 min", "2 mins" };
    
            AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);//ERROR ShowDialog cannot be resolved to a type
            builder.setTitle("Alert Dialog with ListView and Radio button");
            builder.setSingleChoiceItems(items, -1,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            Toast.makeText(getApplicationContext(), items[item],
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
    
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT)
                            .show();
                }
            });
    
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT)
                            .show();
                }
            });
    
            AlertDialog alert = builder.create();
            alert.show();
        }
    }
    
  • mXX
    mXX about 11 years
    I changed the code, how stupid of me... But I'm still getting the error on the showDialogBox. How come? The error is ShowDialog cannot be resolved to a type
  • ElefantPhace
    ElefantPhace about 11 years
    It would be better if you'd post the logcat so we can see what you're talking about
  • mXX
    mXX about 11 years
    Not possible, I can't compile because I'm having errors. Logcat is empty
  • Antrromet
    Antrromet about 11 years
    Instead of ShowDialog.this it should be the context. There is no such thing called as ShowDialog. It should be SettingsActivity.this instead.