1

I have a splash screen activity(ACTIVITY A) which on load completion opens ACTIVITY B

ACTIVITY B ** consists of a button which opens **ACTIVITY C

ACTIVITY A loads a list using async task

I want this loaded list to be displayed in ACTIVITY C when opened

I have read many posts on how to pass values from 1st activity to 3rd activity and tried implementing all those but nothing helped

Evven tried passing the list object via intents from ACTIVITY A > ACTÌVITY B > ACTIVITY C but didnt work

Finally i have used " jacksons library " to convert the loaded list into a jsonstring then put it in sharedpreferences in ACTIVITY A , then retrive the jsonstring from sharedpreferences covert it back to list object in ** ACTIVITY C** and display the list

But the list is not getting displayed

What to do and is there any better process

splash activity(ACTIVITY A)

public class SplashActivity extends Activity{

    List<ParseObject> ob;
    List<CodeList> codelist = null;
    ObjectMapper mapper;
    SearchPreferences searchpref;

    @Override
    public void onCreate(Bundle savedInstanceState){
        // TODO: Implement this method
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        mapper = new ObjectMapper();
        searchpref = new SearchPreferences();

        new DataTask().execute();
    }

    public class DataTask extends AsyncTask<Void, Void, List<CodeList>>{
        @Override
        protected List<CodeList> doInBackground(Void[] p1){    
            codelist = new ArrayList<CodeList>();
            try {
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("InterActivity");

                query.orderByAscending("_created_at");

                ob = query.find();
                for (ParseObject inter : ob) {
                    CodeList map = new CodeList();
                    map.setIntroduction((String) inter.get("intro"));
                    codelist.add(map);
                }
                return codelist;
            } 
            catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<CodeList> result){
            try{
                String jsonsearchlist = mapper.writeValueAsString(result);
                Intent i = new Intent(SplashActivity.this, MainActivity.class);

                searchpref.save(SplashActivity.this, jsonsearchlist);

                startActivity(i);
            }
            catch (JsonProcessingException e){}
        }
    }
}

ACTIVITY C

public class SearchActivity extends Activity{

    protected  EditText searchedittext;
    ImageButton searchButton;
    List<ParseObject> ob;
    List<CodeList> splashcodes;
    FinalAdapter fnladapter;

    SearchPreferences searchpref;
    ObjectMapper mapper;

    @Override
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_layout);

        searchpref = new SearchPreferences();
        mapper = new ObjectMapper();

        String jsonsearchobj = searchpref.getValue(SearchActivity.this);

        try{
             splashcodes = (List<CodeList>) mapper.readValue(jsonsearchobj, CodeList.class);

            final ListView searchedlist = (ListView) findViewById(R.id.searchlist);
            fnladapter = new FinalAdapter(SearchActivity.this, splashcodes);
            searchedlist.setAdapter(fnladapter);
        }
        catch (IOException e){}
    }
}
Lonzak
  • 9,334
  • 5
  • 57
  • 88
user5894647
  • 544
  • 6
  • 15

2 Answers2

1

You were on the right path: Loop your information through the 2nd activty. Please note, that CodeList must be serializable.

public class SplashActivity extends Activity{
 ...
 protected void onPostExecute(ArrayList<CodeList> result){
   Intent intent = new Intent(SplashActivity.this, SoCalledBActivity.class);
   intent.putExtra("YOUR_UNIQUE_KEY",result);
   this.startActivity(intent);
 }
}

public class SoCalledBActivity extends Activity{
 ...
 Button trigger = (Button) findViewById(R.id.trigger);
    trigger.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
          //get result list
          ArrayList<CodeList> result = (ArrayList<CodeList>) this.getIntent().getExtras().get("YOUR_UNIQUE_KEY");
          Intent intent = new Intent(SoCalledBActivity.this, SearchActivity .class);
          //add list for C activity
          intent.putExtra("YOUR_UNIQUE_KEY",result);
          this.startActivity(intent);
        }
    });

public class SearchActivity extends Activity{
  ...
  public void onCreate(Bundle savedInstanceState){
   //extract list
   ArrayList<CodeList> result = (ArrayList<CodeList>) this.getIntent().getExtras().get("YOUR_UNIQUE_KEY");
   //do whatever you want with your list...
  }
...
}
Lonzak
  • 9,334
  • 5
  • 57
  • 88
  • Thnx , i ll try and let you know – user5894647 Mar 04 '16 at 09:54
  • In ACTIVITY A if i put intent.putExtra("YOUR_UNIQUE_KEY",result); , its giving me an error saying no applicablemethod to (java.lang.string, java.util.list ,, so i converted it like this i.putExtra("MYLIST", (Serializable)result); is it ok – user5894647 Mar 04 '16 at 10:05
  • Yes you are right - it must be serializable, so you can't use ...but e.g. ArrayList should work. And of course CodeList must implement the Serializable Interface... – Lonzak Mar 04 '16 at 10:08
  • thnx bro it works but i am getting a yellow line under the list cast in activity b and its saying,,THE CAST IS UNSAFE BEACUASE ITS NOT POSSIBLE TO CHECK AT RUNTIME WHETHER AN INSTANCE OF TYPE javva,lang.object S OF TYPE java.util.list – user5894647 Mar 04 '16 at 10:22
  • even doing that is giving me the same warning – user5894647 Mar 04 '16 at 10:39
  • 1
    Yeah that is normal. Add a @SuppressWarnings("unchecked")... The whole behaviour is described here if you are interested: http://stackoverflow.com/questions/509076/how-do-i-address-unchecked-cast-warnings or http://stackoverflow.com/questions/14642985/type-safety-unchecked-cast-from-object-to-listmyobject – Lonzak Mar 04 '16 at 10:48
0

Create a global class and put that list in that class. Once you finish with filling the list, access the list data in Activity C.

Gvs13
  • 126
  • 12
  • Ya evenchad this process in my mind but didnt understand what is wrong with the the method i followed – user5894647 Mar 04 '16 at 09:38
  • It's simple create a global class as follows; public class Globals extends Application { //yourlist } //Add in menifest file android:name=".Globals" Access list as follows; Globals g = (Globals) getApplication; g.yourlist. – Gvs13 Mar 04 '16 at 09:39
  • ya i have to try it and i will let you know,thnx – user5894647 Mar 04 '16 at 09:44
  • One more way which I used is to make your class serializable and pass it in order of Activity B->Activity C. – Gvs13 Mar 04 '16 at 09:44