简体   繁体   English

我的吸气剂返回了错误的值

[英]My getter returns a wrong value

I'm programming an Android application and I got a little problem. 我正在编写一个Android应用程序,但遇到了一些问题。 I'm trying get a value from the Class A in the Class B but it doesn't return the correct value. 我正在尝试从B类中的A类获取一个值,但是它没有返回正确的值。

Here's my code to better understand (Sorry for my poor english)! 这是我的代码,可以更好地理解(对不起我的英语不好!)!

Class A A级

package com.androidhive.androidlistview;

//import

public class AndroidListViewActivity extends ListActivity {

    int day;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // storing string resources into Array
        String[] adobe_products = getResources().getStringArray(R.array.adobe_products);

        // Binding Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, adobe_products));

        ListView lv = getListView();

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

              // selected item 
              String product = ((TextView) view).getText().toString();

              // Launching new Activity on selecting single List Item
              Intent i = new Intent(getApplicationContext(), SingleListItem.class);
              day = Integer.parseInt(product.replaceAll("[^\\d.]", ""));
              System.out.println(day);
              //prints 1 When I click on the first list item, 2 When I click on the second, ...
              startActivity(i);
              // sending data to new activity
              i.putExtra("product", product);
          }
        });
    }
    public int getDay() {
        return day;
    }
}

Class B B级

package com.androidhive.androidlistview;

//import

@SuppressLint({ "NewApi", "HandlerLeak" })
public class SingleListItem extends Activity {

    AndroidListViewActivity alva = new AndroidListViewActivity();

    int day;
    String url;
    String code;

    //others variables

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Graphic

        new NetworkOperation().execute();

    }

    class NetworkOperation extends AsyncTask<Void, Void, String> {
        protected String doInBackground(Void... params) {
            Document doc;
            try {
                day = alva.getDay();
                System.out.println(day);
                //prints 0
                url = "http://www.planetehockey.com/calendrier.php?saison=45&div=9&club=&journee=" + day + "&jour=&mois=&page=0";
                doc = Jsoup.connect(url).get();
                //Récupère le texte d'une balise ayant bg_tableau pour class
                Elements getId = doc.getElementsByClass("bg_tableau");
                code = getId.text();
                code = code + "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
                handler.sendEmptyMessage(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {

            //other code

        }
    };
}

Thank's a lot for all your answers it helped me a lot: 非常感谢您的所有回答,这对我有很大帮助:

How I solved the problem: 我如何解决问题:

Class A A级

      i.putExtra("product", product);
      startActivity(i);

and: 和:

Class B B级

int day = Integer.parseInt(i.getStringExtra("product").replaceAll("[^\\d.]", ""));

In your Class A, you're trying to bundle components AFTER the activity has been called. 在A类中,您尝试在调用活动之后捆绑组件。

put the call function like this.. 像这样放置调用函数

Intent i = new Intent(getApplicationContext(), SingleListItem.class);
day = Integer.parseInt(product.replaceAll("[^\\d.]", ""));
System.out.println(day);

i.putExtra("product", product);
startActivity(i);

The passes the parameter in a bundle to the called activity. 将参数以捆绑形式传递给被调用的活动。

HTH! HTH!

There are two simple solutions for your problem, 有两种简单的解决方案可以解决您的问题,

1. Pass day values in intent to SingleListItem 

Or 要么

2. Make day as a Static member and use it with class Name like, 
   public static int day; and access it `AndroidListViewActivity.day` 

and remove public int getDay() method from AndroidListViewActivity as in both activity it refers a different object of AndroidListViewActivity . 并从AndroidListViewActivity中删除public int getDay()方法,因为在这两个活动中,它都引用了AndroidListViewActivity的另一个对象。

Try doing i.putExtra("product", product); 尝试做i.putExtra(“ product”,product); before startActivity(i); 在startActivity(i)之前;

In your Activity A you have written the getter method but not setter method to set the value of day in your code. 在活动A中,您编写了getter方法而不是setter方法来在代码中设置day的值。 Just write the setter method also and set the value of day. 只需编写setter方法并设置day的值即可。

 public void setDay(int p_day)
 {
     day=p_day;
 }

Make the variable day as static. 将可变天设置为静态。 After setting the day value try to get it in activity B. I hope this will help you. 设置日值后,尝试在活动B中获取它。希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM