WinterMute
WinterMute

Reputation: 185

Android listview with image

I am trying to get a listview item with some information and an image, but it doesn´t work and I don´t know why. While I am inspecting the database I can see everything as it should be. The scenes are not empty, they referencing correct and they work well. The once problem is by listing it with this adapter. Any hints?

@Edit: This code works now!

Activity and Adapter as inner class:

public class ScenePanel extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scene_panel);

        showScenes();
    }

    /**
     * Show all scenes as listView
     */
    private void showScenes()
    {
        SceneDao dao = new SceneDao(this);
        ArrayList<Scene> allScenes = dao.getAll();
        SceneAdapter sceneAdapter = new SceneAdapter(this, allScenes);
        ListView sceneView = findViewById(R.id.scene_view);
        sceneView.setAdapter(sceneAdapter);
    }

    /**
     * Extract bitmap from color of light.
     *
     * @param lightId to get its color
     * @return
     */
    private Bitmap extractColor(String lightId)
    {
        LightDao dao = new LightDao(this);
        Rect rect = new Rect(0, 0, 1, 1);
        Bitmap image = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(image);
        Paint paint = new Paint();
        paint.setColor(new BigDecimal(dao.getById(lightId).getColor()).intValue());
        canvas.drawRect(0, 0, 1, 1, paint);
        return image;
    }

    /**
     * Custom adapter for listing the scenes as ListView.
     */
    private class SceneAdapter extends BaseAdapter
    {
        private List<Scene> scenes;
        private LayoutInflater inflater;

        public SceneAdapter(Context ctx, List<Scene> scenes)
        {
            this.scenes = scenes;
            inflater = LayoutInflater.from(ctx);
        }

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

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

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

       @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.scene, parent, false);
        }

        ImageView image = convertView.findViewById(R.id.color_image);
        TextView name = convertView.findViewById(R.id.scene_name);
        TextView startingTrack = convertView.findViewById(R.id.starting_track);
        TextView nextTrack = convertView.findViewById(R.id.next_track);

        Scene target = scenes.get(position);
        String light = target.getLight();
        if (light != null)
        {
            image.setImageBitmap(extractColor(light));
        }
        if (target.getName() == null || target.getName().equals(""))
        {
            name.setText(target.getId());
        } else
        {
            name.setText(target.getName());
        }
        startingTrack.setText(target.getStartingTrack());
        nextTrack.setText(target.getNextTrack());

        convertView.setTag(position);
        return convertView;
    }

and my layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">

    <ImageView
            android:id="@+id/color_image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp" />

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        <TextView
                android:id="@+id/scene_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="20sp"
                android:textStyle="bold" />

        <TextView
                android:id="@+id/starting_track"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="18sp" />

        <TextView
                android:id="@+id/next_track"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="18sp" />
    </LinearLayout>
</LinearLayout>

Upvotes: 1

Views: 69

Answers (2)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15423

Your implementation of adapter is wrong. Should be like this:

    @Override
    public int getCount() {
        return scenes.size(); //returns total of items in the list
    }

    @Override
    public Object getItem(int position) {
        return scenes.get(position); //returns list item at the specified position
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.scene, parent, false);
        }

        ImageView image = convertView.findViewById(R.id.color_image);
        TextView name = convertView.findViewById(R.id.scene_name);
        TextView startingTrack = convertView.findViewById(R.id.starting_track);
        TextView nextTrack = convertView.findViewById(R.id.next_track);

        Scene target = scenes.get(position);
        String light = target.getLight();
        if (light != null)
        {
            image.setImageBitmap(extractColor(light));
        }
        if (target.getName() == null || target.getName().equals(""))
        {
            name.setText(target.getId());
        } else
        {
            name.setText(target.getName());
        }
        startingTrack.setText(target.getStartingTrack());
        nextTrack.setText(target.getNextTrack());

        convertView.setTag(position);
        return convertView;
    }

Upvotes: 1

AhmedR
AhmedR

Reputation: 76

You are returning 0 in getCount

It should return the number of elements in the list.

Upvotes: 1

Related Questions