How to show arraylist in AlertDialog

后端 未结 3 1596
迷失自我
迷失自我 2021-01-25 10:32

I have a question, how to show this array item_detail to AllertDialog?

    //Beri keterangan untuk arrayitem detail
    ItemDetail item_detail = new ItemDetail()         


        
3条回答
  •  星月不相逢
    2021-01-25 11:05

    Just put below code in your project & Put your images in drawable folder according to your requirement. That's it. You are done here:

    Java Code:

    public class SOF_AlertDialog extends Activity 
    {
        ArrayList image_detail;
        private int[] images={R.drawable.ball1,R.drawable.ball2,R.drawable.ball5};
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sof__alert_dialog);
    
            image_detail = GetSearchResult();
    
            final ListView lv1 = (ListView) findViewById(R.id.listV_main);
            lv1.setAdapter(new ItemListBaseAdapter());
    
            lv1.setOnItemClickListener(new OnItemClickListener() 
            {
                @Override
                public void onItemClick(AdapterView a, View view, int position, long id)
                {
                    AlertDialog.Builder mAlert = new AlertDialog.Builder(SOF_AlertDialog.this);
                    mAlert.setIcon(image_detail.get(position).getImageNumber()).
                    setTitle(image_detail.get(position).getName()).
                    setMessage(image_detail.get(position).getItemDescription()+"\nPrice: "+image_detail.get(position).getPrice()).
                    setPositiveButton("Buy", new DialogInterface.OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    }).
                    setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                    {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    }).create().show();
                }
             });
           }
    
        private ArrayList GetSearchResult() 
        {
            // TODO Auto-generated method stub
            ArrayList result = new ArrayList();
    
            //Beri keterangan untuk arrayitem detail
            ItemDetail item_detail = new ItemDetail();
            item_detail.setName("Cake");
            item_detail.setItemDescription("");
            item_detail.setPrice("5.000");
            item_detail.setImageNumber(images[0]);
            result.add(item_detail);
    
            item_detail = new ItemDetail();
            item_detail.setName("Coklat");
            item_detail.setItemDescription("Bulat");
            item_detail.setPrice("5.500");
            item_detail.setImageNumber(images[1]);
            result.add(item_detail);
    
            item_detail = new ItemDetail();
            item_detail.setName("Purple");
            item_detail.setItemDescription("Cupcake");
            item_detail.setPrice("6.000");
            item_detail.setImageNumber(images[2]);
            result.add(item_detail);
    
            return result;
        }
    
        private class ItemListBaseAdapter extends BaseAdapter
        {
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return image_detail.size();
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
                    convertView = getLayoutInflater().inflate(R.layout.custom_row, parent, false);
                    TextView txtName = (TextView)convertView.findViewById(R.id.textView1);
                    TextView txtDescription = (TextView)convertView.findViewById(R.id.textView2);
                    TextView txtPrice = (TextView)convertView.findViewById(R.id.textView3);
                    ImageView img = (ImageView)convertView.findViewById(R.id.imageView1);
    
                    txtName.setText(image_detail.get(position).getName());
                    txtDescription.setText(image_detail.get(position).getItemDescription());
                    txtPrice.setText(image_detail.get(position).getPrice());
                    img.setImageResource(image_detail.get(position).getImageNumber());
    
                return convertView;
            }
    
        }
    }
    

提交回复
热议问题