如何在活动A到活动B中显示seekBar值和选定的微调项目?

时间:2015-11-29 18:07:09

标签: java android listview android-edittext seekbar

我在活动A中有一个listView,其值从活动B返回。当点击列表时,它将打算活动B进行编辑。

活动B

 @Override
 protected void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.add_details_information);
         addItemsOnSpinner();
         if(getIntent().getExtras()!=null)  // if has value pass from A
          {
              final String Project1=getIntent().getStringExtra("ReceiveProject");
              final String Description1=getIntent().getStringExtra("ReceiveDescription");
              final String Progress1=getIntent().getStringExtra("ReceiveProgress");
              final String TimeIn1=getIntent().getStringExtra("ReceiveTimeIn");
              final String TimeOut1=getIntent().getStringExtra("ReceiveTimeOut");
              //project.setText(Project1);
              description.setText(Description1);
              //progressText.setText("Covered:")
              timeIn.setText(TimeIn1);
              timeOut.setText(TimeOut1);
              }

            save.setOnClickListener(new View.OnClickListener()
             {  // return to A

           @Override
          public void onClick(View v)
          {
            Intent returnIntent=new Intent();
            Project=project.getSelectedItem().toString(); // Spinner Value
            Description=description.getText().toString(); //from editText
            progress=seekBar.getProgress(); // From SeekBar
            returnIntent.putExtra("Project",Project);
            returnIntent.putExtra("Description", Description);
            returnIntent.putExtra("progress", progress);
            Toast.makeText(getApplicationContext(), progress+"", Toast.LENGTH_LONG).show();
            returnIntent.putExtra("TimeIn", TimeIn);
            returnIntent.putExtra("TimeOut",TimeOut);
            setResult(Activity.RESULT_OK,returnIntent);
            finish();
            }
           });

 public void  addItemsOnSpinner()
    {
        project=(Spinner)findViewById(R.id.SpinnerProject);
        List<String> list = new ArrayList<String>();
        list.add("TRN-XXX-XXX");
        list.add("Pro-XXX-XXX);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.spinner_item, list);
        //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        project.setAdapter(adapter);
    }

活动A

   listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { // if listView is clicked
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                mClickedPosition = position;
                Intent i = new Intent(getApplication(), Add_Details_Information.class);
                i.putExtra("ReceiveProject", ReceiveProject);
                i.putExtra("ReceiveDescription", ReceiveDescription);
                i.putExtra("ReceiveProgress", ReceiveProgress);
                i.putExtra("ReceiveTimeIn", ReceiveTimeIn);
                i.putExtra("ReceiveTimeOut", ReceiveTimeOut);
                startActivityForResult(i,PROJECT_REQUEST_CODE);
            }
        });
    }

我知道我们可以使用setText来显示editText从A到B的传递值,但spinnerseekBar值如何?

这是活动A中的listView。从活动B返回值。

enter image description here

单击listView时,它将再次转到B进行编辑。

enter image description here

那么如何在B中显示微调器显示Pro-XXX-XXX并且seekBar变为48?有什么想法或建议吗?非常感谢

被修改

在使用了@Clairvoyant的答案之后,现在我得到了这个(对于微分值)。

活动A

enter image description here

活动A中有4个列表。

假设点击了第一个列表。

enter image description here

一切正常,只有微调器(项目/服务/培训)显示错误的值。它显示上一个列表中的微调器值(PRO-SKM-D5)而不是它自己(Pro-XXX-XXX)

2 个答案:

答案 0 :(得分:1)

第一步: addItemsOnSpinner 设置如下:

    public void addItemsOnSpinner(String value) 
    { 
     int position = 0; 
     project=(Spinner)findViewById(R.id.SpinnerProject); 
     List<String> list = new ArrayList<String>(); 

     list.add(position,"TRN-XXX-XXX"); 
     list.add("Pro-XXX-XXX"); 

     for(int i=0; i<list.size() ; i++){
         if(list.get(i).equalsIgnoreCase(value)){
             position = i;
             break;
         }
     }


    ArrayAdapter<String> adapter = new ArrayAdapter<String>      (getApplicationContext(),R.layout.spinner_item, list); 
     //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    project.setAdapter(adapter); 

    project.setSelection(position); 
   }

第二步:在为此处必须在spinner中显示的变量赋值时调用上述方法,例如:project1是要显示的字符串值然后,spinner按如下方式调用该方法:

final String Project1=getIntent().getStringExtra("ReceiveProject");
 addItemsOnSpinner(Project1);

答案 1 :(得分:0)

使用SharedPreferences。下次,googling helps

获取SharedPreferences

SharedPreferences prefs = getDefaultSharedPreferences(context);

阅读偏好设置:

String key = "test1_string_pref";
String default = "returned_if_not_defined";
String test1 = prefs.getString(key, default);

编辑和保存偏好

SharedPreferences.Edtior editor = prefs.edit(); //Get SharedPref Editor
editor.putString(key, "My String");
editor.commit();

更短的写作方式

prefs.edit().putString(key, "Value").commit();

SharedPreferences的其他信息:JavaDocAndroid Developers Article