I think the following is a basic problem.
I using this number Picker as there is none provided by Android SDK:
http://www.quietlycoding.com/?p=5&cpage=1#comment-10645
I have integrated it in an inflated alertdialog. Here the inflated layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<com.example.NumberPicker
android:padding="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I have added a button to the alertdialog. If it is clicked, I want to get the current value of the number picker.
The number picker class provides a function:
public static int getCurrent() {
return mCurrent;
}
Problem is that I am not aware how to call it. I have no object of the class.
If I call it via com.example.NumberPicker.getCurrent(), I have to declare it static and this has some negative side-effects.
Does someone know how I get the object of the picker class in order to call the getCurrent() function?
I think simple to create a new object is not the right way, because I want to call the function from the running object in my alerdialog.
Edit:
As I have expected:
NumberPicker np = new NumberPicker(MainScreen.this);
Log.v("TEST", "" + np.getCurrent());
This gives me always 0
Edit 2:
I added an Android ID:
<com.example.NumberPicker
android:id="@+id/numberPicker"
android:padding="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
I my code I do following:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(
R.layout.backfall_layout, null);
new AlertDialog.Builder(MainScreen.this)
.setTitle(this.getString(R.string.relapse))
.setView(textEntryView)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
NumberPicker np = (NumberPicker) findViewById(R.id.numberPicker);
Log.v("TEST", "" + np.getCurrent());
--
But now my App crashes after pressing the button..
Edit 3:
Solved it: I have to call
NumberPicker np = (NumberPicker) textEntryView.findViewById(R.id.cigarettesPicker);
np.setCurrent(1);
Add an
android:idvalue to yourNumberPickerelement in your layout XML.When you inflate the layout for your
AlertDialog, callfindViewById()on the inflated layout, passing in the ID you supplied in step #1, and casting the result to be aNumberPicker.