在DIalog Box中显示图像(使用Volley)

时间:2016-05-06 14:00:55

标签: android listview dialog

我想显示我的图像(使用voley)并在列表视图中点击项目时显示在显示框中,此处下方是截图应用:

此Listview:

Listview

和项目单击

时的此对话框

Dialog Box when item on listview click

我的问题: 单击listview中的项目时如何在对话框中显示图像?

这是我的代码: AbdominalFrgament.java

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class AbdominalFragment extends Fragment implements AdapterView.OnItemClickListener {
    // 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.setTipe(obj.getString("deskripsi"));
                                        exercise.setRating(obj.getDouble("rating"));
                                        //exercise.setRating(((Number)obj.get("rating")).doubleValue());
                                        // Genre is json array
                               /*JSONArray othermuscleArray = obj.getJSONArray("othermuscle");
                                ArrayList<String> othermuscle = new ArrayList<String>();
                                for (int j = 0; j < othermuscleArray.length(); j++) {
                                    othermuscle.add((String) othermuscleArray.get(j));
                                }
                                exercise.setOthermuscle(othermuscle);*/


                                        //movie.setDescribe(obj.getString("describe"));
                                        //movie.setRating(((Number) obj.get("rating"))
                                        //        .doubleValue());


                                /*// Genre is json array
                                JSONArray genreArry = obj.getJSONArray("genre");
                                ArrayList<String> genre = new ArrayList<String>();
                                for (int j = 0; j < genreArry.length(); j++) {
                                    genre.add((String) genreArry.get(j));
                                }
                                movie.setGenre(genre);*/

                                        // adding movie to movies 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();
        }

        listView.setOnItemClickListener(this);
        return rootView;
    }

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

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

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Create custom dialog object
        final Dialog dialog = new Dialog(getContext());
        // Include dialog.xml file
        dialog.setContentView(R.layout.dialog); // layout of your dialog
        // Set dialog title
        dialog.setTitle("Detail");

        // set values for custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.nama);
        text.setText(exerciseList.get(position).getNama());

        //ImageView text1 = (ImageView) dialog.findViewById(R.id.gambar1);
       // text1.setText(exerciseList.get(position).getGambar1());

        TextView text3 = (TextView) dialog.findViewById(R.id.alat);
        text3.setText(exerciseList.get(position).getAlat());

        TextView text4 = (TextView) dialog.findViewById(R.id.mainmuscle);
        text4.setText(exerciseList.get(position).getMainmuscle());
        // similar add statements for other details

        dialog.show();
    }
}

listrow.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_row_selector"
    android:padding="8dp" >

    <!-- Image -->
    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/gambar1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="8dp" />

    <!-- Name -->
    <TextView
        android:id="@+id/nama"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/gambar1"
        android:layout_toRightOf="@+id/gambar1"
        android:textSize="@dimen/title"
        android:textStyle="bold" />

    <!-- Main Muscle -->
    <TextView
        android:id="@+id/mainmuscle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/nama"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/gambar1"
        android:textSize="@dimen/rating" />

    <!-- Rating -->
    <TextView
        android:id="@+id/rating"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/mainmuscle"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/gambar1"
        android:textColor="@color/genre"
        android:textSize="@dimen/genre" />

    <!-- alat -->
    <TextView
        android:id="@+id/alat"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rating"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/gambar1"
        android:textColor="@color/genre"
        android:textSize="@dimen/genre" />

    <!-- tipe -->
    <TextView
        android:id="@+id/tipe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:textColor="@color/year"
        android:textSize="@dimen/year" />

</RelativeLayout>

dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_row_selector"
    android:padding="8dp" >


    <!-- Image -->
    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/gambar1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="8dp" />


    <!-- Name -->
    <TextView
        android:id="@+id/nama"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/gambar1"
        android:layout_toRightOf="@+id/gambar1"
        android:textSize="@dimen/title"
        android:textStyle="bold" />

    <!-- Main Muscle -->
    <TextView
        android:id="@+id/mainmuscle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/nama"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/gambar1"
        android:textSize="@dimen/rating" />

    <!-- Rating -->
    <TextView
        android:id="@+id/rating"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/mainmuscle"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/gambar1"
        android:textColor="@color/genre"
        android:textSize="@dimen/genre" />

    <!-- alat -->
    <TextView
        android:id="@+id/alat"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rating"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/gambar1"
        android:textColor="@color/genre"
        android:textSize="@dimen/genre" />

    <!-- tipe -->
    <TextView
        android:id="@+id/tipe"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textColor="@color/year"
        android:textSize="@dimen/year"
        android:layout_below="@id/alat"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

根据您的代码参考Link,使用CustomListAdapter

中的以下内容设置图像
NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail);
// thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

您也可以使用缩略图URL在Dialog中进行类似的操作。您必须将此缩略图从ListView传递到Dialog

您也可以为Dialog图像视图提供不同的ID,并将NetworkImageView加载到Dialog中的新组件

答案 1 :(得分:0)

更改您的dialog.xml NetworkImageView id并重试。

希望以下链接可以帮助您。

Try this