当我在活动中显示片段时,我正试图从片段中获取JSON列表。 我将展示适配器时遇到问题。
SiswaFragment
public class SiswaFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private List<MenuItem> menuItem;
private ListView listView;
private MenuListAdapter listAdapter;
private String URL_FEED = AppConfig.URL;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public SiswaFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static SiswaFragment newInstance(String param1, String param2) {
SiswaFragment fragment = new SiswaFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_siswa, container, false);
//session = new SessionManager(getActivity().getApplicationContext());
listView = (ListView) view.findViewById(R.id.listSiswa);
menuItem = new ArrayList<MenuItem>();
RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
String no_hp = "085733777717";
String URL = URL_FEED + "/?nomor_hp=" + no_hp;
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with the response
try{
JSONObject o = new JSONObject(response);
JSONArray feedArray = o.getJSONArray("data");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
//Toast.makeText(getApplicationContext(), feedObj.toString(), Toast.LENGTH_LONG).show();
MenuItem item = new MenuItem();
item.setId(feedObj.getString("kode"));
item.setImage(AppConfig.URL_ASSETS + "user.png");
item.setName(feedObj.getString("nama"));
item.setDescription(feedObj.getString("kelas"));
item.setRemarks(feedObj.getString("sekolah"));
menuItem.add(item);
}
//txt.setText(sb.toString());
} catch (JSONException ex){}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
listAdapter = new MenuListAdapter(getActivity(), menuItem);
listView.setAdapter(listAdapter);
// Inflate the layout for this fragment
rq.add(stringRequest );
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
MenuListAdapter
public class MenuListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<MenuItem> menuItem;
private SessionManager session;
private Database db;
private Context context;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public MenuListAdapter(Activity activity, List<MenuItem> menuItem) {
this.activity = activity;
this.menuItem = menuItem;
this.context = activity.getApplicationContext();
}
@Override
public int getCount() {
return menuItem.size();
}
@Override
public Object getItem(int location) {
return menuItem.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
db = new Database(activity.getApplicationContext());
// Session manager
session = new SessionManager(activity.getApplicationContext());
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.menu_item, null);
}
if (imageLoader == null) {
imageLoader = AppController.getInstance().getImageLoader();
}
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView description = (TextView) convertView.findViewById(R.id.description);
NetworkImageView image = (NetworkImageView) convertView.findViewById(R.id.profilePic);
//FeedImageView feedImageView = (FeedImageView) convertView.findViewById(R.id.feedImage1);
final MenuItem item = menuItem.get(position);
name.setText(item.getName());
description.setText(item.getDescription());
image.setImageUrl(item.getImage(), imageLoader);
return convertView;
}
}
我在运行时发现问题,只是显示闪烁(强制关闭)。我认为问题出在listAdapter = new MenuListAdapter(getActivity(), menuItem);
和listView.setAdapter(listAdapter);
。
希望我很清楚,谢谢。
答案 0 :(得分:3)
一切都很好,除了一件事......!
您已在请求之前设置了适配器,并且您的菜单列表为空,因为您还没有从bac-kend收到任何数据。
//just here
listAdapter = new MenuListAdapter(getActivity(), menuItem);
listView.setAdapter(listAdapter);
// Inflate the layout for this fragment
rq.add(stringRequest );
因此,当调用onResponse
回调时,您将获得数据...
您可以在其中添加逻辑,以便在此之前初始化您的适配器并刷新菜单列表并调用notifyDataSetChanged
方法来刷新列表视图。
只需修改您的方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_siswa, container, false);
//session = new SessionManager(getActivity().getApplicationContext());
listView = (ListView) view.findViewById(R.id.listSiswa);
menuItem = new ArrayList<MenuItem>();
RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
String no_hp = "085733777717";
String URL = URL_FEED + "/?nomor_hp=" + no_hp;
listAdapter = new MenuListAdapter(getActivity(), menuItem);
listView.setAdapter(listAdapter);
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with the response
try{
JSONObject o = new JSONObject(response);
JSONArray feedArray = o.getJSONArray("data");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
//Toast.makeText(getApplicationContext(), feedObj.toString(), Toast.LENGTH_LONG).show();
MenuItem item = new MenuItem();
item.setId(feedObj.getString("kode"));
item.setImage(AppConfig.URL_ASSETS + "user.png");
item.setName(feedObj.getString("nama"));
item.setDescription(feedObj.getString("kelas"));
item.setRemarks(feedObj.getString("sekolah"));
menuItem.add(item);
}
//txt.setText(sb.toString());
listAdapter.notifyDataSetChanged();
} catch (JSONException ex){}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
// Inflate the layout for this fragment
rq.add(stringRequest );
return view;
}