我必须将我从数据库获取的回收站视图项传递给另一个片段。
public class FragLoad extends Fragment {
EditText g_type;
EditText l_capacity, date, quotation;
Button search;
RecyclerView recyclerView;
List<RPostLoad> rPostLoads;
SupportPlaceAutocompleteFragment source,destination;
private static final String URL_RecyclerLoad="http://192.168.43.38/internship_project/android_web_services/RecyclerLoad.php";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_load, container, false);
g_type=view.findViewById(R.id.goodstype);
l_capacity=view.findViewById(R.id.loadCap);
date=view.findViewById(R.id.txtDate);
quotation=view.findViewById(R.id.txtQuotations);
search=view.findViewById(R.id.postSearch);
recyclerView=view.findViewById(R.id.rvload);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setHasFixedSize(true);
rPostLoads=new ArrayList<>();
source= (SupportPlaceAutocompleteFragment) getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment2);
destination= (SupportPlaceAutocompleteFragment) getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment1);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rvLoad();
}
});
return view;
}
private void rvLoad() {
Map<String, String> params = new HashMap<>();
params.put("g_type", g_type.getText().toString());
AndroidNetworking.post("http://192.168.43.38/internship_project/android_web_services/RecyclerLoad.php").addBodyParameter(params).setTag("Login").setPriority(Priority.LOW).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("TAG", "Response" + response.toString());
JSONArray array=response.getJSONArray("Data");
for (int i=0;i<array.length();i++){
JSONObject product=array.getJSONObject(i);
rPostLoads.add(new RPostLoad(
product.getString("owner_name"),
product.getString("g_type"),
product.getString("truck_cap")
));
}
RPostLoadAdp adp=new RPostLoadAdp(getActivity(),rPostLoads);
recyclerView.setAdapter(adp);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(ANError anError) {
Toast.makeText(getActivity(), "Server time out please try later", Toast.LENGTH_SHORT).show();
}
});
}
}
上面的代码是从我从数据库获取回收器项目的类,需要将回收器视图项发送到另一个片段。问题是我是android的初学者所以我不知道如何发送它。我必须收到回收商项目的片段是空的,所以我没有在这里发布。
答案 0 :(得分:1)
您有一个Activity
一个Fragment
和一个RecyclerView
和yor Activity
打开Fragment
并在片段内部实施RecylcerView
要在Activity
和RecyclerView
之间进行沟通,您需要Listener
创建此Interface
(监听器):
public interface OnSomeThingHappens {
public void onEvent(); // you can use some argument for the method
}
然后在你的Activity中实现接口并为你的片段创建一个方法,将侦听器从activity传递给片段,然后将它从片段传递给RecyclerView
的适配器。你的活动将是这样的事情:
public class ActivityMain extends AppCompatActivity implements OnSomeThingHappens{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YourFragment yourFragment = (YourFragment) getFragmentManager().findFragmentById(R.id.test_fragment);
yourFragment.setOnSomeThingListener(this); // this indication the listener that is implemented in ActivityMain
}
@Override
public void onEvent() {
// to do your logic
}
}
并在Fragment
中实现RPostLoadAdp
时,使用适配器中的setter方法将侦听器对象适配器发送到适配器。
然后您将有权访问侦听器,并且在您需要时可以调用onEvent()
侦听器方法,并在ActivityMain
中调用该方法。
现在,您可以在ActivityMain
中的此方法中执行逻辑(将一些数据发送到其他片段或..)
希望这有帮助。