我已经创建了一个Tab活动。将一个片段的值发送到另一个片段时,我在ArrayList中得到空值。我在第二个片段中获取值,但无法将其设置为ArrayList。我正在使用
__add__
在行中显示值。
RecyclerView
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.jar.Attributes;
public class Registration_Details extends Fragment {
RecyclerView recyclerView;
ArrayList list;
List<String> list2;
MyAddapter myAddapter;
Context context;
Bundle bundle;
TextView textView,textView2,textView3;
String name, contact,email;
private Random mRandom = new Random();
private TextView memail,mphone,muser;
View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayValueAdd();
}
private void ArrayValueAdd() {
Bundle bundle = this.getArguments();
if (bundle != null) {
name = bundle.getString("Name");
contact = bundle.getString("Contact");
email = bundle.getString("Email");
}
list = new ArrayList<String>(3);
for (int i = 0; i <= 5; i++) {
//Here unable to getting the value
list.add(name);
list.add(contact);
list.add(email);
//Getting the value in Log.e
Log.e(name, "This is my message");
}
}
@Override
public void onStart() {
super.onStart();
ArrayValueAdd();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.registration_details, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.tv);
myAddapter = new MyAddapter(getActivity(), list, new OnMyItemClickListner() {
@Override
public void onItemClicked(int pos) {
}
}, new OndeleteItem() {
@Override
public void ondelete(int pos) {
Toast.makeText(getActivity(), String.valueOf(name), Toast.LENGTH_LONG).show();
myAddapter.notifyDataSetChanged();
}
}
);
recyclerView.setAdapter(myAddapter);
return rootView;
}
}
import android.os.Bundle;
import android.service.carrier.CarrierMessagingService;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Registration_Tab extends Fragment {
EditText editTextName, editTextContact,editTextEmail;
View rootView;
Button submitButton;
String name,contact,email;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.registration_tab, container, false);
editTextName = (EditText)rootView.findViewById(R.id.editTextName);
editTextContact = (EditText)rootView.findViewById(R.id.editTextContact);
editTextEmail = (EditText)rootView.findViewById(R.id.editTextEmail);
submitButton = (Button) rootView.findViewById(R.id.button);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name = editTextName.getText().toString();
contact = editTextContact.getText().toString();
email = editTextEmail.getText().toString();
Bundle bundle=new Bundle();
bundle.putString("Name",name);
bundle.putString("Contact",contact);
bundle.putString("Email",email);
FragmentTransaction transection=getFragmentManager().beginTransaction();
Registration_Details registration_details=new Registration_Details();
registration_details.setArguments(bundle); //data being send to SecondFragment
transection.replace(R.id.constraintLayout, registration_details);
transection.commit();
Toast.makeText(getActivity(),"Goingg " +name,Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.jar.Attributes;
public class Registration_Details extends Fragment {
RecyclerView recyclerView;
ArrayList list;
List<String> list2;
MyAddapter myAddapter;
Context context;
Bundle bundle;
TextView textView,textView2,textView3;
String name, contact,email;
private Random mRandom = new Random();
private TextView memail,mphone,muser;
View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.registration_details, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
Bundle bundle = this.getArguments();
if (bundle != null) {
name = bundle.getString("Name");
contact = bundle.getString("Contact");
email = bundle.getString("Email");
}
list = new ArrayList<String>();
list.add(name);
list.add(contact);
list.add(email);
Log.e(name, "This is my message");
myAddapter = new MyAddapter(getActivity(), list, new OnMyItemClickListner() {
@Override
public void onItemClicked(int pos) {
}
}, new OndeleteItem() {
@Override
public void ondelete(int pos) {
Toast.makeText(getActivity(), String.valueOf(name), Toast.LENGTH_LONG).show();
myAddapter.notifyDataSetChanged();
}
}
);
recyclerView.setAdapter(myAddapter);
return rootView;
}
}
答案 0 :(得分:1)
问题出在您的列表初始化和方法位置上,
执行以下操作:
input_shape
并仅在片段的list = new ArrayList<String>();
或ArrayValueAdd
中调用onCreateView
方法,现在这些方法以onViewCreated
的生命周期执行,而不是生命周期的执行。 Activity
使得它们永远不会执行,因为Fragment
已通过状态。
也不需要循环,您可以这样做:
Activity
no不循环。
片段不能直接将数据传递到另一个片段,因此您需要它从父list = new ArrayList<String>();
list.add(name);
list.add(contact);
list.add(email);
请查看此答案以获取详细说明:here