Top Picks

   
May 4, 2013

 

spinner inside an alertdialog.builder won't display any item

 

i am trying to make an alertdialog.builder with a spinner inside it. the spinner item is originally populated from the database. here is part of my code:

update.setOnClickListener(new OnClickListener() { public void onClick(View v) { final View update_layout = getLayoutInflater().inflate(R.layout.update_layout, null); AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setView(update_layout); builder.setTitle("Update existing DB"); builder.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { spinner = (Spinner) update_layout.findViewById(R.id.spinner1); List<String> arrayspin=new ArrayList<String>(); Cursor csr1 = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null); int count = csr1.getCount(); csr1.moveToFirst(); for (int m=0; m>count; m++){ if (csr1.getString(csr1.getColumnIndex("name"))!="android_metadata"){ String cont = csr1.getString(csr1.getColumnIndex("name")); arrayspin.add(cont); } else { m=m-1; } csr1.moveToNext(); } ArrayAdapter adp = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,arrayspin); adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adp); table2 = spinner.getSelectedItem().toString(); editText2 = (EditText) update_layout.findViewById(R.id.editText2); new Updating().execute(); } }); builder.setNegativeButton("back", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); } });

it gave me no error, but in the app, the spinner is showing no items. then i try to modified the code, just to test the spinner, into something like this:

update.setOnClickListener(new OnClickListener() { public void onClick(View v) { final View update_layout = getLayoutInflater().inflate(R.layout.update_layout, null); AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setView(update_layout); builder.setTitle("Update existing DB"); builder.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { spinner = (Spinner) update_layout.findViewById(R.id.spinner1); String[] items = { "this", "is", "a", "really", "really2", "really3", "really4", "really5", "silly", "list" }; ArrayAdapter adp = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,items); adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adp); table2 = spinner.getSelectedItem().toString(); editText2 = (EditText) update_layout.findViewById(R.id.editText2); new Updating().execute(); } }); builder.setNegativeButton("back", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); } });

but the spinner still not showing any items. what do i do wrong?

 

Answer

csr1.getColumnIndex("name"))!="android_metadata"

you should compare strings with

equals

or

equalsIgnoreCase

instead of

==

if condition should be like this

if (!csr1.getString(csr1.getColumnIndex("name")).equals("android_metadata"))
Professional Hosting fro Just Host

Filed under: Android Coding FAQ

Tags:

Comments

No Comments

Leave a reply