如何从列表视图中显示详细信息项

时间:2016-05-03 04:01:47

标签: android listview android-fragments

每当我点击listview中的项目时,任何人都知道如何在其他片段细节中显示细节项目?以及如何设置它们?谁有这方面的例子?

使用对话框或片段显示详细信息更好?

enter image description here 在listview中只显示: - Nama,gambar1,tipe,mainmuscle,othermuscle,alat,详情评级我要展示的片段: -Nama,gambar1,tipe,mainmuscle,othermuscle,alat,rating, deskripsi,gambar2,gambar3,gambar4

这里是我的代码:

Fragment.java

    public class AbdominalFragment extends Fragment {
    // Log tag
    private static final String TAG = AbdominalFragment.class.getSimpleName();

    // Movies json url
    private static final String url = "http.......";
    private ProgressDialog pDialog;
    private List<Exercise> exerciseList = new ArrayList<Exercise>();
    private ListView listView;
    private CustomListAdapter adapter;

    public AbdominalFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_list, container, false);
        // Inflate the layout for this fragment

        final ListView listView = (ListView) rootView.findViewById(R.id.list);
        adapter = new CustomListAdapter(getActivity(), exerciseList);
        listView.setAdapter(adapter);

        pDialog = new ProgressDialog(getActivity());
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        if(exerciseList.isEmpty()) {
            // Creating volley request obj
            JsonArrayRequest exerciseReq = new JsonArrayRequest(url,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            Log.d(TAG, response.toString());
                            hidePDialog();

                            exerciseList.clear();

                            // Parsing json
                            for (int i = 0; i < response.length(); i++) {
                                try {

                                    JSONObject obj = response.getJSONObject(i);
                                    Exercise exercise = new Exercise();
                                    if (obj.getString("tipe").equals("abdominal")) {
                                        exercise.setNama(obj.getString("nama"));
                                        exercise.setGambar1(obj.getString("gambar1"));
                                        exercise.setTipe(obj.getString("tipe"));
                                        exercise.setMainmuscle(obj.getString("mainmuscle"));
                                        exercise.setAlat(obj.getString("alat"));
                                        exercise.setTipe(obj.getString("othermuscle"));
                                        exercise.setRating(obj.getDouble("rating"));


                                        // adding exercise to exercise array
                                        exerciseList.add(exercise);
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }

                            // notifying list adapter about data changes
                            // so that it renders the list view with updated data
                            adapter.notifyDataSetChanged();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }
            });

            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(exerciseReq);
        }else{
            hidePDialog();
        }
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

1 个答案:

答案 0 :(得分:1)

当然片段更好,因为它们适应屏幕的大小,例如,如果它的平板电脑,它将并排显示列表和细节,否则在点击列表的电话中将出现详细屏幕。

请点击此链接。

http://mobileappdocs.com/android.html