简体   繁体   中英

Get the list count value in first Activity from second Activity

I am trying to get the total messages value from second Activity's list. I have implemented this. But it always shows the value of 0 or null in case of String. Can anyone tell me how can I get the ArrayList size in my first Activity.

My code is,

In second activity:

 private static List<String> msgList = new ArrayList<String>();//above oncreate method
 setMsgList(getSMS());//call the method
 int count = getMsgList().size();
 msgcount = String.valueOf(count);
 System.out.println("listcount" + msgcount);//it prints the correct list size

And my first Activity is,

String size=InboxActivity.msgcount;
System.out.println("count:::::::::::::"+size);//it prints 0

Can anyone tell me how can I get the list count in first Activity?

将列表设为静态,然后创建静态的get方法。

Make your msgList public and write this code in your first Activity and it will be ok.

SecondActivity.msgList.size(); 

If you still want to leave the msgList as private, you should make eventually a public static method in your SecondActivity and call it from the first activity like:

SecondActivity.method();

Some people here have suggested that you should make the list public, but that is not a good idea. If you want to return a value to the previous Activity you can use startActivityForResult .
Try something like this:

// Start the second Activity in the first Activity not with startActivity but with startActivityForResult
Intent secondActivityIntent = new Intent(this, SecondActivity.class);
// The number is used to identify the result later
startActivityForResult(secondActivityIntent, 0); 

In your second Activity:

// You can set a desired Result with setResult.
// You can attach data to the Result with an Intent.
Intent intent = new Intent();
intent.putExtra("tag", someString);
setResult(RESULT_OK, intent); // Use RESULT_CANCELED if something went wrong

Again in your first Activity:

// Implement this method to retrieve the data from the second Activity.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check if the requestCode is corresponding to your Second Activity.
    if (requestCode == 0) {
        // Check if result is ok
        if (resultCode == RESULT_OK) {
            // Retrieve the value
            String value = data.getStringExtra("tag");
        }
    }
}

You can find more detailed Information about this topic here .


You can achieve this by the following
get the list count in the 2nd activity and store it in the application object and in the 1st activity get from the application object
the following are sample code snippets

public class App extends Application
{
}
ACtivity 1:

protected void onStart() 
{
    mApp = (IVYApplication)getApplicationContext();
      int count= mApp.getListCount();  //Get list count
    super.onStart();
}

acitvity 2:
protected void onStart() 
{
super.start();
    mApp = (IVYApplication)getApplicationContext();
      mApp.setListCount(List.getcount());  
//getting the count of elements in list and assign in the application boject
}

To get data from second activity to first activity you can use startActivityForResult() method. Try this link it may help you.

I believe, the right way for interaction between activities is to pass data in Intent s.

One more possible way is SharedPreferences (although I remember on at least one version of Android I had to get the instance using the application context and store it in a static variable).

The 3rd way is a singleton class.

From the MVC viewpoint, an Activity is a Controller , its lifetime is limited by the lifetime of the View (you likely reuse the View classes from the framework). The number you want to pass from one Activity to the other must most likely belong to the Model .

So consider using StartActivityForResult() instead. Or, probably, defining a Model class that outlives Views, Activities and even processes, because an Android process may be killed and restarted at any moment after onPause() .

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