android imageview setvisibility

2019-08-20 03:44发布

i want to change visibility of imageview if value= x

but i have error message on eclipse.

here is part of my java

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_PRODUCTS);

                    // looping through All Products
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_PID);
                        String name = c.getString(TAG_NAME);
                        String price = c.getString(TAG_PRICE);
                        String cu = c.getString(TAG_CU);
                        /////////////
                                                ///HERE THE BUG////
                        if (cu == "1"){

                            cu = "oui";
                        }
                        else {  
                            ImageView image_A_wrong = (ImageView) findViewById(R.id.imageView1);
                            image_A_wrong.setVisibility(View.GONE);

                        }

and here my xml file

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/quo100px"
        android:visibility="gone" />

tks advance for your help

3条回答
We Are One
2楼-- · 2019-08-20 04:16

For that you have to check if you are taking string for compression than use .equalsIgnoreCase("") function otherwise use == to compare .

查看更多
时光不老,我们不散
3楼-- · 2019-08-20 04:31

You are checking for a String in which case the .equals("your_constraint") is used.

So, change this:

if (cu == "1") {
    cu = "oui";
} else {
    ImageView image_A_wrong = (ImageView) findViewById(R.id.imageView1);
    image_A_wrong.setVisibility(View.GONE);
}

to this:

if (cu.equals("1")) {
    cu = "oui";
} else {
    ImageView image_A_wrong = (ImageView) findViewById(R.id.imageView1);
    image_A_wrong.setVisibility(View.GONE);
}
查看更多
戒情不戒烟
4楼-- · 2019-08-20 04:35

Use cu.equals("1") rather than cu == "1" and you'll be fine

查看更多
登录 后发表回答