开发者

how to show a image in a image view, chosen from an gridview.GridView is within in alert box

开发者 https://www.devze.com 2023-04-12 19:57 出处:网络
i am havin an activity which consist of a clickable imageview(with title \"choose your image\").Some thing like this:

i am havin an activity which consist of a clickable imageview(with title "choose your image").Some thing like this:

setContentView(R.layout.main);
        ImageView button = (ImageView)findViewById(R.id.categories);
        button.setClickable(true);

now by clicking the image view a popup/alert opens consisting of a grid view form by images from my drawable folder.Something like this:

protecte开发者_如何学Cd Dialog onCreateDialog(int id) {   

        switch(id) {   

        case CATEGORY_ID:   

         AlertDialog.Builder builder;   
            Context mContext = this;   
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);   
            View layout = inflater.inflate(R.layout.categorydialog,(ViewGroup) findViewById(R.id.layout_root));   
            GridView gridview = (GridView)layout.findViewById(R.id.gridview);   
            gridview.setAdapter(new ImageAdapter(this));   

            gridview.setOnItemClickListener(new OnItemClickListener()   
            {
                public void onItemClick(AdapterView<?> parent, View v,int position, long id) {   
                 Toast.makeText(v.getContext(), "Position is "+position, 3000).show();
                 }   
            });

            ImageView close = (ImageView) layout.findViewById(R.id.close);
            close.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v){
              dialog.dismiss();
            }
            });

            builder = new AlertDialog.Builder(mContext);   
            builder.setView(layout);   
            dialog = builder.create();   
            break;   
        default:   
            dialog = null;   
        }   
        return dialog;   
    }

i am able to click these image.

Problem:

i want to choose one from these images and set it in the image view which user had clicked. any suggestios! Thanks!!


Convert your ImageView button to a field

public class YourActivityName extends Activity
{
    private ImageView mImageView;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mImageView = (ImageView)findViewById(R.id.categories);
        mImageView.setClickable(true);
    }

}

Then in your gridview's onItemClick you can call mImageView.setImageBitmap (or setImageDrawable.. whichever of the setImage..() methods you need).

gridview.setOnItemClickListener(new OnItemClickListener()   
{
    public void onItemClick(AdapterView<?> parent, View v,int position, long id) {   
          Bitmap bmp = ...; //get your image at this position somehow
          mImageView.setImageBitmap(bmp);
    }   
});

You'll need to get the image at the position which you clicked, but I will leave that to you since I don't know how your ImageAdapter works.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号