Another class Android Bundle(Adapter)

Asked

Viewed 149 times

0

Good evening! I have the following situation :

case R.id.item_cep:
    corPadrao = Color.parseColor("#4DC184");
    imgCircle.setColorFilter(corPadrao);
    nomeRoteirizacao.setText("Por Cep");
    setActivityBackgroundColor(corPadrao);
    Intent intent = new Intent(getApplicationContext(),InformarEntrega.class);
    Bundle params = new Bundle();
    params.putString("cor","#4DC184");
    intent.putExtras(params);

where by clicking on item_cep it stores the color, only in another class(Class is Adapter) I need to have this color how to proceed?

Am I doing it right? and how do I get this color in the Adapter class?

  • As far as I know, the Bundle parses between activitys (or Fragments) I have never tried to use it to pass an element to another class. You can use a variable to pass the color to the Adapter class or use Sharedpreferences or have a model that contains the 'color' string and use this model to parse the data.

  • how would I pass the color to the class ? I already have the variable in the first Activity

1 answer

1

Diego,

To solve your problem, create a file called Colors.xml in values (if you don’t have it).

Like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="White">#FFFFFF</color>
 <color name="DarkOrange">#FF8C00</color>
 <color name="WhiteSmoke">#F5F5F5</color>
 <color name="Goldenrod">#DAA520</color>
 <color name="LightGrey">#D3D3D3</color>
 <color name="DarkGray">#333333</color>
 <color name="DarkRed">#8B0000</color>
 <color name="Gray">#808080</color>
 <color name="ForestGreen">#228B22</color>
 <color name="Black">#000000</color>
</resources>

No need for a Bundle. You could use in the Input Method object:

intent.putExtra("color", R.color.Gray);

To recover, in another Activity:

int resColor =  getIntent().getIntExtra("cor",0);

Zero is in case you can’t recover.

This will give you the integer for the color in class R. Simply assign it as you wish.

Editing: No sense in an Intent for an Adapter class.

Att,

  • Matthew! the problem that getIntent() does not recognize in the Adapter class!

  • @Diegodiasmól does not make sense an Intent for an Adapter class in terms of archiving. What do you want to do? If you use a class other than an Activity, type and pass in the constructor what you want. Adapter is not for this case "Informdelivery".

  • pq on Adapter he creates the cards, for example: I click on a menu option he changes the color of those cards! actually it’s not even the whole card is just a card element

  • Create a method in your Adapter that does this job and call it, for example changeColor(int elementPosition) and just change the color of the desired viewHolder element. in the Activity, Adapter.changeColor(1); for example.

  • public void setActivityBackgroundColor(int color) {&#xA; RelativeLayout titleLayout = (RelativeLayout) findViewById(R.id.relativeLeft);&#xA; RelativeLayout topLayout = (RelativeLayout) findViewById(R.id.relativeTop);&#xA; ArrayList<Pedido> items = Pedido.getTestingList();&#xA; for (int i = 0; i < items.size(); i++) { titleLayout.setBackgroundColor(color); topLayout.setBackgroundColor(color); } }

  • @I hope it helped. When you ask, if you can, details of the requirements (what you want to do exactly, so we can help you with more ownership). A hug and good luck!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.