开发者

Default spinner style in android

I am developing an android application in which I am populating a spinner from database using simpleAdaptor.

SimpleCursorAdapter deptype =new SimpleCursorAdap开发者_开发技巧ter(this,R.layout.dbspinner, depcur, from, to);   dep.setAdapter(deptype);

Data is loading fine but I don't like the "Look" of the Spinner.

The spinner I got with

 ArrayAdapter<CharSequence> practype = ArrayAdapter.createFromResource(this,R.array.practice, android.R.layout.simple_spinner_item);

is more beautiful with a radio button on the side,while the one I got is just showing contents separated by lines which is not at all beautiful.

I tried various changes in my layout of dbspinner but nothing is equal to the default stock spinner in android. I also tried to replace dbspinner with android.R.layout.simple_spinner_item but I got blank boxes with radio buttons but no text.

How can I get the stock default spinner?

Should i load the contents of database into a string and give to array adapter? If so how to it?


The simple_spinner_item is defined as:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee" />

So to use it as your item's view, use "text1" as your id in the resource integer array.

Ie:

// Create the array containing column names
String[] columns = new String[] { "ColumnNameX" };

// Create the array containing resource ids
int[] resources = new int[] { android.R.id.text1 };

// Create the cursor adapter
mCursorAdapter = new SimpleCursorAdapter(
    MyActivity.this,
    android.R.layout.simple_spinner_item,
    data,
    columns,
    resources);


You can use this code:

  AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Category");
        builder.setCancelable(true);
        builder.setSingleChoiceItems(CATEGORIES_TXT, mSelectedCategory, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //new category selected
                dialog.dismiss();
            }
        });
        AlertDialog alert=builder.create();
        alert.show();

CATEGORIES_TXT is a String[] and mSelectedCategory an int representing the selected category.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜