1

我进行了搜索,但在互联网上找不到有用的答案。这就是我提出这个问题的原因。我喜欢在getView自定义 ArrayAdapter 类的方法中加载相机和捕获图像。相机已加载并捕获图像,但从onActivityResult()未被调用。我的代码如下所示。

       public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder viewHolder=new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) this.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(vi==null){
            vi = inflater.inflate(R.layout.list_row, parent, false);
            viewHolder.id=(TextView)vi.findViewById(R.id.title);
            viewHolder.thumbnailImage=(ImageView)vi.findViewById(R.id.list_image);
            viewHolder.arrow=(ImageView)vi.findViewById(R.id.list_arrow);
            vi.setTag(viewHolder);
        }
        else
            viewHolder=(ViewHolder) vi.getTag();



        // Setting all values in listview
        viewHolder.id.setText(listIDs.get(position));
        viewHolder.thumbnailImage.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                ((Activity)context).startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }

        });
        return vi;
    }

   protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
//            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
//            imageView.setImageBitmap(photo);
        }  

    } 

然后自定义适配器类从类中激活ListFragment

customList_Adaptor adapter = new customList_Adaptor(
                getActivity(), R.layout.list_row, listIDs);

如何修改以便调用 onActivityResult() 方法。谢谢

4

3 回答 3

2

首先在必须使用或因为是类的方法的类中创建object你的。在您的情况下,您可以按照以下步骤操作:CustomArrayAdapter classextendsActivityListActivityonActivityForResultoverridedActivity

1)创建一个类

import android.graphics.Bitmap;

public class Items { // You can give any name to this class

    private Bitmap bitmap;

    public Bitmap getBitmap() {
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

}

2)在您的类(使用 Activity 或 ListActivity 扩展)中,您应该创建 CustomArrayAdapter 类的对象,例如:

public class YourClassName extends Activity {\

         public static final int CAMERA_REQUEST = 1; // here integer value 1 must be same                
                                                     // as you gave in CustomAdapterClass

        //First make an array list of the class created above
     ArrayList<Items> camera_pic_array = new ArrayList<Items>();

       // make object of CustomArrayAdapter Class
     CustomArrayAdapter adapter = new  CustomArrayAdapter(this, camera_pic_array);


      /* It is assumed by me that you should have a constructor of your CustomArrayAdapter 
       with two parameters like 
       public CustomArrayAdapter(Context ctx, ArrayList<Items> list){
               this.context = context;
        this.list = list;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
        */

       // now here, write onActivityResult code 


     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case CAMERA_REQUEST: {
            if (resultCode == RESULT_OK) {
                Bitmap bitmap = (Bitmap) data.getExtras().get("data");              
                Items item = new Items();
                item.setBitmap(bitmap);
                camera_pic_array.add(item);
                adapter.notifyDataSetChanged(); //you must notify this 
                                                                // line is very important

            }

            break;
        }
           }
        }

希望这对你有用,我做了同样的事情并且对我来说很好......

于 2013-06-26T06:02:07.787 回答
0

onActivityResult 将在您的活动上调用。不在您的阵列适配器上。

于 2013-06-23T07:57:35.710 回答
0

onActivityResult属于Activity. 您只能在Actvity/ListActivity上下文中覆盖。您可以做的是保留对 ArrayAdapter 子类的引用,在其中创建一个方法以在触发和满足条件Actvity/ListActivity时调用onActivityResult

于 2013-06-23T07:57:41.057 回答