0

I have a method that would receive the context and the List so it could change the background color of the adapter and return it, but now I have a situation that it must be either a android.R.Layout.simple_list_item_1 for single line and android.R.Layout.simple_list_item_2 or it could be any other type of android.R.Layout.*

How I can define the param on the method?

This is the method that I have for the moment

private ArrayAdapter<String> AdapterColor(List<String> list, Context context)
{
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list)
    {
        @Override
        public View getView(int position, View converView, ViewGroup parent)
        {
            View view = super.getView(position, converView, parent);
            if(position %2 == 1)
            {
                view.setBackgroundColor(Color.parseColor("#FFD6D6D6"));
            }
            else
            {
                view.setBackgroundColor(Color.parseColor("#FFFAFAFA"));
            }
            return view;
        }
    };
    return adapter;
}

I did try the Layout as param but doesn't work, I did search it but can't find info about this.

Edit

I wanted to do something like this:

private ArrayAdapter<String> AdapterColor(List<String> list, Context context, Layout layoutParam)
{
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, layoutParam, list)
    {
        @Override
        public View getView(int position, View converView, ViewGroup parent)
        {
            View view = super.getView(position, converView, parent);
            if(position %2 == 1)
            {
                view.setBackgroundColor(Color.parseColor("#FFD6D6D6"));
            }
            else
            {
                view.setBackgroundColor(Color.parseColor("#FFFAFAFA"));
            }
            return view;
        }
    };
    return adapter;
}

Wanted to receive the LayoutParam from outside to the method, making the code more easy.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Camadas
  • 509
  • 1
  • 5
  • 21
  • I suggest you to use your own CustomAdapter. How to do it: https://stackoverflow.com/a/23125744/5272951 . There you can parse any object from your activity you want. And you can set different colors by using `convertView.setBackgroundColor(position % 2 == 0 ? Color.parseColor("#FFFAFAFA") : Color.parseColor("#FFD6D6D6"));` – Dumbo Apr 10 '18 at 14:14

2 Answers2

0

Instead of:

View view = super.getView(position, converView, parent);

use the "converView" (or convertView, however you prefer :D ). Like so:

converView.setBackgroundColor(Color.parseColor("#FFD6D6D6"));
Luís Henriques
  • 604
  • 1
  • 10
  • 30
  • Ty, but that still doesn't answer my question :P let me edit the Question for better understanding – Camadas Apr 10 '18 at 13:59
  • Oh, sorry. Yeah, you just have to link it to a List of objects, and read them. Use the "position" parameter to get the item you want. SomeObject someObject = someList.get(position); Then simply read the data from the object model. In your case, read the color parameter from a method in the SomeObject model. – Luís Henriques Apr 10 '18 at 14:02
  • No worries but still not what I asked :p, I want is to receive as param on the method the `android.R.Layout.simple_list_item_1` or `android.R.Layout.simple_list_item_2` – Camadas Apr 10 '18 at 14:05
  • Ok, last try :D You have to pass / accept an int, and pass the layout resource such as: R.layout.some_resouce. Android treats compiled layout resources as ints, so that's what you want. Simply, instead of (..., Layout layoutParam) use (... int layoutParam). Either that or set it on a View object and pass it. If this was helpful, I'll edit my answer so it can be helpful to others. – Luís Henriques Apr 10 '18 at 14:17
  • Ty but i did have tryed that all rdy, but adding the `@LayoutRes` before the int did worked, like wew lad as told on is answer – Camadas Apr 10 '18 at 14:19
0
void setLayout(@LayoutRes int layoutRes){
    // do something with layoutRes
}

setLayout(R.layout.doggo)

see https://developer.android.com/reference/android/support/annotation/LayoutRes.html for more info

Zun
  • 1,553
  • 3
  • 15
  • 26
  • Ty the `@LayoutRes` did work, I did all rdy did try with the `int layoutRes` but did got an error, thanks – Camadas Apr 10 '18 at 14:15