Skip to content Skip to sidebar Skip to footer

How To Show Listview Itemclick Image Into The Alertbox Using Android Json?

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 part

Solution 1:

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

Solution 2:

publicclassSubMenuextendsAppCompatActivity {

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

        staticStringFLAG="image";
        Integeri=1;

        @OverridepublicvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


            setContentView(R.layout.activity_sub_menu);

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

            StringSelectedId= getIntent().getStringExtra("id");


            getSupportActionBar().setDisplayHomeAsUpEnabled(true);


            // Get the view from listview_main.xml// Execute DownloadJSON AsyncTasknewDownloadJSON().execute();
        }

        // DownloadJSON AsyncTaskprivateclassDownloadJSONextendsAsyncTask<Void, Void, Void> implementsAdapterView.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();// }@Overrideprotected Void doInBackground(Void... params) {
                // Create an array
                arraylist = newArrayList<HashMap<String, String>>();
                // Retrieve JSON Objects from the given URL address
                jsonarray = JsonFunctions
                        .getJSONfromURL("http://cloud.granddubai.com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id"));
                try {
                    // Locate the array name in JSON//                    jsonarray = jsonobject.getJSONArray("main_menu_items");for (inti=0; i < jsonarray.length(); i++) {
                        HashMap<String, String> map = newHashMap<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();
                }
                returnnull;
            }

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


            @OverridepublicvoidonItemClick(AdapterView<?> parent, View view,
                                    int position, long rowId) {


                finalAlertDialogcustomDialog=newAlertDialog.Builder(SubMenu.this).create();
                finalLayoutInflaterinflater= getLayoutInflater();
                finalViewdialogView= inflater.inflate(R.layout.popup
                        , null);
                customDialog.setView(dialogView);
                // set the custom customDialogimation components - text, image and buttonfinalTextViewtvDistance= (TextView) dialogView.findViewById(R.id.h2);
 ImageLoader imageLoader= newImageLoader(dialogView.getContext());
                finalImageViewdesimage= (ImageView) dialogView.findViewById(R.id.desimage);
                imageLoader.DisplayImage(arraylist.get(position).get("image"), desimage);
                //  crust.setOnItemSelectedListener(this);// public void addListenerOnSpinnerItemSelection() {finalButtonok= (Button) dialogView.findViewById(R.id.ok);
                ok.setOnClickListener(newView.OnClickListener() {

                    @OverridepublicvoidonClick(View v) {

                        customDialog.dismiss();


                    }
                });


                finalButtoncncl= (Button) dialogView.findViewById(R.id.canc);
                cncl.setOnClickListener(newView.OnClickListener() {

                    @OverridepublicvoidonClick(View v) {

                        customDialog.dismiss();


                    }
                });


                Button_decrease= (Button) dialogView.findViewById(R.id.incr);
                Button_increase= (Button) dialogView.findViewById(R.id.decr);
                Spinnerspinner1= (Spinner) dialogView.findViewById(R.id.crust);
                Spinnerspinner2= (Spinner) dialogView.findViewById(R.id.adson);
                ArrayList<String> alspinner1 = newArrayList<>();
                ArrayList<String> alspinner2 = newArrayList<>();
                /**
                 * @purpose Array list to show the questions in spinner
                 */final String[] _spvalue1 = getResources().getStringArray(R.array.Crust_array);
                final String[] _spvalue2 = getResources().getStringArray(R.array.AddsOn_array);
                for (inti=0; i < _spvalue1.length; i++) {
                    alspinner1.add(_spvalue1[i]);
                }
                for (inti=0; i < _spvalue2.length; i++) {
                    alspinner2.add(_spvalue2[i]);
                }
                ArrayAdapter<String> adapter1 = newArrayAdapter<>(dialogView.getContext(), android.R.layout.simple_spinner_item, alspinner1);
                ArrayAdapter<String> adapter2 = newArrayAdapter<>(getApplicationContext(), 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);

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


                _decrease.setOnClickListener(newView.OnClickListener() {


                    publicvoidonClick(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(newView.OnClickListener() {

                    @OverridepublicvoidonClick(View v) {
                        String _stringVal;

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


        }


        @OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    onBackPressed();
                    returntrue;
                default:
                    returnsuper.onOptionsItemSelected(item);
            }
        }
    }

It's the solution. Try this.

Solution 3:

hi i gt the solution here:

publicvoidonItemClick(AdapterView<?> parent, View view,
                                int position, long rowId) {

            finalAlertDialogcustomDialog=newAlertDialog.Builder(SubMenu.this).create();
            finalLayoutInflaterinflater= getLayoutInflater();
            finalViewdialogView= inflater.inflate(R.layout.popup
                    , null);
            customDialog.setView(dialogView);
            HashMap<String, String> map =arraylist.get(position);
            ImageViewimage= (ImageView) dialogView.findViewById(R.id.desimage);
            ImageLoaderimageLoader=newImageLoader(SubMenu.this);
            imageLoader.DisplayImage(map.get(SubMenu.FLAG), image);

            //  crust.setOnItemSelectedListener(this);// public void addListenerOnSpinnerItemSelection() {finalButtonok= (Button) dialogView.findViewById(R.id.ok);
            ok.setOnClickListener(newView.OnClickListener() {

                @OverridepublicvoidonClick(View v) {

                    customDialog.dismiss();


                }
            });


            finalButtoncncl= (Button) dialogView.findViewById(R.id.canc);
            cncl.setOnClickListener(newView.OnClickListener() {

                @OverridepublicvoidonClick(View v) {

                    customDialog.dismiss();


                }
            });

Post a Comment for "How To Show Listview Itemclick Image Into The Alertbox Using Android Json?"