-1

I was just wondering if any of you know how to do this. I have an activity that has a key value pair and I am trying to display an image if the key is equal to a string else don't show any image.

Does anyone know how or where to start with trying to get this to work right?

 final String splitedDouble[] = companyString.split(",,");

    String[] arrayOfString = { "Beard Vape Co;beard_vape_co", "two;zwei", "three;drei" };

    Hashtable<String, String> hashTable = new Hashtable<String, String>();
    for(String s: arrayOfString){
        String[] array = s.split(";");
        String sKey ="", sValue="";
        if(array.length > 1){
            sKey = array[0]; sValue = array[1];
            hashTable.put(sKey, sValue);
            if(sKey.toString().equals(liquidCompany)){
                imageView.setImageResource(Integer.parseInt("R.drawable." + sValue.toString()));
            }
        }

This is what I have for the loop

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Aaron B
  • 461
  • 1
  • 5
  • 21
  • Add some code to show what you are doing. BTW you can use View.setVisibility() to show/ hide your ImageView. – Balkrishna Rawool Jun 19 '15 at 22:20
  • okay sounds good sorry about that I got the loop up that Im trying to make work but so far no luck – Aaron B Jun 19 '15 at 22:46
  • Ok. What's the problem with it? – Balkrishna Rawool Jun 19 '15 at 22:49
  • Im not sure to be honest. What I am trying to do is with the arrayOfStrings as a key value pair so then I can pass the sValue to an Imageview for display if the title of the page is in this instance Beard Vape Co. but its not displaying the images at all – Aaron B Jun 19 '15 at 22:56
  • Proper use of stackoverflow: At first, you just had an overly vague question. Should have been posted on some forum somewhere, not on a stackexchange site. Then you had a buggy piece of code that you didn't know why it didn't work. Still belongs on some forum, not on stackoverflow. The time to post on stackoverflow, is *after* you've worked on the code long enough to *pinpoint* a place where some feature of the language or OS doesn't work the way you think it should. Then you would have a *specific* question, whose answer *would be useful to others*. That's the intent of stackexchange. – ToolmakerSteve Jan 06 '17 at 18:11

4 Answers4

0

If you already have your layout with the ImageView set, you can use View.setVisibility to show or hide the ImageView:

ImageView image = findViewById(R.id.image);
if (key.equals("String"))    
    image.setVisibility(View.VISIBLE);
else
    image.setVisibility(View.GONE);

If you want to change your image after you've inflated your layout, you can use

image.setImageResource(R.drawable.whatever);
Johann Bauer
  • 2,488
  • 2
  • 26
  • 41
0

This is classic case of XY problem. Your real problem is to get resource id from string. which has been answered many time before on SO. link

Try this:

final String splitedDouble[] = companyString.split(",,");

String[] arrayOfString = { "Beard Vape Co;beard_vape_co", "two;zwei", "three;drei" };


Hashtable<String, String> hashTable = new Hashtable<String, String>();
for(String s: arrayOfString){
    String[] array = s.split(";");
    String sKey ="", sValue="";
    if(array.length > 1){
        sKey = array[0]; sValue = array[1];
        hashTable.put(sKey, sValue);
        imageView.setImageResource(getResId("R.drawable." + sValue.toString(), Drawable.class));
        imageView.setVisiblity(View.GONE);
        if(sKey.toString().equals(liquidCompany)){
            imageView.setVisiblity(View.VISIBLE);
        }
    }

public static int getResId(String resName, Class<?> c) {
    try {
        Field idField = c.getDeclaredField(resName);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}
Community
  • 1
  • 1
Balkrishna Rawool
  • 1,865
  • 3
  • 21
  • 35
  • With this when I put it in to my android studios there were quite a few things that I could not resolve or import – Aaron B Jun 19 '15 at 23:11
  • True. This isn't something I tested. Also I haven't added any imports. So I can imagine it had some unresolved symbols. But I hope it gave you enough information to point out the problem and arrive at solution. – Balkrishna Rawool Jun 20 '15 at 09:49
0

The reason your image didn't show is because here

imageView.setImageResource(Integer.parseInt("R.drawable." + sValue.toString()));

You didn't set the right resource id of your image. Integer.parseInt("R.drawable." + sValue.toString()) return an invalid identifier.

You can following the example below to do.

    Class<?> drawableClass = R.drawable.class;
    try {
        Field icLauncherField = drawableClass.getDeclaredField("ic_launcher");
        imageView.setImageResource(Integer.valueOf(icLauncherField.getInt(null)));
    } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
alijandro
  • 11,627
  • 2
  • 58
  • 74
0

So this is the solution to the problem that I was having I know its not the prettiest or the best code but it has the functionality that I was looking for. Thanks to everyone for all of the help with this issue!

String[] arrayOfString = { "Beard Vape Co.;beard_vape_co", "two;zwei", "three;drei" };

    List<String> sKeyArray = new ArrayList<>();
    List<String> sValueArray = new ArrayList<>();
    for(String s: arrayOfString) {
        String[] array = s.split(";");
        String sKey = "", sValue = "";
        if (array.length > 1) {
            sKey = array[0];
            sValue = array[1];
            sKeyArray.add(sKey);
            sValueArray.add(sValue);
        }

    }
    for(int i = 0; i < sKeyArray.size(); i++){
        if(sKeyArray.get(i).toString().contains(liquidCompany.trim().toString())){
            testingText.append(sKeyArray.get(i).toString());
            testingText.append(sValueArray.get(i).toString());
            int resID = getResources().getIdentifier(sValueArray.get(i).toString() , "drawable", getPackageName());
            imageView.setImageResource(resID);
        }
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Aaron B
  • 461
  • 1
  • 5
  • 21
  • So basically it was just buggy code, that you needed help debugging. Next time, try to *isolate* the problem yourself. Ask yourself what could be wrong, then test just that. Here, it could have been the `for` loop, the `if` test, or the resID passed to `setImageResource`. StackOverflow is best used ***after*** you've tested each aspect of your code, and determined ***which*** line(s) don't work the way you expect. Then you would have a *specific* question, whose answer *would be useful to other people*. Here, what you had is "my code is broken and I don't know why". Solving it only helped you. – ToolmakerSteve Jan 06 '17 at 17:56