So I fill the texts in my listview
String[] titulo = new String[]{
"Prueba1",
"Prueba2",
"Prueba3",
"Prueba4",
"Prueba5",
};
How can I use getString to be able to translate it to any language in string.xml
For example for other texts in my class I use
.setTitle(getResources().getString(R.string.text_1));
How can I do it in this case? Thank you
My code,
ListViewAdapter:
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
String[] titulos;
int[] imagenes;
LayoutInflater inflater;
public ListViewAdapter(Context context, String[] titulos, int[] imagenes) {
this.context = context;
this.titulos = titulos;
this.imagenes = imagenes;
}
@Override
public int getCount() {
return titulos.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtTitle;
ImageView imgImg;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.list_row, parent, false);
// Locate the TextViews in listview_item.xml
txtTitle = (TextView) itemView.findViewById(R.id.list_row_title);
imgImg = (ImageView) itemView.findViewById(R.id.list_row_image);
// Capture position and set to the TextViews
txtTitle.setText(titulos[position]);
imgImg.setImageResource(imagenes[position]);
return itemView;
}
}
Activities:
public class cat_s extends AppCompatActivity {
ListViewAdapter adapter;
private CollapsingToolbarLayout collapsingToolbarLayout = null;
String[] titulo = new String[]{
"Prueba1",
"Prueba2",
"Prueba3",
"Prueba4",
"Prueba5",
};
int[]imagenes={
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cat);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(getResources().getColor(R.color.colorbarra_inf));
}
ListView listview = (ListView) findViewById(R.id.listview_shi);
listview.startAnimation(AnimationUtils.loadAnimation(this, R.anim.custom_bounce_entry));
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ListView lista = (ListView) findViewById(R.id.listview_shi);
adapter = new ListViewAdapter(this, titulo, imagenes);
lista.setAdapter(adapter);
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int posicion, long l) {
switch (posicion) {
case 0:
Intent as = new Intent(getApplicationContext(), uno.class);
startActivity(as);
break;
case 1:
Intent ca = new Intent(getApplicationContext(), dps.class);
startActivity(ca);
break;
case 2:
Intent el = new Intent(getApplicationContext(), tres.class);
startActivity(el);
break;
case 3:
Intent elne = new Intent(getApplicationContext(), cuatro.class);
startActivity(elne);
break;
case 4:
Intent faa = new Intent(getApplicationContext(), cicon.class);
startActivity(faa);
break;
}
}
});
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle(getResources().getString(R.string.text_shi));
toolbarTextAppernce();
}
private void toolbarTextAppernce() {
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.collapsedappbar);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.expandedappbar);
}
}
UPDATE:
ListViewAdapter:
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
String[] titulos;
int[] imagenes;
LayoutInflater inflater;
public ListViewAdapter(Context context, String[] titulos, int[] imagenes) {
this.context = context;
this.titulos = titulos;
this.imagenes = imagenes;
}
@Override
public int getCount() {
return titulos.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtTitle;
ImageView imgImg;
// ------------------------------------------- NUEVO
String[] titulos = context.getResources().getStringArray(R.array.titulos); //Obtiene Array.
//----------------------------------------------
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.list_row, parent, false);
// Locate the TextViews in listview_item.xml
txtTitle = (TextView) itemView.findViewById(R.id.list_row_title);
imgImg = (ImageView) itemView.findViewById(R.id.list_row_image);
// Capture position and set to the TextViews
txtTitle.setText(titulos[position]);
imgImg.setImageResource(imagenes[position]);
return itemView;
}
}
String.xml
<string-array name="titulos">
<item>Uno</item>
<item>Dos</item>
<item>Tres</item>
<item>Cuatro</item>
<item>Cinco</item>
</string-array>
With these modifications I can already modify the text from the string.xml but I use ListViewAdapter for different classes and when making these modifications in all the classes I get the same texts, those of that array and I need them to be different in each class but using the same ListViewAdapter . Obviously without having to create different ListViewAdapter
:::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::
UPDATE 2:
string.xml
<resources>
<string-array name="titulos">
<item>Uno</item>
<item>Dos</item>
<item>Tres</item>
<item>Cuatro</item>
<item>Cinco</item>
</string-array>
<string-array name="titulos1">
<item>Seis</item>
<item>Siete</item>
<item>Ocho</item>
<item>Nueve</item>
<item>Diez</item>
</string-array>
</resources>
ListViewAdapter:
public class ListViewAdapter extends BaseAdapter {
int[] idArray;
public ListViewAdapter(Context context, String[] titulos, int[] imagenes, int[] idArray) {
this.idArray = idArray;
String[] titulos = context.getResources().getStringArray(R.array.titulos); //Obtiene Array.
Class:
int[] idArray = {
R.array.titulos1,
};
---
adapter = new ListViewAdapter(this, titulo, imagenes, idArray);
You could define inside the file
strings.xml
located inside/res/values
an array with the values:
To obtain the values of the array , they would be obtained in this way:
you can also access each element individually based on its index :
If you want to define your
array
in code, it should be within theAdapter
preferably:In this way, within
getView()
you would obtain the values correctly contained in the array and they will be displayed in theTextView
:To access the array from another class it is defined as
public static
:and you access it this way assuming that the class where the array was defined is
MainActivity
:Update:
According to your question you want the elements in Strings.xml so you declare the array inside this file:
code
getView()
according to your question:In
ListViewAdapter
change:a
You can do the same with the images if you want.
In /res/values you create your strings.xml file, default language.
In /res/values-es you create your strings.xml file, for Spanish.
In /res/values-fr you create your strings.xml file, for French. Etc etc
In each of these files, in the corresponding language:
Finally, in Java, you access those literals via R.string.title or R.string.hello_world.
Applying this to your code, you would add the five "Test" texts, and access them by R.string.testX
In order to obtain the list of titles you want at any given time:
And in the Activity: public class cat_s extends AppCompatActivity {
either