具有不同选项的按钮,用于下载不同的图像

时间:2012-03-29 17:59:12

标签: java android

人们!我是Java和Android开发的新手。我正在尝试编写一个简单的代码,以便我可以按下按钮,它会查看具有不同选项的列表。每个选项都下载一个图像,图像来自ftp服务器。问题是下载图像不起作用,它是一个单独的按钮。这意味着我有两个按钮,一个用于查看选项列表,另一个用于下载。除了显示列表外,该选项不会做太多。我想点击选项按钮并选择例如“DICE”并从ftp服务器下载图像。

这是我的代码:

public class HelloWorldActivity extends Activity 
{
    CharSequence[] items = { "Massive", "Dice", "EA"};
    boolean[] itemsChecked = new boolean [items.length];

    ImageView imView;
    String imageUrl = "http://chuuck.blogg.se/images/2009/adriana-lima6_43297496.jpg";
    Random r = new Random();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button bt3 = (Button)findViewById(R.id.get_imagebt);
        bt3.setOnClickListener(getImgListener);

        imView = (ImageView)findViewById(R.id.imView);


        Button btn = (Button) findViewById(R.id.btn_lista);
        btn.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v)
            {
                showDialog(0);
            }

        });
    }


    View.OnClickListener getImgListener = new View.OnClickListener() 
    {

        @Override
        public void onClick(View view) 
        {
            // TODO Auto-generated method stub
            int i = r.nextInt(4);
            downloadFile(imageUrl+"png"+i+".png");
            Log.i("im url", imageUrl+"png"+i+".png");
        }
    };


    @Override
    protected Dialog onCreateDialog(int id)
    {

            switch (id)
            {
                case 0:
                return new AlertDialog.Builder(this)
                        //.setIcon(R.drawable.bbb)
                        .setTitle("Välj klass!")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int whichButton)
                            {
                                Toast.makeText(getBaseContext(),
                                        "OK clicked!", Toast.LENGTH_SHORT).show();
                            }
                        })

                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int whichButton)
                            {
                                Toast.makeText(getBaseContext(),
                                        "Cancel clicked!", Toast.LENGTH_SHORT).show();
                            }
                        })

                        // setMultiChoice(items, itemsChecked)
                        .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() 
                        {
                                    // (boolean isChecked)
                                    @Override
                                    public void onClick(DialogInterface dialog, int whichButton) 
                                    {
                                        //Toast.makeText(getBaseContext(),
                                                //items[which] + (isChecked ? " checked!": " unchecked!"), Toast.LENGTH_SHORT).show();
                                    }

                        }

            )
            .create();

        }
        return null;

    }

    Bitmap  bmImg;
    void downloadFile(String fileUrl)
    {
        URL myFileUrl = null;
        try
        {
            myFileUrl = new URL(fileUrl);
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        try
        {
            HttpURLConnection conn = (HttpURLConnection)myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();

            bmImg = BitmapFactory.decodeStream(is);
            imView.setImageBitmap(bmImg);
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }

}

0 个答案:

没有答案