javaandroidandroid-layoutbaseadaptercustom-arrayadapter

False colors / no images are set by adapter in layout what to do?


The wrong colors look like you see in the image (in the square)

I made an adapter for a custom GridView item but the false colors are shown. I checked already but it passes the right color Integers to the adapter so I think the mistake should be in it. Thank you for your help.

public class Uebersichtadapter extends BaseAdapter {
private Context context;
private ArrayList<Uebersichtliste> liste;

public Uebersichtadapter(Context c, ArrayList<Uebersichtliste> liste) {
    context = c;
    this.liste = liste;
}

@Override
public int getCount() {
    return liste.size();
}

@Override
public Object getItem(int position) {
    return position;
}

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

private class ViewHolder {
    TextView titel;
    TextView underline;
    TextView text;
    RelativeLayout background;
    ImageView bild;
}

@Override
public View getView(int position, View convertView, ViewGroup
        parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder = null;
    if (convertView==null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.uebersichtelement, null);
        GridView grid = (GridView)parent;
        int size = grid.getColumnWidth();
        convertView.setLayoutParams(new GridView.LayoutParams(size,size));
        holder.text = (TextView) convertView.findViewById(R.id.textView10);
        holder.underline  = (TextView) convertView.findViewById(R.id.textView9);
        holder.titel  = (TextView) convertView.findViewById(R.id.textView8);
        holder.background=(RelativeLayout) convertView.findViewById(R.id.uebbackground);
        holder.bild=(ImageView) convertView.findViewById(R.id.imageView3);
        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }
    holder.titel.setText(liste.get(position).getTitel());
    holder.underline.setText(liste.get(position).getUnderline());
    holder.text.setText(liste.get(position).getText());
    Log.e("Test",String.valueOf(liste.get(position).getTextffarbe()));
    Log.e("Test",String.valueOf(liste.get(position).getBildfarbe()));
    holder.text.setTextColor(liste.get(position).getTextffarbe());
    if (String.valueOf(liste.get(position).getBildfarbe()).contains("drawable_border"))holder.background.setBackgroundResource(liste.get(position).getBildfarbe());
    else if (String.valueOf(liste.get(position).getBildfarbe()).contains("drawable")) holder.bild.setImageResource(liste.get(position).getBildfarbe());
    else holder.background.setBackgroundColor(liste.get(position).getBildfarbe());
    return convertView;
}

}

I don't know what could be wrong there but anyway there have to be something, it couldn't be in the list, maybe in the layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/uebbackground"
android:layout_width="150dp"
android:layout_height="150dp"
android:padding="5dp">

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:singleLine="true"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:singleLine="true"
    android:textColor="@android:color/darker_gray"
    android:textSize="12sp" />

<TextView
    android:id="@+id/textView10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:singleLine="true"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    android:textStyle="bold" />

Data in adapter is:

        a.add(new Uebersichtliste("Vorgeschlagenes Element",elemente.get(n).getsymbol(),elemente.get(n).getdeutsch(),hintergrund,1,textfarbe));

hintergund= something like R.color.red textfarbe= something like R.color.black


Solution

  • First, as @EugenPechanec says, you shouldn't get the item from the list each times you want to access the properties. Because there's a performance penalty when you're calling the same method multiple times. You shouldn't use the following:

    ...
    holder.titel.setText(liste.get(position).getTitel());
    holder.underline.setText(liste.get(position).getUnderline());
    holder.text.setText(liste.get(position).getText());
    Log.e("Test",String.valueOf(liste.get(position).getTextffarbe()));
    Log.e("Test",String.valueOf(liste.get(position).getBildfarbe()));
    holder.text.setTextColor(liste.get(position).getTextffarbe());
    
    ...
    

    use something like this:

    ...
    // Get the item only once, then reuse it.
    Uebersichtliste item = liste.get(position);
    
    holder.titel.setText(item.getTitel());
    holder.underline.setText(item.getUnderline());
    holder.text.setText(item.getText());
    ...
    
    holder.text.setTextColor(litem.getTextffarbe());
    

    Then, you need to change the following code which is so hard to read and error-prone:

    if (String.valueOf(liste.get(position).getBildfarbe()).contains("drawable_border"))holder.background.setBackgroundResource(liste.get(position).getBildfarbe());
    else if (String.valueOf(liste.get(position).getBildfarbe()).contains("drawable")) holder.bild.setImageResource(liste.get(position).getBildfarbe());
    else holder.background.setBackgroundColor(liste.get(position).getBildfarbe());
    

    to something like this:

    // call item.getBildfarbe() only once.
    // bildfarbe is a drawable resource id (guessing)
    int bildfarbeResId = item.getBildfarbe();
    
    // and convert to string
    String bildfarbe = (String) bildfarbeResId;
    
    if(bildfarbe.contains("drawable_border")) {
       holder.background.setBackgroundResource(bildfarbeResId);
    } else if(bildfarbe.contains("drawable")) {
       holder.bild.setImageResource(bildfarbeResId);
    } else {
       holder.background.setBackgroundColor(bildfarbeResId);
    }
    

    Now, let's dissect the last code where the source of your problem reside.

    You can see that you're trying to convert a drawable id to string:

    // call item.getBildfarbe() only once.
    // assuming bildfarbe is a drawable resource id.
    int bildfarbeResId = item.getBildfarbe();
    
    // and convert to string
    String bildfarbe = (String) bildfarbeResId;
    

    where it will gives you a string of integer, something like this:

    String bildfarbe = "123456";
    

    but then you're comparing it to non integer value:

    if(bildfarbe.contains("drawable_border")) {
       ...
    } else if(bildfarbe.contains("drawable")) {
       ...
    } else {
       holder.background.setBackgroundColor(bildfarbeResId);
    }
    

    so it will always fall to the else part. Hence you only set the background color each times you set the item value to the holder.