如何在基础适配器类中使用onActivityResult?

时间:2014-05-21 09:21:17

标签: android baseadapter onactivityresult

我有一个listview,其中包含两个buttons,当点击button时,它应该会打开一个图库。我尝试实现它并在stackoverflow上发布了一些帖子,但我仍然无法得到确切的结果。如何使用listView listview中的按钮打开图库?

我尝试了以下内容并将适配器类用作内部类

MyAdapter adapter=new MyAdapter(getApplicationContext(),videoFileList);

我的适配器类

 private class MyAdapter extends BaseAdapter
        {

         ImageView picture;
         Button imgbtn,videobtn;
         ImageButton play;
            Context context;
            private LayoutInflater inflater;
            private ArrayList<String> videolisty;
            public MyAdapter(Context context,ArrayList<String> videolistx)
            {
                this.context=context;
                inflater = LayoutInflater.from(context);
                videolisty = videolistx;
                Field[] arrayOfField = R.raw.class.getFields();
                for (int i = 0; ; i++)
                {
                  if (i >= arrayOfField.length)
                  {
                    System.out.println("-----------videolist------" +videoFileList);
                    return;
                  }
                  System.out.println("audio files-----" + arrayOfField[i].getName());
                  videoFilename.add(arrayOfField[i].getName());
                  Uri localUri = Uri.parse("android.resource://" + Listmodels.this.getPackageName() + "/" + "R.raw." + arrayOfField[i].getName());
                  System.out.println("--------uri path------" + localUri);
                }

            }

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

            @Override
            public Object getItem(int i)
            {
                return videolisty.get(i);
            }

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

            @Override
            public View getView(int i, View view, ViewGroup viewGroup)
            {
                View v = view;

                final int xx = i;
                if(v == null)
                {
                   v = inflater.inflate(R.layout.clistview, viewGroup, false);
                   v.setTag(R.id.categoryimageView1, v.findViewById(R.id.categoryimageView1));
                   v.setTag(R.id.uploadimg, v.findViewById(R.id.uploadimg));
                   v.setTag(R.id.uploadvideo, v.findViewById(R.id.uploadvideo));
                   v.setTag(R.id.play, v.findViewById(R.id.play));
                }

                picture = (ImageView)v.getTag(R.id.categoryimageView1);
                imgbtn = (Button)v.getTag(R.id.uploadimg);
                videobtn = (Button)v.getTag(R.id.uploadvideo);
                play=(ImageButton)v.getTag(R.id.play);
                imgbtn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                          Activity aa = (Activity)context;
                         aa.startActivityForResult(i, RESULT_LOAD_IMAGE);
                    }





                });

                play.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        System.out.println("YEST"+xx);
                        Intent localIntent = new Intent(Listmodels.this, Videoplay.class);
                        localIntent.putExtra("videopath", videoFileList.get(xx));
                        startActivity(localIntent);
                    }
                });

                /*picture.setImageResource(item.drawableId);
                name.setText(item.name);*/


                return v;
            }

            protected void onActivityResult(int requestCode, int resultCode) {
                super.onActivityResult(requestCode, resultCode, data);

                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };

                    Cursor cursor = getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                   String picturePath = cursor.getString(columnIndex);
                    cursor.close();

                    System.out.println("------data path-------"+picturePath);

                    //filechoose.setText(picturePath);

                }






        }

1 个答案:

答案 0 :(得分:0)

onActivityResult()方法属于Activity。我们不能在java类中使用。