如何返回值getintent和putintents的参数 [英] how to return value to parameter in between getintent and putintents

查看:313
本文介绍了如何返回值getintent和putintents的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想以下(布尔=加)布尔更改为真正的被点击时,返回假的时候再次点击该产品的getter和setter方法​​。下面详细信息:低于code是getter和setter方法​​

 包kfc.project;

    公共类产品{


        字符串名称;
        INT servingSize;
        INT热量;
        INT脂肪;
        INT saturatedFat;
        诠释transFat;
        INT胆固醇;
        诠释钠;
        诠释碳水化合物;
        INT纤维;
        INT糖;
        INT蛋白;
        INT维生素A;
        INT维生素C;
        诠释钙;
        INT铁;
        布尔补充;

        公共产品(字符串名称,诠释servingSize,诠释热量,INT脂肪,诠释saturatedFat,INT transFat,诠释胆固醇,INT钠,诠释碳水化合物,INT纤维,诠释糖,诠释蛋白质,诠释维生素A,INT维生素C,INT钙,INT铁){
            this.name =名称;
            this.calories =热量;
            this.servingSize = servingSize;
            this.fat =脂肪;
            this.saturatedFat = saturatedFat;
            this.transFat = transFat;
            this.cholesterol =胆固醇;
            this.fiber =纤维;
            this.sugar =糖;
            this.sodium =钠;
            this.carbs =碳水化合物;
            this.protein =蛋白;
            this.vitamina =维生素A;
            this.vitaminc =维生素C;
            this.calcium =钙;
            this.iron =铁;
            添加= FALSE;
        }

        @覆盖
        公共字符串的toString(){

                返回名称;

        }

        公共产品(字符串名称,诠释servingSize){
            this.name =名称;
            this.servingSize = servingSize;
        }

        公共字符串的getName(){
            返回名称;
        }

        公众诠释getServingSize(){
            返回servingSize;
        }

        公众诠释getCalories(){
            返回的热量;
        }

        公众诠释getFat(){
            返回脂肪;
        }

        公众诠释getSaturatedFat(){
            返回saturatedFat;
        }

        公众诠释getTransFat(){
            返回transFat;
        }

        公众诠释getCholesterol(){
            返回胆​​固醇;
        }

        公众诠释getSodium(){
            返回钠;
        }

        公众诠释getCarbs(){
            返回碳水化合物;
        }

        公众诠释getProtein(){
            返回的蛋白质;
        }

        公众诠释getFiber(){
            返回纤维;
        }

        公众诠释getSugar(){
            返回糖;
        }
        公众诠释getVitaminA(){
            返回维生素A;
        }
        公众诠释getVitaminC(){
            返回维生素C;
        }
        公众诠释getCalcium(){
            返回钙;
        }
        公众诠释getIron(){
            返回的铁;
        }
        公共无效setAdded(布尔X){
            添加= X;
        }
        公共布尔getAdded(){
            收益增加;
        }

    }
 

这是对信息进行存储,并转移到一个列表视图中的ArrayList

 包kfc.project;

进口的java.util.ArrayList;

公共类产品列表{
    ArrayList的<产品>清单;


    公共产品列表(){
        名单=新的ArrayList<产品>();

        //此处是创建产品
        商品鸡;
        list.add(新产品(鸡,10,20,30,40,50,60,70,80,90,80,70,60,50,40,30));

        产品大米;
        list.add(新产品(米,11));
    }

    公众的String [] getNames(){
        INT C = 0;
        INT大小=则为list.size() -  1;
        的String []的名称=新的String [尺寸]

        而(尺寸与GT; = C){
            //names.add(list.get(三).getName());
            名字[C] = list.get(C).getName();
            C ++;
        }

        C = 0;

        返回名称;
    }

    公众的ArrayList<产品>的GetList(){
        返回列表;
    }

}
 

下面是code对于ListView那里是它转移到另一个Java文件,其中的XML文件位于意图

 包kfc.project;

进口的java.util.ArrayList;

进口android.app.Activity;
进口android.app.ListActivity;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.Toast;

公共类ProductListView扩展ListActivity {

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        最终产品列表PL =新产品列表();

        // setListAdapter(新ArrayAdapter<字符串>(这一点,R.layout.list_item,产品));
        setListAdapter(新ArrayAdapter<产品>(这一点,R.layout.list_item,pl.getList()));

        ListView的LV = getListView();
        lv.setTextFilterEnabled(真正的);

        lv.setOnItemClickListener(新OnItemClickListener(){

          公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){

            //单击时,显示出与TextView的文字敬酒
            Toast.makeText(getApplicationContext(),查看产品,Toast.LENGTH_SHORT).show();

            产品产品= pl.getList()得到(位置)。
            //意图工具和产品的细节
            意向意图=新的意图(ProductListView.this,productdetail.class);
            // !!!!!!!!!!!!!!!!!!!!!!!!!!
            //intent.putCharSequenceArrayListExtra("list,pl.getList());
            intent.putExtra(姓名,product.getName());
            intent.putExtra(份量,product.getServingSize());
            intent.putExtra(卡路里,product.getCalories());
            intent.putExtra(胖,product.getFat());
            intent.putExtra(饱和脂肪,product.getSaturatedFat());
            intent.putExtra(反式脂肪,product.getTransFat());
            intent.putExtra(胆固醇,product.getCholesterol());
            intent.putExtra(钠,product.getSodium());
            intent.putExtra(碳水化合物,product.getCarbs());
            intent.putExtra(纤维,product.getFiber());
            intent.putExtra(糖,product.getSugar());
            intent.putExtra(蛋白质,product.getProtein());
            intent.putExtra(维生素A,product.getVitaminA());
            intent.putExtra(维生素C,product.getVitaminC());
            intent.putExtra(钙,product.getCalcium());
            intent.putExtra(铁,product.getIron());
            intent.putExtra(补充,product.getAdded());

            ProductListView.this.startActivity(意向);

            // startActivity(新的意向书(kfc.project.productdetail));
          }

        });

    }

}
 

现在,这是其中我想定义布尔只要单击该按钮XML的Java文件。我怎样才能做到这一点。

 包kfc.project;

进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;

公共类产品详情延伸活动{

    @覆盖
    保护无效的onCreate(束捆){
        // TODO自动生成方法存根
        super.onCreate(包);
        的setContentView(R.layout.productdetail);
        //东西拿到意图
        意图receivedIntent = getIntent();


        字符串产品名称= receivedIntent.getStringExtra(姓名);
        INT productServingSize = receivedIntent.getIntExtra(份量,0);
        INT productCalories = receivedIntent.getIntExtra(卡路里,0);
        INT productFat = receivedIntent.getIntExtra(胖,0);
        INT productSaturatedFat = receivedIntent.getIntExtra(饱和脂肪,0);
        INT productTransFat = receivedIntent.getIntExtra(反式脂肪,0);
        INT productCholesterol = receivedIntent.getIntExtra(胆固醇,0);
        INT productSodium = receivedIntent.getIntExtra(钠,0);
        INT productCarbs = receivedIntent.getIntExtra(碳水化合物,0);
        INT productFiber = receivedIntent.getIntExtra(纤维,0);
        INT productSugar = receivedIntent.getIntExtra(糖,0);
        INT productProtein = receivedIntent.getIntExtra(蛋白质,0);
        INT productVitaminA = receivedIntent.getIntExtra(维生素A,0);
        INT productVitaminC = receivedIntent.getIntExtra(维生素C,0);
        INT productCalcium = receivedIntent.getIntExtra(钙,0);
        INT productIron = receivedIntent.getIntExtra(铁,0);
        布尔补充= receivedIntent.getBooleanExtra(补充,假);

        //钮扣
        按钮Add按钮=(按钮)findViewById(R.id.button1);
        按钮removeButton =(按钮)findViewById(R.id.button3);

        捆绑额外= getIntent()getExtras()。

        字符串名称= extras.getString(姓名);

        如果(名字!= NULL){
            TextView的文本1 =(TextView中)findViewById(R.id.productname);
            text1.setText(产品名称);

        }

        //显示/隐藏ADD'和; 删除钮扣
        如果(加== FALSE){
            //隐藏删除按钮
            removeButton.setVisibility(View.GONE);
            // SHOW添加按钮
            addButton.setVisibility(View.VISIBLE);
        }其他{
            // SHOW删除按钮
            removeButton.setVisibility(View.VISIBLE);
            //隐藏添加按钮
            addButton.setVisibility(View.GONE);
        }

        addButton.setOnClickListener(新View.OnClickListener(){

            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                //添加= TRUE;
            }
        });

        removeButton.setOnClickListener(新View.OnClickListener(){

            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                //添加= FALSE;
            }
        });


            TextView的文本2 =(TextView中)findViewById(R.id.servingsize);
            text2.setText(+ productServingSize);

            TextView的文本3 =(TextView中)findViewById(R.id.calories);
            text3.setText(+ productCalories);

            TextView的文本4 =(TextView中)findViewById(R.id.totalfat);
            text4.setText(+ productFat);

            TextView的text5 =(TextView中)findViewById(R.id.saturatedfat);
            text5.setText(+ productSaturatedFat);

            TextView的text6 =(TextView中)findViewById(R.id.transfat);
            text6.setText(+ productTransFat);

            TextView的text7 =(TextView中)findViewById(R.id.cholesterol);
            text7.setText(+ productCholesterol);

            TextView的text8 =(TextView中)findViewById(R.id.sodium);
            text8.setText(+ productSodium);

            TextView的text9 =(TextView中)findViewById(R.id.totalcarbohydrates);
            text9.setText(+ productCarbs);

            TextView的text10 =(TextView中)findViewById(R.id.fiber);
            text10.setText(+ productFiber);

            TextView的text11 =(TextView中)findViewById(R.id.sugar);
            text11.setText(+ productSugar);

            TextView的文本12 =(TextView中)findViewById(R.id.protein);
            text12.setText(+ productProtein);

            TextView的text13 =(TextView中)findViewById(R.id.vitamina);
            text13.setText(+ productVitaminA);

            TextView的text14 =(TextView中)findViewById(R.id.vitaminc);
            text14.setText(+ productVitaminC);

            TextView的text15 =(TextView中)findViewById(R.id.calcium);
            text15.setText(+ productCalcium);

            TextView的text16 =(TextView中)findViewById(R.id.iron);
            text16.setText(+ productIron);




        / *
        意向意图= getIntent();
        字符串str = intent.getStringExtra(姓名); * /

    }





}
 

解决方案

首先,如果你想通过这样的,你现在做的方式活动之间的信息,你应该让你的产品类的 Parcelable 并通过手动设置所有标记的​​对象,而不是的样子,你正在做的现在它。

然后,一旦你获得这个产品在产品详情的活动,您可以使用对象上的getter和setter,一旦用户完成后,你传回给你的previous活动。

做你想做的事的

然而,正确的方法是使用内容提供商:在意图不应该被用来传递之类的活动之间的产品持久性信息

I have this product getters and setters i want the boolean below (boolean=added) to be changed to true when it is clicked and back to false when its clicked again. More information below: code below is the getters and setters

 package kfc.project;

    public class Product {


        String name;
        int servingSize;
        int calories;
        int fat;
        int saturatedFat;
        int transFat;
        int cholesterol;
        int sodium;
        int carbs;
        int fiber;
        int sugar;
        int protein;
        int vitamina;
        int vitaminc;
        int calcium;
        int iron;
        boolean added;

        public Product (String name, int servingSize, int calories, int fat, int saturatedFat, int transFat, int cholesterol, int sodium, int carbs, int fiber, int sugar, int protein, int vitamina, int vitaminc, int calcium, int iron){
            this.name=name;
            this.calories=calories;
            this.servingSize=servingSize;
            this.fat=fat;
            this.saturatedFat=saturatedFat;
            this.transFat=transFat;
            this.cholesterol=cholesterol;
            this.fiber=fiber;
            this.sugar=sugar;
            this.sodium=sodium;
            this.carbs=carbs;
            this.protein=protein;
            this.vitamina=vitamina;
            this.vitaminc=vitaminc;
            this.calcium=calcium;
            this.iron=iron;
            added = false;
        }

        @Override
        public String toString(){

                return name;

        }

        public Product (String name, int servingSize){
            this.name=name;
            this.servingSize=servingSize;
        }

        public String getName() {
            return name;
        }

        public int getServingSize() {
            return servingSize;
        }

        public int getCalories() {
            return calories;
        }

        public int getFat() {
            return fat;
        }

        public int getSaturatedFat() {
            return saturatedFat;
        }

        public int getTransFat() {
            return transFat;
        }

        public int getCholesterol() {
            return cholesterol;
        }

        public int getSodium() {
            return sodium;
        }

        public int getCarbs() {
            return carbs;
        }

        public int getProtein() {
            return protein;
        }

        public int getFiber(){
            return fiber;
        }

        public int getSugar(){
            return sugar;
        }
        public int getVitaminA(){
            return vitamina;
        }
        public int getVitaminC(){
            return vitaminc;
        }
        public int getCalcium(){
            return calcium;
        }
        public int getIron(){
            return iron;
        }
        public void setAdded(boolean x){
            added = x;
        }
        public boolean getAdded(){
            return added;
        }

    }

This is the arraylist where the information is stored and is transfered to a list view

package kfc.project;

import java.util.ArrayList;

public class ProductList {
    ArrayList<Product> list;


    public ProductList (){
        list = new ArrayList<Product>();

        //CREATE PRODUCT HERE
        Product chicken;
        list.add(new Product("Chicken", 10, 20, 30, 40, 50, 60, 70, 80, 90, 80,70,60,50,40,30));    

        Product rice;
        list.add(new Product("Rice",11));
    }

    public String[] getNames (){
        int c = 0;
        int size = list.size() - 1;
        String[] names = new String[size];

        while (size >= c) {
            //names.add(list.get(c).getName());
            names[c] = list.get(c).getName();
            c++;
        }

        c = 0;

        return names;
    }

    public ArrayList<Product> getList (){
        return list;
    }

}

Here is the code for the listview where there is an intent that transfers it to another java file where the xml file is located

package kfc.project;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class ProductListView extends ListActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);

        final ProductList pl = new ProductList();

        //setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, PRODUCTS));
        setListAdapter(new ArrayAdapter<Product>(this, R.layout.list_item, pl.getList() ));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {

          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(), "View Product", Toast.LENGTH_SHORT).show();

            Product product = pl.getList().get(position);
            // intent stuff for product detail
            Intent intent = new Intent(ProductListView.this, productdetail.class);
            //!!!!!!!!!!!!!!!!!!!!!!!!!!
            //intent.putCharSequenceArrayListExtra("list", pl.getList());
            intent.putExtra("name",product.getName());
            intent.putExtra("serving size", product.getServingSize());
            intent.putExtra("calories", product.getCalories());
            intent.putExtra("fat", product.getFat());
            intent.putExtra("saturated fat", product.getSaturatedFat());
            intent.putExtra("trans fat", product.getTransFat());
            intent.putExtra("cholesterol", product.getCholesterol());
            intent.putExtra("sodium", product.getSodium());
            intent.putExtra("carbs", product.getCarbs());
            intent.putExtra("fiber", product.getFiber());
            intent.putExtra("sugar", product.getSugar());
            intent.putExtra("protein", product.getProtein());
            intent.putExtra("vitamina", product.getVitaminA());
            intent.putExtra("vitaminc", product.getVitaminC());
            intent.putExtra("calcium", product.getCalcium());
            intent.putExtra("iron", product.getIron());
            intent.putExtra("added", product.getAdded());

            ProductListView.this.startActivity(intent);

            //startActivity(new Intent("kfc.project.productdetail"));
          }

        });

    }

}

Now this is the xml java file wherein i want to define boolean whenever the button is clicked. How can i do this

package kfc.project;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class productdetail extends Activity{

    @Override
    protected void onCreate(Bundle bundle) {
        // TODO Auto-generated method stub
        super.onCreate(bundle);
        setContentView(R.layout.productdetail);
        //stuff to get intent
        Intent receivedIntent = getIntent();


        String productName = receivedIntent.getStringExtra("name");
        int productServingSize = receivedIntent.getIntExtra("serving size",0);
        int productCalories = receivedIntent.getIntExtra("calories",0);
        int productFat = receivedIntent.getIntExtra("fat",0);
        int productSaturatedFat = receivedIntent.getIntExtra("saturated fat",0);
        int productTransFat = receivedIntent.getIntExtra("trans fat",0);
        int productCholesterol = receivedIntent.getIntExtra("cholesterol",0);
        int productSodium = receivedIntent.getIntExtra("sodium",0);
        int productCarbs = receivedIntent.getIntExtra("carbs",0);
        int productFiber = receivedIntent.getIntExtra("fiber",0);
        int productSugar = receivedIntent.getIntExtra("sugar",0);
        int productProtein = receivedIntent.getIntExtra("protein",0);
        int productVitaminA = receivedIntent.getIntExtra ("vitamina",0);
        int productVitaminC = receivedIntent.getIntExtra ("vitaminc",0);
        int productCalcium = receivedIntent.getIntExtra ("calcium",0);
        int productIron = receivedIntent.getIntExtra ("iron",0);
        boolean added = receivedIntent.getBooleanExtra("added", false);

        //BUTTONS
        Button addButton = (Button) findViewById(R.id.button1);
        Button removeButton = (Button) findViewById(R.id.button3);

        Bundle extras = getIntent().getExtras();

        String name = extras.getString("name");

        if (name != null) {
            TextView text1 = (TextView) findViewById(R.id.productname);
            text1.setText(productName);

        }

        //SHOW/HIDE 'ADD' & 'REMOVE' BUTTONS
        if (added==false) {
            //HIDE 'REMOVE' BUTTON
            removeButton.setVisibility(View.GONE);
            //SHOW 'ADD' BUTTON
            addButton.setVisibility(View.VISIBLE);  
        }else{
            //SHOW 'REMOVE' BUTTON
            removeButton.setVisibility(View.VISIBLE);   
            //HIDE 'ADD' BUTTON         
            addButton.setVisibility(View.GONE); 
        }

        addButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                //added=true;
            }       
        });

        removeButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                //added=false;
            }       
        });


            TextView text2 = (TextView) findViewById(R.id.servingsize);
            text2.setText(""+productServingSize);

            TextView text3 = (TextView) findViewById(R.id.calories);
            text3.setText(""+productCalories);

            TextView text4 = (TextView) findViewById(R.id.totalfat);
            text4.setText(""+productFat);

            TextView text5 = (TextView) findViewById(R.id.saturatedfat);
            text5.setText(""+productSaturatedFat);

            TextView text6 = (TextView) findViewById(R.id.transfat);
            text6.setText(""+productTransFat);

            TextView text7 = (TextView) findViewById(R.id.cholesterol);
            text7.setText(""+productCholesterol);

            TextView text8 = (TextView) findViewById(R.id.sodium);
            text8.setText(""+productSodium);

            TextView text9 = (TextView) findViewById(R.id.totalcarbohydrates);
            text9.setText(""+productCarbs);

            TextView text10 = (TextView) findViewById(R.id.fiber);
            text10.setText(""+productFiber);

            TextView text11 = (TextView) findViewById(R.id.sugar);
            text11.setText(""+productSugar);

            TextView text12 = (TextView) findViewById(R.id.protein);
            text12.setText(""+productProtein);

            TextView text13 = (TextView) findViewById(R.id.vitamina);
            text13.setText(""+productVitaminA);

            TextView text14 = (TextView) findViewById(R.id.vitaminc);
            text14.setText(""+productVitaminC);

            TextView text15 = (TextView) findViewById(R.id.calcium);
            text15.setText(""+productCalcium);

            TextView text16 = (TextView) findViewById(R.id.iron);
            text16.setText(""+productIron);




        /*
        Intent intent = getIntent();
        String str = intent.getStringExtra("name");*/

    }





}

解决方案

First of all, if you want to pass this kind of information between activities in the way you're doing now, you should make your Product class Parcelable and pass the object instead of manually set all the flags the way you're doing it now.

Then once you retrieve the Product in your productdetail activity, you use the getter and setter on the object, and once the user is done, you pass it back to your previous activity.

However the right way of doing what you want to do is to use Content Providers: intents are not supposed to be used to pass persistent information like products between activities.

这篇关于如何返回值getintent和putintents的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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