Search code examples
androidsqlitelistviewsimplecursoradapter

image is not setting in the ImageView inside a listView with a cursor


@SuppressWarnings("deprecation")
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            MainActivity.this, R.layout.user, cursor, from, to)

    {

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            cursor.moveToPosition(position);
            String s = cursor.getString(cursor.getColumnIndex("USER_SEX"));
            final View row = super.getView(position, convertView, parent);

                LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                View sexxx = inflater.inflate(R.layout.user, null);
                sexlogo = (ImageView) sexxx.findViewById(R.id.sex);

            if (s.equalsIgnoreCase("Male"))
                sexlogo.setImageResource(R.drawable.male);

            else 
                sexlogo.setImageResource(R.drawable.female);
            return row;
        }
    };

am new to Android, the app adds a user to SQLite databsethen retrieves all users found in the database to a ListView using a cursor.

i want to check user's sex and then set him a logo after retrieving his data from database and before adding him to the listView.

i have edited my question and added the code above, the app didn't crash moreover images didn't appear .

any help please


Solution

  • this is the correct answer , just remove LayoutInflater since the super already inflates it .

    inflating a layout will return a new rootrepresenting the xml

    {
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
                cursor.moveToPosition(position);
                String s = cursor.getString(cursor.getColumnIndex("USER_SEX"));
                final View row = super.getView(position, convertView, parent);
    
                sexlogo = (ImageView) row.findViewById(R.id.sex);
    
                if (s.equals("Male"))
                    sexlogo.setImageResource(R.drawable.male);
    
                else
                    sexlogo.setImageResource(R.drawable.female);
    
                return row;
    
            }
        };