微调器显示错误的值

问题描述:

我有一个简单的程序,我将微调器设置到某个位置。然后我调用第二个模块,当我返回时,我重置了微调器。微调器显示不显示微调器值。当您点击微调器时,它指向正确的值,但显示的值不正确。事实上,它实际上是退步了。微调器显示错误的值

我写了下面这个简单的程序来演示。只有当表单在具有至少1个其他元素的Linearlayout或TableLayout中具有微调器时才会发生这种情况。

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView 
    android:layout_width="fill_parent" android:layout_height="200dp" 
    android:text="Main Form" /> 

    <LinearLayout android:id="@+id/widget43" 
    android:layout_width="320dp" android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <Button 
     android:id="@+id/btn" android:text="Button" android:textSize="16sp"  
     android:layout_width="160dp" android:layout_height="40dp" /> 
    <Spinner 
     android:id="@+id/btnFour" android:textSize="16sp"  
     android:layout_width="160dp" android:layout_height="40dp" /> 
    </LinearLayout>  
</LinearLayout> 

next.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

    <TextView android:id="@+id/id1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Next" 
     /> 
</LinearLayout> 

main.java

package tt.zzz; 

import android.app.Activity; 
import android.os.Bundle; 
import android.content.Intent; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 

public class main extends Activity { 

@Override 
protected void onCreate(Bundle icicle) 
{ 
    super.onCreate(icicle);  
    setContentView(R.layout.main);  
    fillSpinner(); 

    Button btnGo = (Button) findViewById(R.id.btn); 
    btnGo.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { Go(); } }); 
} 

void Go() { 
    Intent i = new Intent(this, next.class); 
    startActivityForResult(i, 0); 
} 

public void fillSpinner(){ 
    Spinner spin_test = (Spinner) findViewById(R.id.btnFour); 

    ArrayAdapter<CharSequence> myAdapter = 
    new ArrayAdapter<CharSequence>(
    this, 
    android.R.layout.simple_spinner_item 
    ); 
    myAdapter.setDropDownViewResource(
        android.R.layout.simple_spinner_dropdown_item); 

    myAdapter.add("Option 1"); 
    myAdapter.add("Option 2"); 
    myAdapter.add("Option 3"); 

    spin_test.setAdapter(myAdapter); 
    spin_test.setSelection(1); 

    spin_test.setOnItemSelectedListener( 
       new Spinner.OnItemSelectedListener(){ 
       public void onItemSelected(AdapterView<?> 
          arg0,View arg1,int arg2,long arg3){ 
    } 
    public void onNothingSelected(AdapterView<?> arg0) { 
    } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, 
      Intent intent) { 
    super.onActivityResult(requestCode, resultCode, intent); 
    fillSpinner(); 
} 
} 

next.java

package tt.zzz; 

import android.app.Activity; 
import android.os.Bundle; 

public class next extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.next); 
    } 
} 

尝试致电

@Override 
public void onStart() 
{ 
    super.onStart(); 
    this.fillSpinner(); 
} 

由于所谓的onCreate只有一次,所以每次您应该刷新别处时间更新微调。在onStart() - 对于

+0

好吧,我想这和它没有任何效果。显示的微调器值在每个周期递减,并且弹出的值仍然显示正确的值,与收缩的微调器不同。 – miannelle 2010-10-17 21:08:53

适当的地方,我会做的barmaley建议相同,不同的是加入如下:

@Override 
public void onResume() 
{ 
    super.onResume(); 
    fillSpinner(); 
} 

这是为什么,因为你启动另一个作业的需要返回的原因,所以这一个暂停。

您为setSelection后,添加以下行:

myAdapter.notifyDataSetChanged();