传递价值从一个活动到另一个机器人 [英] Passing of value from one activity to another in android

查看:106
本文介绍了传递价值从一个活动到另一个机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个应用程序,它有一个文本框和搜索按钮,当我在文本框中输入一个数字,然后点击它需要通过进入到下一个活动的价值的搜索按钮,它使用该值从一个数据库中的值。

我使用下面的code传递值。

  search_button.setClickable(真正的);
      sea​​rch_button.setOnClickListener(新View.OnClickListener(){

      @覆盖
      公共无效的onClick(视图v){
        // TODO自动生成方法存根
        。字符串outlet_no = outlet_id.g​​etText()的toString();
        的System.out.println(outlet_no);
        如果(!outlet_no.isEmpty()){
        @燮pressWarnings(德precation)
        共享preferences我的preFS = getApplicationContext()getShared preferences(我的preFS,MODE_WORLD_READABLE)。
        共享preferences.Editor prefsEditor =我的prefs.edit();

        prefsEditor.putString(outlet_id,outlet_no);
        prefsEditor.commit();

        意图myIntent =新的意图(HomeActivity.this,StoreActivity.class);
        startActivity(myIntent);
        HomeActivity.this.startActivity(myIntent);
        }
        其他{
          Toast.makeText(getApplicationContext(),请输入一个出口ID,Toast.LENGTH_SHORT);
        }
      }
    });
 

我的问题是,它不列入传递值。任何一个可以帮我这个,也可以any1能解释我上面的code,我因此未写,而且我发现它理解困难。

StoreActivity侧

 公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.store);
        我的preFS = this.getShared preferences(我的preFS,MODE_WORLD_READABLE);
        mOutletID =我的prefs.getString(outlet_id,0);
        mOutletDetails =我的prefs.getString(outlet_details,{});
        Log.v(outlet_details,我的prefs.getString(outlet_details,{}));
        如果(mOutletDetails!={}){
            setOutletData(mOutletDetails);
        }
        其他{
            executeAjaxRequest();
        }
    }
 

你发

解决方案

误区:

1)为什么你想开始活动两次?

  startActivity(myIntent); //这是OK
HomeActivity.this.startActivity(myIntent);
 

2)从一个活动传递输入的文本到另一个,你可以使用 putExtra()意图的方法,例如:

  myIntent.putExtra(SearchText,outlet_no);
 

所以,你完全code将是:

 意图myIntent =新的意图(HomeActivity.this,StoreActivity.class);
 myIntent.putExtra(SearchText,outlet_no);
 startActivity(myIntent);
 

3)要接收你从第一次活动都通过值:

 意向意图= getIntent();
捆绑包= intent.getExtras();
串outlet_no = bundle.getString(SearchText);
 

4)共享preferences:

仅供参考,共享preference 是用来保存数据量小,可以作为参考的,后来在整个应用程序中使用。

您应该参考以下链接:

  1. 共享preferences
  2. 意图
  3. 意图和意图过滤器

Hi i have developed an app which has a text box and a search button when i enter a number in the text box and click on the search button it needs to pass the value entered to the next activity where it uses that value to get the value from a database.

I am using the following code to pass the value.

search_button.setClickable(true);
      search_button.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        String outlet_no = outlet_id.getText().toString();
        System.out.println(outlet_no);
        if(!outlet_no.isEmpty()){
        @SuppressWarnings("deprecation")
        SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = myPrefs.edit();

        prefsEditor.putString("outlet_id", outlet_no);
        prefsEditor.commit();

        Intent myIntent = new Intent(HomeActivity.this, StoreActivity.class);       
        startActivity(myIntent);
        HomeActivity.this.startActivity(myIntent);
        }
        else{
          Toast.makeText(getApplicationContext(), "Please enter an outlet id", Toast.LENGTH_SHORT);
        }  
      }
    });

My problem is that it doesnot pass the value. Can any one help me with this and also can any1 explain me the above code as i didnot write it and i am finding it difficult to understand it.

StoreActivity side

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store);      
        myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);             
        mOutletID = myPrefs.getString("outlet_id", "0");
        mOutletDetails = myPrefs.getString("outlet_details","{}");
        Log.v("outlet_details",myPrefs.getString("outlet_details","{}"));
        if(mOutletDetails != "{}"){
            setOutletData(mOutletDetails);
        }
        else{
            executeAjaxRequest();
        }
    }    

解决方案

Mistakes you made:

1) Why are you trying to start activity twice?

startActivity(myIntent);   // this is OK
HomeActivity.this.startActivity(myIntent);

2) To pass entered text from one activity to another, you can use putExtra() method of Intent, for example:

myIntent.putExtra("SearchText", outlet_no);

So your complete code would be:

 Intent myIntent = new Intent(HomeActivity.this, StoreActivity.class);   
 myIntent.putExtra("SearchText", outlet_no);    
 startActivity(myIntent);

3) To receive value which you have passed from first activity:

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String outlet_no= bundle.getString("SearchText");

4) SharedPreferences:

FYI, SharedPreference is used to save small amount of data which can be reference and used later across application.

You should refer below links:

  1. SharedPreferences
  2. Intent
  3. Intent and Intent Filters

这篇关于传递价值从一个活动到另一个机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆