Issue in sending value from MainActivity to Custom Adapter

79 Views Asked by At

I am getting two values in Fragment Class. One value is from SharedPreferences that I have saved at the time of Login and another value is from a JsonArray. Both have an userId. Now I want to compare those values and set a result value according to that. After that I want to pass that result value to adapter class and get that value.

I am using this code in to pass that value:

inbox = json.getJSONArray(TAG_FEED);
                //inboxImage = json.getJSONArray(TAG_TWEET_IMAGE);

                String savedId = mAppPreference.getUserID();

                // looping through All messages
                for (int i = 0; i < inbox.length(); i++) {
                    JSONObject c = inbox.getJSONObject(i);

                    /*inboxImage = json.getJSONArray(TAG_TWEET_IMAGE);
                    Log.d("babababa", inboxImage.toString());*/

                    Ozone_Beans ozonebean = new Ozone_Beans(c.getString(TAG_CONTENT), c.getString(TAG_NAME),
                            c.getString(TAG_DATE),c.getString(TAG_USER_IMAGE),c.getString(TAG_TWEET),c.getString(TAG_USER));

                    String userKiId = c.getString(TAG_USER);

                    String finalValue ;

                    if((savedId.equals(userKiId))){

                         finalValue = "a";
                    } else {

                         finalValue = "b";
                    }



                    context2 = getActivity().getApplicationContext();
                    OzoneAdapter ozonadptr = new OzoneAdapter(context2);
                    ozonadptr.finalval=finalValue.toString();

                    feedList.add(ozonebean);

And I am trying to retrieve this value in adapter class like this:

public OzoneAdapter(Context context2) {
        // TODO Auto-generated constructor stub
        context2.finalval.getText().toString().trim();
    }

But its allowing to write me this line: context2.finalval.getText().toString().trim();

Please tell me what how to achieve this.

My AsyncTask class is this:

class LoadOzone extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(context);
            pDialog.setMessage("Loading Inbox ...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting Inbox JSON
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            String url = INBOX_URL +mAppPreference.getUserID()+"/"+ "key"+"/"+mAppPreference.getServerKey();

            Log.d("urlll", url);
            // getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(url, "GET",params);
            Log.d("general JSON ", json.toString());
            try {
                inbox = json.getJSONArray(TAG_FEED);
                //inboxImage = json.getJSONArray(TAG_TWEET_IMAGE);

                String savedId = mAppPreference.getUserID();

                // looping through All messages
                for (int i = 0; i < inbox.length(); i++) {
                    JSONObject c = inbox.getJSONObject(i);

                    /*inboxImage = json.getJSONArray(TAG_TWEET_IMAGE);
                    Log.d("babababa", inboxImage.toString());*/

                    Ozone_Beans ozonebean = new Ozone_Beans(c.getString(TAG_CONTENT), c.getString(TAG_NAME),
                            c.getString(TAG_DATE),c.getString(TAG_USER_IMAGE),c.getString(TAG_TWEET),c.getString(TAG_USER));

                    String userKiId = c.getString(TAG_USER);

                    String finalValue ;

                    if((savedId.equals(userKiId))){

                         finalValue = "a";
                    } else {

                         finalValue = "b";
                    }


                    OzoneAdapter ozonadptr = new OzoneAdapter(finalValue.toString());
                    //ozonadptr.finalval=finalValue.toString();

                    feedList.add(ozonebean);
                    }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            adapter = new OzoneAdapter(feedList,context);
            oZone_listView.setAdapter(adapter);
        }
    }

    private void initialiseNoramlVariable() {
        context  = getActivity();
        mAppPreference = AppPreference.getInstance(context);
    }

this is my adapter class generating two constructor when I am trying to send the 'finalValue'

public OzoneAdapter(ArrayList<Ozone_Beans> feedList, Context context) {
        // TODO Auto-generated constructor stub
        super();
        this.feedList = feedList;
        this.context = context;
        this.activity = activity;
        inflater = (LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageloader = ImageLoader.getInstance();
        imageloader.init(ImageLoaderConfiguration.createDefault(context));
        imageloader1 = ImageLoader.getInstance();
        imageloader1.init(ImageLoaderConfiguration.createDefault(context));

    }


    public OzoneAdapter(String finalValue) {
        // TODO Auto-generated constructor stub

        String GettingValue = activity.finalValue.getText().toString().trim();
    }
1

There are 1 best solutions below

9
On BEST ANSWER

Try like below...

OzoneAdapter ozonadptr = new OzoneAdapter( getActivity() );

public OzoneAdapter(Activity activity) {
        // TODO Auto-generated constructor stub
        activity.finalval.getText().toString().trim();
    }

Edit:

You can directly pass it to OzoneAdapter class by passing it as constructor parameter like below.

OzoneAdapter ozonadptr = new OzoneAdapter(finalValue.toString());

public OzoneAdapter(String value) {
        // Get finalval here.            
}

Edit

AsyncTask class modify with below code:

class LoadOzone extends AsyncTask<String, String, String> {

    String finalValue = "";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Loading Inbox ...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting Inbox JSON
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        String url = INBOX_URL +mAppPreference.getUserID()+"/"+ "key"+"/"+mAppPreference.getServerKey();

        Log.d("urlll", url);
        // getting JSON string from URL
        JSONObject json = jsonParser.makeHttpRequest(url, "GET",params);
        Log.d("general JSON ", json.toString());
        try {
            inbox = json.getJSONArray(TAG_FEED);
            //inboxImage = json.getJSONArray(TAG_TWEET_IMAGE);

            String savedId = mAppPreference.getUserID();

            // looping through All messages
            for (int i = 0; i < inbox.length(); i++) {
                JSONObject c = inbox.getJSONObject(i);

                /*inboxImage = json.getJSONArray(TAG_TWEET_IMAGE);
                Log.d("babababa", inboxImage.toString());*/

                Ozone_Beans ozonebean = new Ozone_Beans(c.getString(TAG_CONTENT), c.getString(TAG_NAME),
                        c.getString(TAG_DATE),c.getString(TAG_USER_IMAGE),c.getString(TAG_TWEET),c.getString(TAG_USER));

                String userKiId = c.getString(TAG_USER);


                if((savedId.equals(userKiId))){

                     finalValue = "a";
                } else {

                     finalValue = "b";
                }

                feedList.add(ozonebean);
                }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        adapter = new OzoneAdapter(feedList,context,finalValue);
        oZone_listView.setAdapter(adapter);
    }
}

private void initialiseNoramlVariable() {
    context  = getActivity();
    mAppPreference = AppPreference.getInstance(context);
}

OzoneAdapter class is modify with below code...

public OzoneAdapter(ArrayList<Ozone_Beans> feedList, Context context, String finalval ) {
    // TODO Auto-generated constructor stub
    super();
    this.feedList = feedList;
    this.context = context;
    this.activity = activity;
    inflater = (LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageloader = ImageLoader.getInstance();
    imageloader.init(ImageLoaderConfiguration.createDefault(context));
    imageloader1 = ImageLoader.getInstance();
    imageloader1.init(ImageLoaderConfiguration.createDefault(context));

    // get final value here from constructor parameter
    String GettingValue = finalval;

}