How to import or use any defined thing in the MainActivity.java File?
The simple Answer is from Resources.Now, What are resources and how to use Resources?
The Most simple and Standard way is to create a Variable and use that to get the other resources from other Files like Strings.xml.
_______________________________________________________________________________
Consider that had defined a String-Array in Strings.xml file and want to import that array into you ListView.
Step 1. Create a Listview in MainActivty.xml(Your main Layout file) and define ID.
Step 2. Define you string -array in Strings.xml file(It's located in Values directory) and give a name say "Languages".
Step 3. Go to MainActivity.java file and type following code
ListView list = (ListView) findViewById(R.id.list_items);
Resources resources = getResources();
String[] languages = resources.getStringArray(R.array.Languages);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.Simple_list_1, languages); //I don't exactly know the ListView drop-down options
listview.setAdapter(adapter);
Step 4. Run the Emulator.
No comments:
Post a Comment