-1

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

我的微调器代码:

 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);                

            }
        }
}
4

3 回答 3

2

尝试启动 SpinnerEdit Activity,startactivityforresult而不是startActivity让用户保存的值返回 Spinner Activity,然后您可以使用以下中的最新值更新 Spinner startactivityforresult

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();
  }
});
于 2012-07-16T07:34:42.603 回答
1

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

((BaseAdapter)s2.getAdapter()).notifyDataSetChanged();
于 2012-07-16T07:14:35.943 回答
1

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

于 2012-07-16T07:15:20.280 回答