<Android> Change the background color of the List row of ListView

Posted after a long time

I've been busy and couldn't update much, so I updated it after a long time. This time, I wanted to change the line color of ListView in the app I'm currently working on, so I'll leave a note about it.

Store data acquired from DB in Adapter

I have omitted various contents, but the purpose is It feels like "when getting data from the DB and preparing the List display, I will color the row of the currently selected data so that it is easy to understand."

sample.java


list = findViewById(R.id.splist_array); 
SampleList = new ArrayList<HashMap<String,String>>();

int i = 0;
flg_pos = -1;

//list
String NAME,ID,KGN,FLG;

//DB
helper = new DBHelper(getApplicationContext());
HashMap<String,String> bList = new HashMap<String,String>();

   try{
       db = helper.getWritableDatabase();

       //Read the number
       RecordCount = DatabaseUtils.queryNumEntries(db,TABLE);
       if(RecordCount == 0){
         flg = false;
       }else{
       //Read
        c = db.rawQuery(SAMPLE_SQL,null);
        while(c.moveToNext()){
                    NAME = c.getString(c.getColumnIndex(COL_NAME));
                    ID = String.valueOf(c.getInt(c.getColumnIndex(COL_ID)));
                    KGN = c.getString(c.getColumnIndex(COL_KGN));
                    FLG = c.getString(c.getColumnIndex(COL_FLG));

            //Judge whether it is selected here and give it a Position.
                    if(FLG.equals("t")){
                        flg_pos = i;
                    }

                    bList = new HashMap<String,String>();

                    bList.put(COL_ID,ID);
                    bList.put(COL_NAME,NAME);
                    bList.put(COL_KGN,KGN);
                    SampleList.add(bList);

                    i++;

                }
            }

        }catch(SQLiteException e){
            e.printStackTrace();
        }

Main subject

After the above acquisition is completed, it is finally set in Adapter.

sample.java


        if(SampleList != null){
        //GetView when setting the Adapter()Override so that can be called.
        //getView(--)Is called repeatedly for the set line, so use that. (Position=From 0)
            adapter = new SimpleAdapter(MainActivity.this,SampleList,
                    R.layout.list_row,FROM,TO){
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {

                    View view = super.getView(position,convertView, parent);
            
            //Determine the line you want to color here
                    //flg_pos is the line you want to color
                    if(flg_pos == position){
                        //Color the view parts.
                        view.setBackgroundColor(Color.parseColor("#4169e1"));
                        listText1 = view.findViewById(R.id.btext1); //id
                        listText2 = view.findViewById(R.id.btext2); //name
                        listText3 = view.findViewById(R.id.btext3); //kgn

                        listText1.setTextColor(Color.WHITE);
                        listText2.setTextColor(Color.WHITE);
                        listText3.setTextColor(Color.WHITE);
                    }

                    return view;
                }
            };

            list.setAdapter(adapter);
        }else if(flg == false){
            //If there is nothing in the List, I want to display something.
        }else{
            Log.e("ListError","Missing List...");
            Toast.makeText(this,"ListReadingError...",Toast.LENGTH_SHORT);
        }

It was easy. Rather.

At the end

I tried various googles, but there were many articles such as creating methods, and this was the easiest way to implement it smoothly. Well, if you want to customize something else, I feel like I can do it a little more. I'm sorry that there is no completed drawing or contents such as List_row. I don't have much time, so I'll come to update it here and there.

Reference site

This may be easier to understand. Octagonal Research Institute. Thank you very much. It was helpful. (http://www.hakkaku.net/hakkaker_blog/20090831-590)

Recommended Posts

<Android> Change the background color of the List row of ListView
[Eclipse] Change the color of the vertical ruler
Bootstrap4 Change the color of the hamburger menu
[Swift] Change the color of SCN Node
[Android] [Java] Manage the state of CheckBox of ListView
[AndroidStudio] Change the color of the ActionBarDrawerToggle hamburger menu
I saw the list view of Android development collectively
[Java] Delete the elements of List
[Swift] Change the textColor of UIDatePicker
Official logo list of the service
[Android] Dynamically set the height of ListView with copy (for beginners)
[swift5] How to change the color of TabBar or the color of item of TabBar with code
Understand the basics of Android Audio Record
[Rails] Change the label name of f.label
Ransack sort_link How to change the color!
How to sort the List of SelectItem
The list of installed apps is not displayed in getPackageManager (Android11 ​​/ API level 30)
[No.004] Corrected the order list screen of the orderer
Add empty data to the top of the list
Ominous odor list suggesting the possibility of refactoring
[Android] List all setting items on the setting screen
The story of tuning android apps with libGDX
Change the URL of the application of Eclipse project (Maven).
Change the timezone of the https-portal container to JST
How Microservices Change the Way of Developing Applications
Change the location folder of Docker image & container