Android:从Activity中的自定义适配器获取TextView

时间:2016-01-21 18:24:04

标签: android android-fragments

我有一个自定义适配器来填充我的listview。在我的适配器是一个DataObjectHolder。这包含视图:

public static class DataObjectHolder extends RecyclerView.ViewHolder {
        TextView label;
        public TextView dateTime, textViewIsAbonned, textViewAbos, textViewUserName;
        ImageView imageViewCardSmall;
        private GridView gridView;
        private GridViewAdapter gridAdapter;

        public DataObjectHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textViewOther);
            dateTime = (TextView) itemView.findViewById(R.id.textViewBig);
            textViewIsAbonned = (TextView) itemView.findViewById(R.id.textViewIsAbonned);
            textViewAbos = (TextView) itemView.findViewById(R.id.textViewAbos);
            imageViewCardSmall = (ImageView) itemView.findViewById(R.id.imageViewCardSmall);
            textViewUserName = (TextView) itemView.findViewById(R.id.textViewUserName);
            Log.i(LOG_TAG, "Adding Listener");
            gridView = (GridView) itemView.findViewById(R.id.gridView);
        }


    }

现在,我想在我的Activity中更改textViewIsAbonned中的文本,但我找不到解决方案。此外,我在我的活动中使用Fragments。我在asyn-task中的post-Execute方法中调用了Fragment中的Adapter:

    private class GetCustomerDetailsURL extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.e("IDRecyclerViewFragment ", "GET CUSTOMERDEITAILS URL");
//            progressBarProfile.setVisibility(View.VISIBLE);
//            getData();
        }

        @Override
        protected void onCancelled() {
            Log.e("IDRecyclerViewFragment", "TAB3");
            super.onCancelled();
        }

        @Override
        protected String doInBackground(String... params) {
            if (!isCancelled()) {
                Log.e("IDRecyclerViewFragment", "TAB3");
                try {
                    URL url = new URL(StaticVariables.url_customer_details + CodesForURL.getCustomerDetailsOne + CodesForURL.getCustomerDetailsTwo +
                            CodesForURL.getCustomerDetailsThree + CodesForURL.getCustomerDetailsFour + "&uid=" + idOfUser); //idOfUser);
                    Log.e("IDRecyclerViewFragment", "for customer details is " + url.toString());
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setUseCaches(true);
                    String readStream = readStream(con.getInputStream());

                    System.out.println(readStream);

                    arr = new JSONArray(readStream);
                    for (int i = 0; i < arr.length(); i++) {
                        object = arr.getJSONObject(i);
                    }
                    return readStream;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            } else {
                Log.e("IDRecyclerViewFragment", "TAB3");
                return null;
            }
        }


        @Override
        protected void onPostExecute(String string) {

            try {


                JSONObject customer = arr.getJSONObject(0);

                strUserName = customer.getString("name");
                strFullName = customer.getString("full_name");
                strAge = customer.getString("age");
                strEmail = customer.getString("email");
                strAbout = customer.getString("about_the_customer");
                strAboCounts = customer.getString("a");

                imageName = customer.getString("imageName");


                urlForImage = StaticVariables.baseUrlForImage + customer.get("imageName");
//                new DownloadImageTask(picture).execute(urlForImage);

//                ImageLoader.getInstance().displayImage(urlForImage, picture, optionsFeedImage, null);
//                imageLoader.get(urlForImage, new com.android.volley.toolbox.ImageLoader.ImageListener() {
//                            @Override
//                            public void onResponse(com.android.volley.toolbox.ImageLoader.ImageContainer imageContainer, boolean b) {
//                                logo_white.setImageBitmap(imageContainer.getBitmap());
//                            }
//
//                            @SuppressLint("ShowToast")
//                            @Override
//                            public void onErrorResponse(VolleyError volleyError) {
//                                Toast.makeText(getApplicationContext(), "No Profile Image for you!", Toast.LENGTH_SHORT);
//                            }
//                        }
//                );
            } catch (JSONException e) {
                return;
            } catch (NullPointerException npe) {
                Toast.makeText(NewMainActivity.getContextOfApplication(), "can't get details", Toast.LENGTH_SHORT).show();
            }

            mAdapter = new RecyclerViewMaterialAdapter(new MyRecyclerViewAdapter(getDataSet(), getActivity()));
            mAdapter.notifyDataSetChanged();
            mRecyclerView.setAdapter(mAdapter);
//            progressBarProfile.setVisibility(View.GONE);
        }
    }

在我的Activity中,我将Fragment称为MaterialViewPager:

mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {

            @Override
            public Fragment getItem(int position) {
                switch (position % 2) {
                    case 0:
                        return RecyclerViewFragment.newInstance();
//                    case 1:
//                        return WebViewFragment.newInstance();
                    default:
                        return null;
                }
            }

            @Override
            public int getCount() {
                return 1;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                switch (position % 4) {
                    case 0:
                        return "User Details";
                }
                return "";
            }
        });

总而言之,我想从我的Activity更改textViewIsAbonned中的文本。我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

您需要更新模型,然后您的适配器将更新视图。

请勿忘记致电li,以便您的适配器知道模型已更改。