daniel12345smith, assuming the class stores objects/properties of the type View
. So instead of "inflate" (which is an exclusive option to work with the xml
), you can call methods of this class that you insert into Context
desired these views related to this class.
Example
PrincipalActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
Contatos.carregaContatos(PrincipalActivity.this);
}
Contatos.java
public class Contatos
{
public static void carregaContatos(Activity activity)
{
// declara TextView
// note que "activity" agora pode procurar uma view da activity PrincipalActivity.java
TextView textView = new TextView(); // ou "this.minhaTextView", whatever
// codigo que insere uma View guardada e gerida por esta classe "Contatos"
// note que "activity" agora pode procurar uma view da activity PrincipalActivity.java
LinearLayout linearLayout = activity.findViewById(R.id.info);
linearLayout.addView(textView);
}
}
And so on and so forth. The idea is to keep the "logica" (Domain Logic and business Logic) separate (unfortunately Android follows a View-Controller standard) and we have to manage as we can.
vlw but I have nothing xml in my project, that’s the problem.
– daniel12345smith
Oops, so, all right! This is the idea. Create Views as OBJECTS (by code) in your classes and then add in the Activity you want, all by code. Note that
TextView textView = new TextView();
of my example above does not exist in XML any...and it was added to the container Relativelayout by code, in the line below. But for this, Activityprincipal had to call the methodContatos.carregaContatos(PrincipalActivity.this);
and pass your Activity as a parameter. It was just to illustrate that your Idea in the question can follow this flow. By the way, I do everything like this hehe.– Felipe Douradinho