'Changing object's text color in an array list

I'm been trying to add a color parameter in the Array List:

MainActivity.java:

package com.example.ofir.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

        final ArrayList<Brands> brands = new ArrayList<>();
        brands.add(new Brands("KTM"));
        brands.add(new Brands("BMW"));
        brands.add(new Brands("Suzuki"));
        brands.add(new Brands("Yamaha"));
        brands.add(new Brands("HONDA"));

        BrandAdapter itemsAdapter = new BrandAdapter(this, brands);

        ListView listView = (ListView) findViewById(R.id.brandlist);

        listView.setAdapter(itemsAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                brands.get(position);
                Intent intent = new Intent(MainActivity.this, infoPage.class );
                startActivity(intent);
            }
        });
    }
}

BrandAdapter.java

 package com.example.ofir.myapplication;
    import android.app.Activity;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    /**
     * Created by Ofir on 21-Mar-17.
     */
    public class BrandAdapter extends ArrayAdapter<Brands>{
    
        private int mColorResourceId;
    
        //Resource id for background color of list
    
        public BrandAdapter(Activity context, ArrayList<Brands> brands) {
            super(context, 0, brands);
           // mColorResourceId = ColorId;
    
        }
    
        @Override
        public View getView(int position,  View convertView, ViewGroup parent) {
            // check if the current view is reused else inflate the view
            View listItemView = convertView;
            if(listItemView == null){
                listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
            }
    
            Brands brand_item = getItem(position);
    
            TextView brandName = (TextView) listItemView.findViewById(R.id.brandtextview);
            brandName.setText(brand_item.getBrand());
    
            return listItemView;
        }
    }

Brands.java

package com.example.ofir.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by Ofir on 20-Mar-17.
 */

public class Brands extends AppCompatActivity {

    private String mBrandName;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public Brands(String brandName){
        mBrandName = brandName;
      
}

    public String getBrand(){
        return mBrandName;
    }
        }
    }

I'm trying to add a colour to the Arraylist so instead of

brands.add(new Brands("KTM"));

I'll have this line of code:

brands.add(new Brands("KTM", Orange));

and the text colour will change accordingly to the colour orange.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source