Skity Skity -4 years ago 85
Android Question

Changing object's text color in an array list

I've been trying to add a color parameter in the Array List and I just cant seem to do it, i've spent countless of hours trying to do it and even searched for an answer here.

this is my code:

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);
}
});



}



}




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;



}




}




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;
}




}

}


What im trying to do is adding a color to the Arralist so instead of brands.add(new Brands("KTM"));


I'll have this line of code:

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


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

Thanks in advanced for any sort of help.

Answer Source

So you want to have list with Object that have 2 values, brand name and colour. If so you have to declair colour in your brand class.

public class Brands {

    private String mBrandName;
    private String colour;

    //constructor with 2 arguments, brand name and colour
    public Brands(String brandName, String colour){
        mBrandName = brandName;
        this.colour = colour;
    }

    public String getBrand(){
        return mBrandName;
    }

    public String getColour(){ //getter of colour
        return colour;
    }
}

Then you can add your colour (as a string, so in quotes) in list:

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

If you want to display colour, then you have to change your adapter, this line:

brandName.setText(brand_item.getBrand());

to something like this:

brandName.setText(brand_item.getBrand()+", "+brand_item.getColour());

But I strongly recommend to add new textView that will be displaying colour, then findByView it in your adapter (like brandName) and assign colour of Brand to newly created textView of colour.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download