如何使用Android json将listview itemclick图像显示到警报框中? [英] how to show listview itemclick image into the alertbox using android json?

查看:59
本文介绍了如何使用Android json将listview itemclick图像显示到警报框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ListView个项目来自带有图像的服务器,现在的问题是,如果我单击ListView中的任何项目,警报框将打开,并且在该警报框中该特定图像也应存在,例如我单击起司比萨饼,这样它将打开一个警告对话框,并且在该框中显示与ListView相同的图像.

这是我的子菜单活动:

public class SubMenu extends AppCompatActivity {

    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "id";
    static String COUNTRY = "name";

    static String FLAG = "image";
    Integer i = 1;

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


        setContentView(R.layout.activity_sub_menu);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        String SelectedId = getIntent().getStringExtra("id");


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // Get the view from listview_main.xml

        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> implements AdapterView.OnItemClickListener {

        // @Override
        //  protected void onPreExecute() {
        //  super.onPreExecute();
        // Create a progressdialog
        //   mProgressDialog = new ProgressDialog(SubMenu.this);
        // Set progressdialog title
        //   mProgressDialog.setTitle("Categories of Main categories.....");
        // Set progressdialog message
        //  mProgressDialog.setMessage("Loading...");
        //  mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        //  mProgressDialog.show();
        // }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonarray = JsonFunctions
                    .getJSONfromURL("http://cloud......com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id"));
            try {
                // Locate the array name in JSON
//                    jsonarray = jsonobject.getJSONArray("main_menu_items");


                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();

                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    // map.put("id", jsonobject.getString("id"));
                    map.put("name", jsonobject.getString("name"));

                    map.put("image", jsonobject.getString("image"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.list1);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(SubMenu.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            listview.setOnItemClickListener(this);
            // Close the progressdialog
            // mProgressDialog.dismiss();
        }


        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long rowId) {


            final AlertDialog customDialog = new AlertDialog.Builder(SubMenu.this).create();
            final LayoutInflater inflater = getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.popup
                    , null);
            customDialog.setView(dialogView);
            // set the custom customDialogimation components - text, image and button
            final TextView tvDistance = (TextView) dialogView.findViewById(R.id.h2);

            //  crust.setOnItemSelectedListener(this);
// public void addListenerOnSpinnerItemSelection() {


            final Button ok = (Button) dialogView.findViewById(R.id.ok);
            ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    customDialog.dismiss();


                }
            });


            final Button cncl = (Button) dialogView.findViewById(R.id.canc);
            cncl.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    customDialog.dismiss();


                }
            });


            Button _decrease = (Button) dialogView.findViewById(R.id.incr);
            Button _increase = (Button) dialogView.findViewById(R.id.decr);

            Spinner spinner1 = (Spinner) dialogView.findViewById(R.id.crst);

            Spinner spinner2 = (Spinner) dialogView.findViewById(R.id.adson);
            ArrayList<String> alspinner1 = new ArrayList<>();
            ArrayList<String> alspinner2 = new ArrayList<>();


            final String[] _spvalue1 = getResources().getStringArray(R.array.Crust_array);
            final String[] _spvalue2 = getResources().getStringArray(R.array.AddsOn_array);
            for (int i = 0; i < _spvalue1.length; i++) {
                alspinner1.add(_spvalue1[i]);
            }
            for (int i = 0; i < _spvalue2.length; i++) {
                alspinner2.add(_spvalue2[i]);
            }
            ArrayAdapter<String> adapter1 = new ArrayAdapter<>(dialogView.getContext(), android.R.layout.simple_spinner_item, alspinner1);
            ArrayAdapter<String> adapter2 = new ArrayAdapter<>(dialogView.getContext(), android.R.layout.simple_spinner_item, alspinner2);

            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(adapter1);
            spinner2.setAdapter(adapter2);

            final TextView _value = (TextView) dialogView.findViewById(R.id.value);
            i = Integer.parseInt(_value.getText().toString());


            _decrease.setOnClickListener(new View.OnClickListener() {


                public void onClick(View v) {
                    String _stringVal;
                    Log.d("src", "Decreasing value...");
                    if (i > 0) {
                        i = i - 1;
                        _stringVal = String.valueOf(i);
                        _value.setText(_stringVal);
                    } else {
                        Log.d("src", "Value can't be less than 0");
                    }

                }
            });

            _increase.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String _stringVal;

                    Log.d("src", "Increasing value...");
                    i = i + 1;
                    _stringVal = String.valueOf(i);
                    _value.setText(_stringVal);
                }
            });
            customDialog.show();
        }


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

图片应该放在此处, 演示图片 这是我的列表视图适配器:

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
                           ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView id;
        TextView name;
        TextView population;
        ImageView image;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.list_item1, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
       // id = (TextView) itemView.findViewById(R.id.idq);
        name = (TextView) itemView.findViewById(R.id.type1);


        // Locate the ImageView in listview_item.xml
        image = (ImageView) itemView.findViewById(R.id.subimg);

        // Capture position and set results to the TextViews
      //  id.setText(resultp.get(SubMenu.RANK));
        name.setText(resultp.get(SubMenu.COUNTRY));

        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(SubMenu.FLAG), image);
        // Capture ListView item click
        /**itemView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
        // Get the position
        resultp = data.get(position);
        Intent intent = new Intent(context, SingleItemView.class);
        // Pass all data rank
        intent.putExtra("rank", resultp.get(MainActivity.RANK));
        // Pass all data country
        intent.putExtra("country", resultp.get(MainActivity.COUNTRY));
        // Pass all data population
        intent.putExtra("population",resultp.get(MainActivity.POPULATION));
        // Pass all data flag
        intent.putExtra("flag", resultp.get(MainActivity.FLAG));
        // Start SingleItemView Class
        context.startActivity(intent);

        }
        });*/
        return itemView;
    }
}

这是实际图像:

解决方案

如何使用Android将ListView ItemClick图像显示到警报框中 json?

onItemClick使用positionarraylist获取被点击的行HashMap:

HashMap<String, String> map =arraylist.get(position);

从地图中获取单击的行名和图像URL,并像在ListViewAdaptergetView()方法中一样在AlertDialog中显示它.

i am having ListView where items are coming from server with images, now the problem is that if i click any item from ListView the alertbox will open and in that alertbox that particular image should also be there for example if i click cheese pizza so it will open an alert dialog box and it will show the same image in that box which is in ListView.

here is my submenu activity:

public class SubMenu extends AppCompatActivity {

    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "id";
    static String COUNTRY = "name";

    static String FLAG = "image";
    Integer i = 1;

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


        setContentView(R.layout.activity_sub_menu);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        String SelectedId = getIntent().getStringExtra("id");


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // Get the view from listview_main.xml

        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> implements AdapterView.OnItemClickListener {

        // @Override
        //  protected void onPreExecute() {
        //  super.onPreExecute();
        // Create a progressdialog
        //   mProgressDialog = new ProgressDialog(SubMenu.this);
        // Set progressdialog title
        //   mProgressDialog.setTitle("Categories of Main categories.....");
        // Set progressdialog message
        //  mProgressDialog.setMessage("Loading...");
        //  mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        //  mProgressDialog.show();
        // }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonarray = JsonFunctions
                    .getJSONfromURL("http://cloud......com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id"));
            try {
                // Locate the array name in JSON
//                    jsonarray = jsonobject.getJSONArray("main_menu_items");


                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();

                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    // map.put("id", jsonobject.getString("id"));
                    map.put("name", jsonobject.getString("name"));

                    map.put("image", jsonobject.getString("image"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.list1);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(SubMenu.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            listview.setOnItemClickListener(this);
            // Close the progressdialog
            // mProgressDialog.dismiss();
        }


        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long rowId) {


            final AlertDialog customDialog = new AlertDialog.Builder(SubMenu.this).create();
            final LayoutInflater inflater = getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.popup
                    , null);
            customDialog.setView(dialogView);
            // set the custom customDialogimation components - text, image and button
            final TextView tvDistance = (TextView) dialogView.findViewById(R.id.h2);

            //  crust.setOnItemSelectedListener(this);
// public void addListenerOnSpinnerItemSelection() {


            final Button ok = (Button) dialogView.findViewById(R.id.ok);
            ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    customDialog.dismiss();


                }
            });


            final Button cncl = (Button) dialogView.findViewById(R.id.canc);
            cncl.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    customDialog.dismiss();


                }
            });


            Button _decrease = (Button) dialogView.findViewById(R.id.incr);
            Button _increase = (Button) dialogView.findViewById(R.id.decr);

            Spinner spinner1 = (Spinner) dialogView.findViewById(R.id.crst);

            Spinner spinner2 = (Spinner) dialogView.findViewById(R.id.adson);
            ArrayList<String> alspinner1 = new ArrayList<>();
            ArrayList<String> alspinner2 = new ArrayList<>();


            final String[] _spvalue1 = getResources().getStringArray(R.array.Crust_array);
            final String[] _spvalue2 = getResources().getStringArray(R.array.AddsOn_array);
            for (int i = 0; i < _spvalue1.length; i++) {
                alspinner1.add(_spvalue1[i]);
            }
            for (int i = 0; i < _spvalue2.length; i++) {
                alspinner2.add(_spvalue2[i]);
            }
            ArrayAdapter<String> adapter1 = new ArrayAdapter<>(dialogView.getContext(), android.R.layout.simple_spinner_item, alspinner1);
            ArrayAdapter<String> adapter2 = new ArrayAdapter<>(dialogView.getContext(), android.R.layout.simple_spinner_item, alspinner2);

            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(adapter1);
            spinner2.setAdapter(adapter2);

            final TextView _value = (TextView) dialogView.findViewById(R.id.value);
            i = Integer.parseInt(_value.getText().toString());


            _decrease.setOnClickListener(new View.OnClickListener() {


                public void onClick(View v) {
                    String _stringVal;
                    Log.d("src", "Decreasing value...");
                    if (i > 0) {
                        i = i - 1;
                        _stringVal = String.valueOf(i);
                        _value.setText(_stringVal);
                    } else {
                        Log.d("src", "Value can't be less than 0");
                    }

                }
            });

            _increase.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String _stringVal;

                    Log.d("src", "Increasing value...");
                    i = i + 1;
                    _stringVal = String.valueOf(i);
                    _value.setText(_stringVal);
                }
            });
            customDialog.show();
        }


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

here on top the image should come, thts a demo image here is my listview adapter:

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
                           ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView id;
        TextView name;
        TextView population;
        ImageView image;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.list_item1, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
       // id = (TextView) itemView.findViewById(R.id.idq);
        name = (TextView) itemView.findViewById(R.id.type1);


        // Locate the ImageView in listview_item.xml
        image = (ImageView) itemView.findViewById(R.id.subimg);

        // Capture position and set results to the TextViews
      //  id.setText(resultp.get(SubMenu.RANK));
        name.setText(resultp.get(SubMenu.COUNTRY));

        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(SubMenu.FLAG), image);
        // Capture ListView item click
        /**itemView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
        // Get the position
        resultp = data.get(position);
        Intent intent = new Intent(context, SingleItemView.class);
        // Pass all data rank
        intent.putExtra("rank", resultp.get(MainActivity.RANK));
        // Pass all data country
        intent.putExtra("country", resultp.get(MainActivity.COUNTRY));
        // Pass all data population
        intent.putExtra("population",resultp.get(MainActivity.POPULATION));
        // Pass all data flag
        intent.putExtra("flag", resultp.get(MainActivity.FLAG));
        // Start SingleItemView Class
        context.startActivity(intent);

        }
        });*/
        return itemView;
    }
}

here is the actual image:

解决方案

how to show listview itemclick image into the alertbox using android json?

onItemClick use position to get clicked row HashMap from arraylist :

HashMap<String, String> map =arraylist.get(position);

Get clicked row name and image URL from map and show it in AlertDialog as doing in getView() method of ListViewAdapter

这篇关于如何使用Android json将listview itemclick图像显示到警报框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆