如何将数据从片段(包含recyclerview)传输到对话框片段,该片段在对话框片段上显示所选项目

时间:2017-03-31 12:03:12

标签: android android-fragments

如何将数据从fragment(包含recyclerview)传输到对话框片段,在对话框片段中显示所选项目,在我的情况下,我收到错误。

这是RecyclerViewCollegeNewsLetter.java,

public class RecyclerViewCollegeNewsLetter extends RecyclerView.Adapter<RecyclerViewCollegeNewsLetter.MyCollege> {

static List<Topics> topicses = new ArrayList<>();
Context context;
Topics topics;


public RecyclerViewCollegeNewsLetter(Context context, ArrayList<Topics> topicses) {
    this.context = context;
    this.topicses = topicses;
}

@Override
public RecyclerViewCollegeNewsLetter.MyCollege onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_row, parent, false);
    Log.d("ssss", "in oncreate");


    return new MyCollege(view);
}

@Override
public void onBindViewHolder(final RecyclerViewCollegeNewsLetter.MyCollege holder, final int position) {
    topics = topicses.get(position);
    holder.id.setText(topics.getId());
    holder.givenby.setText(topics.getGivenby());
    holder.article.setText(topics.getArticle());

    holder.more.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            FilterDialogFragment filterDialogFragment = new FilterDialogFragment();
            int pos=holder.getAdapterPosition();
            Bundle bundle = new Bundle();
            bundle.putString("more_id",topicses.get(pos).getId() );
            Log.e("rrrr",topicses.get(pos).getId());
            bundle.putString("more_given_by",topicses.get(pos).getGivenby());
            bundle.putString("more_article", topicses.get(pos).getArticle());
            bundle.putString("more_content", topicses.get(pos).getContent());

            //pasing data to filterDialogFragment
            filterDialogFragment.setArguments(bundle);


            showDialog();
        }
    });
    // holder.content.setText(topics.getContent());

    if (topics.getContent().length() > 100) {
        holder.content.setText((topics.getContent().substring(0, 99)) + "...");
    } else
        holder.content.setText(topics.getContent());

}

@Override
public int getItemCount() {
    return topicses.size();
}

public class MyCollege extends RecyclerView.ViewHolder {
    TextView id, content, article, givenby;
    Button more;

    public MyCollege(View itemView) {
        super(itemView);
        id = (TextView) itemView.findViewById(R.id.id);
        givenby = (TextView) itemView.findViewById(R.id.givenby);
        article = (TextView) itemView.findViewById(R.id.article);
        content = (TextView) itemView.findViewById(R.id.content);
        more = (Button) itemView.findViewById(R.id.more);

    }

}
void showDialog() {
    FilterDialogFragment filterDialogFragment = new FilterDialogFragment();
   // MoreDetails moreDetails = new MoreDetails();
    FragmentManager ft = ((MainActivity) context).getSupportFragmentManager();
    DialogFragment newFragment = filterDialogFragment.newInstance();
    newFragment.show(ft, "dialog");
 }

这是FilterDialogFragment.java

 public class FilterDialogFragment extends DialogFragment {

private AdView mAdView;
Bundle mArgs;
TextView more_id, more_content, more_article, more_givenby;

static FilterDialogFragment newInstance() {
    FilterDialogFragment f = new FilterDialogFragment();
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.more_details, container, false);
    mAdView = (AdView) v.findViewById(R.id.adView);
    more_id = (TextView) v.findViewById(R.id.more_id);
    more_content = (TextView) v.findViewById(R.id.more_content);
    more_article = (TextView) v.findViewById(R.id.more_article);
    more_givenby = (TextView) v.findViewById(R.id.more_givenby);


    return v;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    AdRequest adRequest = new AdRequest.Builder()
            .build();
    mAdView.loadAd(adRequest);

    Bundle mArgs = getArguments();
    more_id.setText(mArgs.getString("more_id"));
}

@Override
public void onPause() {
    if (mAdView != null) {
        mAdView.pause();
    }
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
    if (mAdView != null) {
        mAdView.resume();
    }
}

@Override
public void onDestroy() {
    if (mAdView != null) {
        mAdView.destroy();
    }
    super.onDestroy();
}

@Override
public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null) {
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
    }
}

我把Bundle mArgs = getArguments();

more_id.setText(mArgs.getString( “more_id”));

onActivityCreated中的

但是给了我错误: -

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                                                                                 at com.example.kulde.itechvcet.FilterDialogFragment.onViewCreated(FilterDialogFragment.java:52)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                                 at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                                 at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                                 at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                                 at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                                 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                                 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:152)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5497)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

帮我Pro Devs!

2 个答案:

答案 0 :(得分:0)

检查并在FilterDialogFragment.java中使用。

 string data = "DwRktl1y8st4k11pSxy2tE9kJMiNlIgV6Gu9ekY8ia2QtoGbdiaiemHeQJ+2MGTZmRKM0IGsiXQyqvXLx/t47FcXmwzZPayS3i6mmYD+qFibbcmA5lGI1uIjT7FSgLM9Xi9QBnTMjIwIEmv6tQaKGGTbhwvUuaP7hek57Xnlk+9CCarkkDlGLed5y+6GedXED0KgMcW1rqXLH7EQub+KzQ==";

  byte[] arrb = Convert.FromBase64String(data);
  string decodedString = Encoding.UTF8.GetString(arrb); 

   const string key = "AbcNtByIGI1BpgcsAG8GZl8pdwwxyz";

   RijndaelManaged aes = new RijndaelManaged();
   aes.KeySize = 256;
   aes.BlockSize = 256;
   aes.Padding = PaddingMode.None;
   aes.Mode = CipherMode.ECB;
   aes.GenerateIV();
   ICryptoTransform decryptor = aes.CreateDecryptor(Encoding.UTF8.GetBytes(key), aes.IV);
   MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(decodedString.Trim()));
   CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

 StreamReader sr = new StreamReader(cs);
 user_data = sr.ReadToEnd();

答案 1 :(得分:0)

您正在为FilterDialogFragment设置包值,但您没有使用它,因为您在showDialog()方法中创建了一个新的FilterDialogFragment实例

   FilterDialogFragment filterDialogFragment = new FilterDialogFragment();
    int pos=holder.getAdapterPosition();
    Bundle bundle = new Bundle();
    bundle.putString("more_id",topicses.get(pos).getId() );
    Log.e("rrrr",topicses.get(pos).getId());
                bundle.putString("more_given_by",topicses.get(pos).getGivenby());
    bundle.putString("more_article", topicses.get(pos).getArticle());
    bundle.putString("more_content", topicses.get(pos).getContent());

    //pasing data to filterDialogFragment
    filterDialogFragment.setArguments(bundle);


    showDialog(filterDialogFragment);

传递FilterDialogFragment变量

void showDialog(FilterDialogFragment ff) {
   // MoreDetails moreDetails = new MoreDetails();
    FragmentManager ft = ((MainActivity) context).getSupportFragmentManager();
    DialogFragment newFragment = ff.newInstance();
    newFragment.show(ft, "dialog");
 }

您没有将任何值传递给DialogFragment并尝试检索实际不存在的more_id键的值。

Bundle mArgs = getArguments();
more_id.setText(mArgs.getString("more_id"));

按以下方式更改您的实施

static FilterDialogFragment newInstance(String moreId) {
    FilterDialogFragment f = new FilterDialogFragment();
    Bundle bundle = new Bundle();
    bundle.putSTring("more_id", moreId);
    f.setArguments(bundle);
    return f;
}

并在调用DialogFragment

时相应地更改代码