我已经制作了第二个片段,其中我收到了第一个片段的值。但问题是我无法将接收设置为第二个片段的textview。
我使用Log.d()
;要检查的方法,但我发现永远不会调用databindholder:
问题1:未调用databindholder
问题2:无法将值设置为第二个片段的文本视图
感谢您的关注!
public class ListTable extends Fragment {
private RecyclerView recyclerView;
public ListTable() {
// Required empty public constructor
}
public static ListTable newInstance(String param1, String param2) {
ListTable fragment = new ListTable();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_table, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
MyAdapter a = new MyAdapter();
// a.notifyDataSetChanged();
recyclerView.setAdapter(a);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
public void receiveValue(String value) {
int result = Integer.parseInt(value);
Bundle bundle = new Bundle();
bundle.putInt("value", result);
ListTable listTable = new ListTable();
listTable.setArguments(bundle);
MyAdapter b=new MyAdapter();
b.notifyDataSetChanged();
// a.notifiedatachanged
Log.d("ashu", "value recived :" + value);
}
public class MyAdapter extends RecyclerView.Adapter<MyDataViewHolder> {
@Override
public MyDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_list_table, parent, false);
return new MyDataViewHolder(view);
}
@Override
public void onBindViewHolder(MyDataViewHolder holder, int position) {
if (getArguments() != null) {
int value1 = getArguments().getInt("value");
Log.d("ashu", "the value received inthe bindholder is " + value1);
holder.myEditText.setText("Table is: " + position * value1);
}
}
@Override
public int getItemCount() {
return 10;
}
}
public class MyDataViewHolder extends RecyclerView.ViewHolder {
public TextView myEditText;
public MyDataViewHolder(View itemView) {
super(itemView);
myEditText = (TextView) itemView.findViewById(R.id.table_value);
}
}
}
答案 0 :(得分:1)
只需添加以下代码:
getFragmentManager.beginTransaction().replace(R.id.holder, fragment2).commit();
它应该添加到你在receiveValue函数中创建第二个片段的对象的地方!
public void receiveValue(String value) {
int result = Integer.parseInt(value);
Bundle bundle = new Bundle();
bundle.putInt("value", result);
ListTable listTable = new ListTable();
listTable.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.table_value_container, listTable).commit();
}
答案 1 :(得分:0)
您不必显式声明一个空构造函数,如果您尚未声明任何其他构造函数,Java将为您执行此操作。
您的整个结构错误,您在ListTable中收到了值,但随后您创建了一个新的ListTable实例!?
您应该做的是让您的活动从第一个片段(通过界面)接收值,并从您的活动中将值直接设置为ListTable(如果您已经有对它的引用)或者使用ListTable.newInstance添加新的ListTable片段();
答案 2 :(得分:0)
使用构造函数将数据列表传递给MyAdapter
并使用该列表获取每个位置的值。像这样......
public class MyAdapter extends RecyclerView.Adapter<MyDataViewHolder> {
private List<YourObject> items;
public MyAdapter(List<YourObject> items) {
this.items = items;
}
@Override
public MyDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_list_table, parent, false);
return new MyDataViewHolder(view);
}
@Override
public void onBindViewHolder(MyDataViewHolder holder, int position) {
YourObject val = items.get(position);
if(val!=null){
//do something
}
}
@Override
public int getItemCount() {
return items.size();
}
}
在片段中,像这样打电话......
List<YourObject> items = ..... (generate anyway you need)
MyAdapter a = new MyAdapter(items);
recyclerView.setAdapter(a);
答案 3 :(得分:0)
未调用Data Binder,因为未正确使用setArguments()。对于您的情况,您只需在适配器中创建一个方法并传递该值。
你应该在像这样的碎片中使用 setArguments
Fragment fragment2 = new FragmentB();
fragment2.setArguments(bundle);
getFragmentManager.beginTransaction().replace(R.id.holder, fragment2).commit();
像这样使用 getArguments
class FragmentB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view, container, false);
Bundle data = getArguments();
if (data != null) {
//TODO - retrieve all your data
}
// Create view
// Create your adapter
return view;
}
}