我有片段可以进行搜索并从服务器获取结果 并将结果发送到带有列表视图的另一个片段 为了展示它。结果显示为空列表,不显示任何内容 片段搜索
public class Search extends Fragment implements View.OnClickListener {
EditText Drugname;
Button SendDrug;
private ProgressDialog plog;
private List<SearchAvailDrug> drugreq= new ArrayList<SearchAvailDrug> () ;
private CustomListAdapter adapter;
public Search() {
// Required empty public constructor
}
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_search, container, false);
Drugname=(EditText) view.findViewById(R.id.searchdrug);
SendDrug =(Button) view.findViewById(R.id.startdrug) ;
adapter = new CustomListAdapter(this, drugreq);
SendDrug.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
SendDataUp(Drugname.getText().toString());
}
@Override
public void onDetach() {
super.onDetach();
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
private void SendDataUp(final String Drugname){
Log.i("SendDataUp","Start");
String url = Urls.URL_AvailDrug;
//showing dialog box
plog = new ProgressDialog(getActivity());
plog.setIndeterminate(false);
plog.setMessage("Registration Please wait ...");
plog.show();
// Start Using Volley
// Request a string response
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onResponse(String response) {
hidePDialog();
String UserIdNum=null;
Log.v("Search",response);
try {
if (response.equalsIgnoreCase(String.valueOf(false)))
{
hidePDialog();
;
}
else
{
for (int i = 0; i < response.length(); i++) {
JSONArray RideJArray = new JSONArray(response);
SearchAvailDrug ride = new SearchAvailDrug(); ride.setDrugBrand(jpart.getString("BrandName")); ride.setPharmcyNameAr(jpart.getString("phname_ar")); ride.setPharmcyNameEn(jpart.getString("phname_ar"));
ride.setPhinfo(jpart.getString("info")); ;
ride.setLang(jpart.getDouble("lang"));
ride.setLat(jpart.getDouble("lat"));
drugreq.add(ride);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
Bundle args = new Bundle();
args.putParcelableArrayList("mylist", (ArrayList<? extends Parcelable>) drugreq);
Fragment fragment = new Listpage();
fragment.setArguments(args);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_nav_main, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
// shownextpage();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hidePDialog();
// Error handling
System.out.println("Something went wrong!");
error.printStackTrace();
Toast.makeText(getActivity(), "FAILED TO CONNECT",Toast.LENGTH_LONG).show();
}
})
{
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("BrandName",Drugname);
return params;
}
};
// Add the request to the queue
int socketTimeout = 30000;//30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
Volley.newRequestQueue(getActivity()).add(stringRequest);
}
private void shownextpage(){
Bundle args = new Bundle();
Fragment fragment = new Listpage();
fragment.setArguments(args);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_nav_main, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
private void hidePDialog() {
if (plog != null) {
plog.dismiss();
plog = null;
}
}
}
并将其显示在另一个片段列表视图中 这是代码
public class Listpage extends Fragment {
private ListView listView;
private CustomListAdapter adapter;
private List<SearchAvailDrug> drugreq= new ArrayList<SearchAvailDrug>() ;
public Listpage() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_Listpage_list, container, false);
Bundle args = getArguments();
ArrayList<SearchAvailDrug>arrayList;
// arrayList = (ArrayList<SearchAvailDrug>)args.getParcelableArrayList("mylist");
drugreq = args.getParcelableArrayList("mylist");
adapter = new CustomListAdapter(this, drugreq);
listView = (ListView) view.findViewById(R.id.listpharmcy);
listView.setAdapter(adapter);
return view;
}
}
问题显示给我空列表视图 在我加载片段之后
答案 0 :(得分:0)
在处理片段和参数时,您应遵循“ newInstance”建议,否则,Android OS可能会使用空参数重新创建实例。