活动后的微调器更新

时间:2012-07-16 07:05:34

标签: android spinner

在我的应用程序中,当用户点击微调器中的项目时,它将转到新活动,因为我有5个编辑文本,因为我保留了微调器的值,当用户编辑此值并按下保存按钮时,编辑值应该替换spinner中的旧值。我怎么能实现这个?请帮助我..

我的微调代码:

 public class BudgetJulyo4Activity extends Activity
{
        /** Called when the activity is first created. */
        DBadapter db =new DBadapter(this); 
        DBadapterRec dbrec=new DBadapterRec(this);
        public void onCreate(Bundle savedInstanceState) 
        {       

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);  
            Button b1=(Button)findViewById(R.id.button1);
            Button b2=(Button)findViewById(R.id.button2);
            Button b3=(Button)findViewById(R.id.button3);
            Button b4=(Button)findViewById(R.id.button4); 
            Spinner s1=(Spinner)findViewById(R.id.spinner);
            Spinner s2=(Spinner)findViewById(R.id.spinner1);

            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                    this, R.array.recurr_array, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s1.setAdapter(adapter);        

            String group[]=getResources().getStringArray(R.array.group_array);------>Before calling edit(i.e when enters first time i am showing some hardcore value)

            ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item,group);
            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s2.setAdapter(adapter1);    
}

 protected void onActivityResult(int requestCode, int resultCode, Intent data) ------>After edit option.
        {      
            java.lang.System.out.println("Inside act");
            // TODO Auto-generated method stub        
//      }
            super.onActivityResult(requestCode, resultCode, data);        
            if(resultCode==1)
            {          
                 Spinner s2=(Spinner)findViewById(R.id.spinner1);
                String str1=data.getStringExtra("grp1val");    
                String str2=data.getStringExtra("grp2val");  
                String str3=data.getStringExtra("grp3val");
                String str4=data.getStringExtra("grp4val"); 
                String str5=data.getStringExtra("grp5val");  
                String str6="Edit";

                String group[] = new String[] { str1,str2,str3,str4,str5,str6};
                 ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
                        this, android.R.layout.simple_spinner_item,group);               
                adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                s2.setAdapter(adapter1);                

            }
        }
}

3 个答案:

答案 0 :(得分:2)

尝试启动SpinnerEdit Activity作为startactivityforresult而不是startActivity以获取用户保存的值返回Spinner活动,然后您可以使用startactivityforresult中的最新值添加Spinner:

public void onItemSelected(AdapterView<?> s2, View arg1,
    int pos, long id) {
    // TODO Auto-generated method stub  
    group=s2.getItemAtPosition(pos).toString();
    if(group.equals("Edit"))
      {
        Intent i = new Intent(getApplicationContext(),SpinnerEdit.class);
        startActivityForResult(i, 1);
      }
      //your code here...

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==1){
           String str1=ata.getStringExtra("grp1val");
           ...
           //here you can update your Spinner Adapter with latest values
        }
        else{
            Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
        }

并在SpinnerEdit中:

saveButton.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    Intent spin = new Intent();
    spin.putExtra("grp1val", group1.getText().toString()); 
    spin.putExtra("grp2val", group2.getText().toString()); 
    spin.putExtra("grp3val", group3.getText().toString()); 
    spin.putExtra("grp4val", group4.getText().toString()); 
    spin.putExtra("grp5val", group5.getText().toString());
    setResult(1,spin);
    finish();
  }
});

答案 1 :(得分:1)

如果您的Spinner的适配器扩展了BaseAdapter,那么您应该在修改数据后在其上调用notifyDataSetChanged()

((BaseAdapter)s2.getAdapter()).notifyDataSetChanged();

答案 2 :(得分:1)

在setOnItemSelectedListener中使用s2.setselection(position)并将该值保存在共享首选项中,并在该活动的oncreate中使用上述方法