我不知道为什么创建函数不在类扩展BaseAdapter中

时间:2016-04-22 16:19:38

标签: android listview android-studio baseadapter

我不知道为什么创建函数不在类扩展BaseAdapter中。

我想在自定义列表视图中从图库到图像视图中选择图像。

我尝试添加一个函数" onActivityResult"在功能" getView"没有用。

CustomList.java

public class CustomList extends BaseAdapter {

    public static final int REQUEST_GALLERY = 1;

    Context mContext;
    String[] strText1;

    TextView Text1;
    public static ImageView imgView;

    public CustomList(Context context, String[] strText1) {
        this.mContext = context;
        this.strText1 = strText1;
    }

    public int getCount() {
        return strText1.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater mInflater =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (view == null)
            view = mInflater.inflate(R.layout.custom_listview, parent, false);

        Text1 = (TextView) view.findViewById(R.id.Text1);
        Text1.setText(strText1[position]);

        imgView = (ImageView) view.findViewById(R.id.imgView);
        imgView.setImageResource(R.drawable.ic_NoPhoto);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                ((Activity) mContext).startActivityForResult(Intent.createChooser(intent
                        , "Select Picture"), REQUEST_GALLERY);
            }
        });

        return view;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_GALLERY && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                imgView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

但是我尝试添加一个函数" onActivityResult"到课堂" MainActivity"它有效。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CustomList.REQUEST_GALLERY && resultCode == RESULT_OK) {
            Uri uri = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                CustomList.imgView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

如果我想运作" onActivityResult"在班级" CustomList"。怎么办?

1 个答案:

答案 0 :(得分:0)

CAN' T document.getElementsByTagName("p").length // number of p elements on the page 旨在使用扩展onActivityResult的类,这就是为什么它可以在Activity类中使用。