简体   繁体   中英

My getter returns a wrong value

I'm programming an Android application and I got a little problem. I'm trying get a value from the Class A in the Class B but it doesn't return the correct value.

Here's my code to better understand (Sorry for my poor english)!

Class 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

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

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

and:

Class 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.

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!

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 .

Try doing i.putExtra("product", product); before 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. Just write the setter method also and set the value of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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