从MainActivity向Custom Adapter发送值时出现问题

时间:2015-06-23 14:06:39

标签: android string adapter

我在Fragment班级中获得了两个值。一个值来自我在登录时保存的SharedPreferences,另一个值来自JsonArray。两者都有userId。 现在我想比较这些值并根据它设置结果值。之后,我想将该结果值传递给适配器类并获取该值。

我正在使用此代码传递该值:

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);

我正在尝试在适配器类中检索此值,如下所示:

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

但它允许我写这一行:context2.finalval.getText().toString().trim();

请告诉我如何实现这一目标。

我的AsyncTask类是这样的:

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);
    }

当我尝试发送&#39; finalValue&#39;

时,这是我的适配器类生成两个构造函数
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 个答案:

答案 0 :(得分:1)

尝试如下...

OzoneAdapter ozonadptr = new OzoneAdapter( getActivity() );

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

修改

您可以将其作为构造函数参数传递给OzoneAdapter类,如下所示。

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

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

修改

使用以下代码修改

AsyncTask类:

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

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;

}