Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner updating only on user actions

I have IconText that has image and text views inside it, both class and xml.

I then populate the Spinner inside of the MainActivity with these IconTexts, using extended BaseAdapter (IconTextAdapter) as adapter.

Now, IconText works fine (shows as it should). Spinner however doesn't.

  1. When I start the app, it shows the first IconText as it should.
  2. When I open the choose dialog, everything is showing as it should.
  3. When I select another item, the choose dialog collapses and spinner displays no IconText (ie. only "arrow down" for opening choose dialog).

I can still open the choose dialog and choose another. I've noticed that if I exit from app (return button, not really quiting the app) and enter again that the proper IconText is shown.

I guess that the fault lies with the adapter? I will try avoiding posting a lot of code [:

IconText.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView android:id="@+id/it_image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/home"/>

    <TextView
        android:id="@+id/it_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20sp"
        android:text="ABC"
        android:textColor="@color/black_overlay"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>

IconText.java

public class IconText {

    static LayoutInflater inflator;

    public ImageView icon;
    public TextView text;
    public View view;

    public IconText(String title, int icon_id){
        Log.d("IconText", "Create");
        view = inflator.inflate(R.layout.icon_text, null);

        text = (TextView) view.findViewById(R.id.it_text);
        icon = (ImageView) view.findViewById(R.id.it_image);

        text.setText(title);
        icon.setImageResource(icon_id);
    }

    public static void initInflator(Context context){
        if(inflator != null) return;
        Log.d("IconText", "Init inflator");
        inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
}

IconTextAdapter.java

public class IconTextAdapter extends BaseAdapter implements SpinnerAdapter{

    public ArrayList<IconText> items;

    public IconTextAdapter(){
        super();
        items = new ArrayList<>();
    }

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

    @Override
    public Object getItem(int arg0) {
        return items.get(arg0);
    }

    @Override
    public long getItemId(int position) {
        return items.get(position).view.getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return items.get(position).view;
    }
}

MainActivity, relevant code

void initFilter(){
        Log.d("MainAction", "Find Filter");
        filter = (Spinner) findViewById(R.id.filter);
        Log.d("MainAction", "Create Adapter");
        IconTextAdapter adapter = new IconTextAdapter();

        Log.d("MainAction", "Create items");
        for(int i=0; i<ETypes.names.length; i++){
            IconText it = new IconText(ETypes.names[i], ETypes.icons[i]);
            adapter.items.add(it);
        }
        Log.d("MainAction", "Setting adapter");
        filter.setAdapter(adapter);
    }

ETypes names[string] and icons[id] are static. Inflator is initialized succesfully.

I haven't worked much in Android. If it were Java/Swing, I guess I would just call a redraw or something.

I know there are some bad practices here (all public variables, and so on). This code is still in early prototype stage, it will be fixed soon. I'm not looking for optimization, just for the solution to my problem.

Update 1: So I saw I didn't implement getDropDownView so I did, the same code as getView (no need to post it?).

Also I made an experiment: At the end of IconText contrusctor I added

view = text

And it works just fine (showing only text).

I guess this pinpoints that the problem originates from custom view?

Update 2:

Did another experiment with IconText, setting view = icon; and it doesn't behave as it should, ie. it behaves like it's a custom view.

like image 256
Invader Zim Avatar asked Oct 17 '22 04:10

Invader Zim


1 Answers

Doesn't really solve this specific bug, but it solves my problem.

Custom Image and Text View in Spinner Solution.

like image 54
Invader Zim Avatar answered Nov 03 '22 23:11

Invader Zim