我面临的新的Android开发问题是我无法在相同活动的片段之间传递字符串值 现在让我简要解释一下我的要求我有两个片段片段b和片段c。每当用户从片段c中的微调器中选择值时,我应该传递给片段c中的listview,到目前为止我尝试过:
这是我的Fragment New Requirement片段(片段c):
package com.example.first.servicefirst;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class NewRequirements extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
try {
View view = inflater.inflate(R.layout.fragment_dialog_claim, container,
false);
final Button btnupdate;
btnupdate = (Button) view.findViewById(R.id.update);
final Spinner sbu = (Spinner) view.findViewById(R.id.sbuu);
ArrayAdapter<CharSequence> adaptersbu = ArrayAdapter.createFromResource(
getActivity().getBaseContext(), R.array.newrequirements, R.layout.spinnerlayout);
adaptersbu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sbu.setAdapter(adaptersbu);
final Spinner bu = (Spinner) view.findViewById(R.id.bu);
adapterbu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
bu.setAdapter(adapterbu);
final Spinner sbuu = (Spinner) view.findViewById(R.id.sbu);
ArrayAdapter<CharSequence> adaptersbuu = ArrayAdapter.createFromResource(
getActivity().getBaseContext(), R.array.newrequirements, R.layout.spinnerlayout);
adaptersbuu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sbuu.setAdapter(adaptersbuu);
final Spinner sc = (Spinner) view.findViewById(R.id.sc);
ArrayAdapter<CharSequence> adaptersc = ArrayAdapter.createFromResource(
getActivity().getBaseContext(), R.array.newrequirements, R.layout.spinnerlayout);
adaptersc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sc.setAdapter(adaptersc);
final Spinner ssc = (Spinner) view.findViewById(R.id.ssc);
ArrayAdapter<CharSequence> adapterssc = ArrayAdapter.createFromResource
(getActivity().getBaseContext(), R.array.newrequirements, R.layout.spinnerlayout);
adapterssc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ssc.setAdapter(adapterssc);
setHasOptionsMenu(true);
final String str = sbu.getSelectedItem().toString();
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Bundle bundle = new Bundle();
FragmentManager fm=getFragmentManager();
Add add = new Add();
bundle.putString("yes", str);
Log.i("Bundle", bundle.toString());
Log.v("Add", str);
add.setArguments(bundle);
FragmentTransaction ft=fm.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.replace(R.id.content_frame, add,"hi");
ft.addToBackStack(null);
ft.commit();
}catch (Exception e){
throw e;}
}
});
return view;
}catch (Exception e){
throw e;
}
}
}
这是片段添加(片段B)
package com.example.first.servicefirst;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
public class Add extends Fragment implements View.OnClickListener
{
public static Add()
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_btn_add, container, false);
Spinner ldsource=(Spinner)rootView.findViewById(R.id.lead_source);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity().getBaseContext(), R.array.dropbox1, R.layout.spinnerlayout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ldsource.setAdapter(adapter);
// EditText editText=(EditText)rootView.findViewById(R.id.title);
Spinner ldtype=(Spinner)rootView.findViewById(R.id.ldtype);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
getActivity().getBaseContext(), R.array.dropbox2,R.layout.spinnerlayout);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ldtype.setAdapter(adapter1);
Spinner ldstatus=(Spinner)rootView.findViewById(R.id.ldstatus);
TextView txt=(TextView)rootView.findViewById(R.id.spinnerTarget);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
getActivity().getBaseContext(), R.array.dropbox3, R.layout.spinnerlayout);
adapter2.setDropDownViewResource(android.R.layout.simple_selectable_list_item);
ldstatus.setAdapter(adapter2);
// Bundle bundle=getArguments();
// String good=bundle.getString("sbu");
ArrayList<LdNewsItem> listContact = GetlistContact();
final ListView lv = (ListView)rootView.findViewById(R.id.ldrequirements);
lv.setAdapter(new customListAdapterldrequirements(getActivity(), listContact));
// lv.setOnTouchListener(new View.OnTouchListener() {
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// return false;
// }
// Setting on Touch Listener for handling the touch inside ScrollView
//});
sbu = getArguments().getString("yes");
return rootView;
}
在以下位置获取空指针异常:sbu = getArguments()。getString(“yes”);真的Donno在哪里犯了错误,如果有人帮我提前谢谢你会非常有帮助!
答案 0 :(得分:1)
如果您想在Fragment之间传递数据,可以使用以下方法。
1.您可以使用https://lodash.com/docs#findWhere使用此功能将数据发送到LBM,您可以在目标片段上实现接收器,以监听广播数据。
您可以在源片段和目标片段之间实现回调侦听器。 使用回调侦听器源片段将在回调方法中传递数据,目标片段将侦听它。查找说明LocalBroadcastManager
您可以使用here第三方库在两个片段之间传递数据的简单方法。
答案 1 :(得分:1)
执行此操作的最佳方法是使用Interface
并处理Activity
内的所有片段更改逻辑。我已经快速写了一些代码来告诉你这是如何工作的。我没有包含任何构造函数或Fragment
实例化和填充代码。您必须在Fragment
内保留对Activity
FragmentTransaction
的引用,并在那里处理public class MainActivity extends Activity implements FragmentOneListener {
private FragmentOne fragmentOne;
private FragmentTwo fragmentTwo;
@Override
public void sendDataBack(String data) {
if (fragmentTwo != null) {
fragmentTwo.doSomethingWithData(data);
}
}
}
。
如果您不知道如何做某事,请告诉我,我会尝试将其添加到示例代码中。
<强> MainActivity.java 强>
public class FragmentOne extends Fragment {
private FragmentOneListener fragmentOneListener;
private void sendDataBack(String data) {
if (fragmentOneListener != null) {
fragmentOneListener.sendDataBack(data);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
fragmentOneListener = (FragmentOneListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()+ " must implement FragmentOneListener");
}
}
@Override
public void onDetach() {
super.onDetach();
fragmentOneListener = null;
}
public interface FragmentOneListener {
void sendDataBack(String data);
}
}
FragmentOne.java
public class FragmentTwo extends Fragment {
public void doSomethingWithData(String data) {
}
}
<强> FragmentTwo.java 强>
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg">
<g id="visibility-test">
<rect x="100" y="0" height="140" width="40"/>
<rect x="0" y="100" height="40" width="140" visibility="visible"/>
</g>
</svg>