-1

I have two activity.Both activity loaded the same data in the spinner from server.and both activity populate properly.I'm not getting proper idea that change in one spinner item position also display the same spinner's item when switch to other activity.How to synchronize both spinners of two different activities.

private class BackTask extends AsyncTask<Void,Void,Void> {
    ArrayList<String> list;
    protected void onPreExecute(){
        super.onPreExecute();
        list=new ArrayList<>();
    }
    protected Void doInBackground(Void...params){
        InputStream is=null;
        String result="";
        try{
            HttpClient httpclient=new DefaultHttpClient();
            HttpPost httppost= new HttpPost("http://my_url/Service.asmx/GetServiceList");
            HttpResponse response=httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            // Get our response as a String.
            is = entity.getContent();
        }catch(IOException e){
            e.printStackTrace();
        }

        //convert response to string
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                result+=line;
            }
            is.close();
            //result=sb.toString();
        }catch(Exception e){
            e.printStackTrace();
        }
        // parse json data
        try{
            JSONArray jArray =new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                JSONObject jsonObject=jArray.getJSONObject(i);
                // add interviewee name to arraylist

                list.add(jsonObject.getString("ServiceName"));

        }
    }
    catch(JSONException e){
        e.printStackTrace();
    }
    return null;
}
protected void onPostExecute(Void result){
    listItems.addAll(list);
    adapter.notifyDataSetChanged();
}

}
Adi
  • 400
  • 8
  • 25

2 Answers2

0
public class StaticResources
{
    private List<String> sDataSource = new ArrayList<String>();
    public  List<String> getDataSource() {...}
    public void setDataSource(..) {...}
}

In both activities, refer to StaicResources.getDataSource(), that is it.

David
  • 15,894
  • 22
  • 55
  • 66
  • it is already very clear, you just use StaicResources.getDataSource() to populate the data and then use it in everywhere (inclusive of your two activities). – David Jul 28 '16 at 05:04
  • Can u understand my queston what i want ,there is no is no issue in population of data. – Adi Jul 28 '16 at 05:08
  • the issue is synchronization of two different spinner'item position in two different activities. – Adi Jul 28 '16 at 05:09
  • @David, would you not want to make these resources `static`? – SlashG Jul 28 '16 at 05:12
  • @Adi, if you have static shared resources, it is always synchronized. I could not understand what the problem is! – David Jul 28 '16 at 05:34
  • same data, comes from same url;it is not synchronized because they are in two different activity.so change in one spinner's item position not change in other spinneris item position,@david ,Do u get it – Adi Jul 28 '16 at 05:39
  • @Adi, as said if you get the latest update and keep it in a central data repository, like the one, you don't have such sync issues. – David Jul 28 '16 at 06:23
0

Try with this way

public static ArrayList listid,listdata;

onPostExecute(){

try{
 JSONArray jarrvideos=new JSONArray(responcedata);
   for(int i=0;i<jarrvideos.length();i++){
            JSONObject jobj = jarrvideos.getJSONObject(i);

           listid.add(jobj.getString("id"));
           listdata.add(jobj.getString("id"));
       }

 BindSpinnerData();

   }catch(Exception e){
  e.printStackTrace();
  }
}


private void BindSpinnerData() {
ArrayAdapter<String> spinnerLocation = new 
  ArrayAdapter<String(MedicalSurgical.this, R.layout.spinneritem, listdata);
    spinnerLocation.setDropDownViewResource(R.layout.spinnerpopupblack);
    spnDatasecond.setAdapter(spinnerLocation);
}

now it will display your first activity dpinner data if you need another activity same spinner data then you need to access listid,listdata array list in another class

for Accessing classname.listid or classname.listdata

Amitsharma
  • 1,577
  • 1
  • 17
  • 29
  • the issue is how to access thieir position in other activity's spinner – Adi Jul 28 '16 at 05:31
  • get spinner selected item id and name and go on second activity then set adapter to spinner and then set item on same position which you have already get in your first activity . may be this will help you. – Amitsharma Aug 26 '16 at 11:51
  • http://stackoverflow.com/questions/38929897/how-to-change-the-position-of-a-spinner-same-as-other-spinner-in-two-different-a Plz see this question ..I have tried a lot but something going wrong. – Adi Aug 26 '16 at 11:53
  • you dont have any need to set this type of solution this is very simple as i write same steps you have to follow your issue will resolve – Amitsharma Aug 26 '16 at 11:55
  • The above link is for reference of my complete code .I have just added a model and adapter class for other use,nothing more than that/ – Adi Aug 26 '16 at 12:00
  • how you going to set your spinner in second activity and first activity those code you posted here that is need for api to get data and parse that data .... same as you have given here a link that also says same in actual you need to set adapter from your way only nothing else ... no buddy are here to present to help you in free of cost and no buddy have not free time to stay personally to help you ...if you wana exact solution for your issue you have to show your issue in detail those things you need ... else your choice.... – Amitsharma Aug 26 '16 at 12:05
  • not enough time to resolve right now lets talk on this after 2 days. bye. – Amitsharma Aug 26 '16 at 12:22