1

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Skity
  • 131
  • 1
  • 10
  • Why your `Brands` class extends `AppCompatActivity` that means it is activity.. And what I understand you just store name of `Brand` so it shold be some pojo class – miljon Mar 22 '17 at 20:35
  • thank you for letting me know that, I removed it, basically what im trying to do is make a list on the main screen and on each list item click it will move to the correct page, im trying to make every list item have the color that represents the brand. – Skity Mar 22 '17 at 20:52

1 Answers1

0

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", "#FF9933")); //#FF9933 means orange in RGB

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());
brandName.setColorText(Color.parseColor(brand_item.getColour()));

More informations about setting colour you can find here:

https://stackoverflow.com/a/4602929/3870382

========= OLD TEXT

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.

Community
  • 1
  • 1
miljon
  • 2,611
  • 1
  • 16
  • 19
  • First of all thank you for replying, I don't understand how does it know GetColour refers to a color? for all it knows its just a simple String? – Skity Mar 22 '17 at 22:13
  • I changed `Brand` class it has field `colour` of type `String` and this class has a getter method `getColour()` that returns field `colour` of type `String`. Look at `Brand` class. – miljon Mar 22 '17 at 22:18
  • I saw that, but isn't getColour just the name of the string? what defines it as a colour? – Skity Mar 22 '17 at 22:33
  • Oh man, it's basic of Java :P getColour is method (a getter), you call it if you need value of field colour. You have constructor in your class that assign constructor argument to object field (first arg. name, and second arg. colour), when you construct your Brand object through `new Brand("KTM", "orange");` it knows that name is `KTM` and colour is `orange`. Read about methods, constructors and object oriented programming. Maybe this help you: http://stackoverflow.com/a/17942675/3870382 – miljon Mar 22 '17 at 22:37
  • 1
    I see, thank you for your help and patience, im still in my first steps of coding and you've helped a ton! – Skity Mar 22 '17 at 22:47
  • Just an update, I didn't fully understand your way because I didn't quite get what defines String getColor as a color because just putting "orange" after the brand name just adds another String to the display orange string, didnt quite get where orange is defined in your explanation using the edits you gave me, what I did eventually was in the brands class I set an int for the color and getter for the color, added setTextColor row in my adapter and in the mainactivity just used ContextCompat.getColor in the arraylist row i wanted to change. – Skity Mar 23 '17 at 01:13
  • Yeah it just adds a String variable color, I just understand your question now, it makes sense :P, you did good job to change it to int and set a color by your own ;) I don't know why you use `ContextCompat.getColor` I would have to look at code first.. But It's not important. One advice, you can store a color in a lot of ways, as a Color object, as an int like you did or as a String as well, I change my answer a little bit, to show you how you can accomplish it ;) – miljon Mar 23 '17 at 07:18
  • Just added in my brandsAdapter: TextView brandColor = (TextView) listItemView.findViewById(R.id.brandtextview); brandColor.setTextColor(brand_item.getColor()); and the arraylist now looks like this: brands.add(new Brands("KTM", ContextCompat.getColor(this, R.color.colorOrange))); – Skity Mar 23 '17 at 23:29