Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:32:42+00:00 2026-06-02T00:32:42+00:00

onCreate() method: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.database); ListView lv=(ListView)findViewById(R.id.mylist); dbh = new

  • 0

onCreate() method:

public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.database);

       ListView lv=(ListView)findViewById(R.id.mylist);    
       dbh = new DatabaseHelper(this);
       c = dbh.getReadableDatabase().rawQuery("SELECT _id, " + 
                        DatabaseHelper.NAME + 
                ", " + DatabaseHelper.LASTNAME + 
                ", " + DatabaseHelper.ans2 + 
                " FROM " +
                DatabaseHelper.TABLE_NAME, null); // initializing 


       String[] dataFrom ={DatabaseHelper.NAME, DatabaseHelper.LASTNAME, DatabaseHelper.ans2};
       int[] dataTo = {R.id.name, R.id.value1, R.id.value2};

       SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                    R.layout.row, c, dataFrom, dataTo);
       lv.setAdapter(adapter);
       registerForContextMenu(lv);


    }

My row.xml file to display all data:

<?xml version="1.0" encoding="utf-8"?>


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


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_weight="30"
    android:id="@+id/name"
    />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
   android:layout_weight="40"
    android:gravity="center_horizontal"
    android:id="@+id/value1"
    />
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="30"
    android:gravity="right"
    android:id="@+id/value2"
    />


<ImageView
  android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:gravity="right"
   android:src="@drawable/ic_launcher"
   android:id="@+id/icon"
   />


</LinearLayout>

I have successfully created a table with SQLite. I would like to display an icon in each database row which will be displayed (using ListView) and the icon should depend on a certain element from the row. For example , if the sex (DatabaseHelper.SEX) is male, then we would display a male icon , and if the sex is female , we would display a female icon. If you look at my xml file , I have an icon already displayed ( which is the default android icon, but this is not what I want). Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:32:43+00:00Added an answer on June 2, 2026 at 12:32 am

    You could make you own adapter(extending SimpleCursorAdapter) and override only the bindView() method and set the icon as you like:

    class CustomAdapter extends SimpleCursorAdapter {
    
        public CustomAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);
        }
    
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // let the adapter fill the other items in the row
            super.bindView(view, context, cursor); 
           // find the icon in the list row
            ImageView icon = (ImageView) view.findViewById(R.id.icon); 
           // I don't know how you set up the DatabaseHelper.SEX column in your 
           //database, I assumed you have an int value, 0 for male and 1 for female
            int sex = cursor.getInt(cursor.getColumnIndex("sex"));
            if (sex == 0) {
                                //if male
                icon.setImageResource(R.drawable.male_icon);
            } else {
                                //if female
                icon.setImageResource(R.drawable.female_icon);
            }
        }
    
    }
    

    Use this adapter for your ListView and of course don’t forget to add to the query the DatabaseHelper.SEX column:

    c = dbh.getReadableDatabase().rawQuery("SELECT _id, " + 
                            DatabaseHelper.NAME + 
                    ", " + DatabaseHelper.LASTNAME + 
                    ", " + DatabaseHelper.ans2 + ", " + DatabaseHelper.SEX +
                    " FROM " +
                    DatabaseHelper.TABLE_NAME, null); // initializing 
    

    Edit:

    Or you can use a ViewBinder :

    c = dbh.getReadableDatabase().rawQuery("SELECT _id, " + 
                            DatabaseHelper.NAME + 
                    ", " + DatabaseHelper.LASTNAME + 
                    ", " + DatabaseHelper.ans2 + ", " + DatabaseHelper.SEX + 
                    " FROM " +
                    DatabaseHelper.TABLE_NAME, null); // initializing 
    
    
           String[] dataFrom ={DatabaseHelper.NAME, DatabaseHelper.LASTNAME, DatabaseHelper.ans2, DatabaseHelper.SEX};
           int[] dataTo = {R.id.name, R.id.value1, R.id.value2, R.id.icon};
    
           SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                        R.layout.row, c, dataFrom, dataTo);
           adapter.setViewBinder(new ViewBinder(){
    
                @Override
                public boolean setViewValue(View view, Cursor cursor,
                        int columnIndex) {
                    if (view.getId() == R.id.icon) {
                        int sex = cursor.getInt(columnIndex);
                        if (sex == 0) {
                            ((ImageView) view).setImageResource(R.drawable.male_icon);
                        } else if (sex == 1){
                            ((ImageView) view).setImageResource(R.drawable.female_icon);
                        }
    
                        return true;    
                    } else {
                        return false;
                    }
    
                }
    
            });
       lv.setAdapter(adapter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Activity with ListView inside it and in the onCreate method of the
@Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); // providers data // db.execSQL(insert into //
In my activity oncreate method, i have called a service using OnStartCommand(). My requirement
This is my activity oncreate() method. I set a positive button ok with a
I made an application and inside onCreate method i load 16 different soundpools. Everything
I've noticed in some coding people use icicle with the onCreate method, and I
I have noticed that when using actionBar.setSelectedNavigationItem(x) in the onCreate() method of my Activity,
When we develop an Android application, we always start from the onCreate() method of
package one.two; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.SimpleCursorAdapter;
I have a simple contentProvider, a layout with a ListView and a button for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.