选择位置图像将不会显示在ImageView上

时间:2019-07-14 13:31:25

标签: java android imageview appcompatactivity

我有1个int和1个来自意图额外内容的字符串,这些位置的位置我想将其设置为图像视图可绘制的位置。代码正常工作,日志显示正确的位置,但图像视图不会在设备中显示任何内容 有ID吗?

//这是列表项中的意图

Intent inspol = new Intent(getActivity(),Item_Click_open.class);
                inspol.putExtra("IMAGE_SELECT",datassss[position]);
                if (boxcolor1 == 0||boxcolor2 == 0) {
                    inspol.putExtra("IMAGE_TYPE",1);
                }
                if (boxcolor1 == 1||boxcolor2 == 1) {
                    inspol.putExtra("IMAGE_TYPE",2);
                }
                if (boxcolor1 == 2||boxcolor2 == 2) {
                    inspol.putExtra("IMAGE_TYPE",3);
                }





                        new Pair<View, String>(view.findViewById(R.id.big_image),
                                Item_Click_open.VIEW_NAME_HEADER_IMAGE),
                        new Pair<View, String>(view.findViewById(R.id.big_image),
                                Item_Click_open.VIEW_NAME_HEADER_IMAGE2));;
                ActivityCompat.startActivity(getActivity(),inspol,activityOptions.toBundle());
            }
        });

//活动必须将图像视图设置为可绘制的

Intent ilm = getIntent();
    sps = ilm.getStringExtra("IMAGE_SELECT");
    popint = ilm.getIntExtra("IMAGE_TYPE",0);


imsec = (ImageView) findViewById(R.id.imseccc);

if (popint == 1) {
        if (sps == "Beginner") {
            imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_tree_bign));
        }
        if (sps == "Elementary") {
            imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_tree_ele2));
        }
        if (sps == "Intermediate") {
            imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_tree_inter));
        }
        if (sps == "Advance") {
            imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_tree_advice));
        }
    }
    if (popint == 2) {
        if (sps == "Beginner") {
            imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_chery));

        }
        if (sps == "Elementary") {
            imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_limon));
        }
        if (sps == "Intermediate") {
            imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_sbr));
        }
        if (sps == "Advance") {
            imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_oldfood));
        }
    }

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

使用Java的equals方法比较字符串:

if (sps.equals("Beginner") {
    imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_tree_bign));
}
if (sps.equals("Elementary")) {
    imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_tree_ele2));
}
if (sps.equals("Intermediate")) {
    imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_tree_inter));
}
if (sps.equals("Advance")) {
    imsec.setImageDrawable(getResources().getDrawable(R.drawable.ic_tree_advice));
}

答案 1 :(得分:0)

尝试一下:

final String sps = ilm.getStringExtra("IMAGE_SELECT");
int popint = ilm.getIntExtra("IMAGE_TYPE", 0);

if (popint == 1) {
        switch (sps.toLowerCase()){
            case "beginner":
                imsec.setImageDrawable(ContextCompat.getDrawable(Item_Click_open.this, R.drawable.ic_tree_bign));
                break;
            case "elementary":
                imsec.setImageDrawable(ContextCompat.getDrawable(Item_Click_open.this, R.drawable.ic_tree_ele2));
                break;
            case "intermediate":
                imsec.setImageDrawable(ContextCompat.getDrawable(Item_Click_open.this, R.drawable.ic_tree_inter));
                break;
            case "advance":
                imsec.setImageDrawable(ContextCompat.getDrawable(Item_Click_open.this, R.drawable.ic_tree_advice));
                break;

        }
    } else if (popint == 2){
        switch (sps.toLowerCase()){
            case "beginner":
                imsec.setImageDrawable(ContextCompat.getDrawable(Item_Click_open.this, R.drawable.ic_chery));
                break;
            case "elementary":
                imsec.setImageDrawable(ContextCompat.getDrawable(Item_Click_open.this, R.drawable.ic_limon));
                break;
            case "intermediate":
                imsec.setImageDrawable(ContextCompat.getDrawable(Item_Click_open.this, R.drawable.ic_sbr));
                break;
            case "advance":
                imsec.setImageDrawable(ContextCompat.getDrawable(Item_Click_open.this, R.drawable.ic_oldfood));
                break;

        }
    }