In an Android app I have various TextView instances whose IDs are something like: android:id="#+id/a_key where a_key is a word with no numerical values.
A Java class (not activity) parses an external XML file and populates a Map like this:
HashMap<String, String> map = new HashMap<String, String>;
map.put(a_key, a_value);
where the String a_key contains the exact same word of android:id="#id/a_key".
How would I go on about assigning a_value to the android:text resource whose ID is represented by the String a_key?
I apologize if this is confusing. I can post the code if it helps, but I tried to keep it as generic as possible.
Thanks in advance :)
A better way to do this would be with the way codeMagic suggested.
Using
int id = getResources().getIdentifier("a_key", "id", "com.example.app");
Then of course using
Textview a_textView = findViewById(id);
You will need to iterate through the entry set of the map to do it this way so use
map.entrySet()
ex:
for(Entry<String, String> entry : map.entrySet())
{
int id = getResources().getIdentifier(entry.getKey(), "id", "com.example.app");
TextView textView = findViewById(id);
textView.setText(entry.getValue());
}
You say your HashMap is not in an Activity, but you won't be able to set the text for a TextView unless you are in an Activity. So let's assume that your HashMap is available from that class. The following should work in your Activity:
TextView testTextView = (TextView) findViewById(R.id.testTextView);
HashMap<String, String> map = otherClassInstance.getMap();
testTextView.setText(map.get("testTextView"));
The fragility here is that if you refactor the ID, you'll also need to remember to change the hard-coded text in your call to setText(). For that reason Gabe's solution is probably better for your case.
Well, the id is converted to an integer and lost to the system at compile time. So that isn't the way to do it. The best idea is to use the tag on each text view: android:tag="a_key". Then when you want to set the text of the view, use getTag to get the tag, and pass it into the hash map to get the associated string.
Related
I do not know the number. of textview which should be created at runtime. List of Textviews are getting from server. I am creating textviews using below function. Since i am new to android i unable to get text from all textviews. Please suggest me the solution
private void textview(String text)
{
TextView txt_view= new TextView(this);
txt_view.setText(text);
linearLayout.addView(txt_view);
}
Please suggest me the solution i m stuck in getting the text from many textview
Above image is my form, what i want to get text from all views which are created dynamically and store to firebase
add your TextView to HashMap with a unique key
and match your key and get value from map, it will surely help you. after that at time of fetching data use for loop and get data using that unique key
Just get it from your TextView object, like this:
txt_view.getText().toString();
Use same object of TextView to gt text of it. txt_view.getText().toString();
You can get same TextView object by finding childs of LinearLayout.
How about to store them in an ArrayList and iterate over it to get all and maniupulate them afterwards.
ArrayList<TextView> alTextViews = new ArrayList<>();
private void textview(String text) {
TextView txt_view= new TextView(this);
alTextView.add(txt_view);
txt_view.setText(text);
linearLayout.addView(txt_view);
}
with
alTextView.get(0).getText().toString();
you'll get the text
I want to store city names by country in an array.
This is my code
String cities[][]=new String[10][20];
I want to assign all cities of a country one time like this.
cities["USA"]={"NEW YORK","WASHINGTON"}
cities["UK"]={"LONDON","CAMBRIDGE","CARLISLE"}
then I want to use like this
String mycity=cities["UK"][2];
but eclipse shows error for assigning values. how can I use this arrays?
Try like this,
String cities[][]={
{"NEW YORK","WASHINGTON"},
{"LONDON","CAMBRIDGE","CARLISLE"}
};
And
cities[0][0]
will return NEW YORK
May be this helps you.
Better you can use a HashMap - List combination like this
HashMap<String,HashMap<String,List<String>>> cities = new HashMap<>();
Refer following links for more details Storing HashMap inside HashMap,
Storing a HashMap inside another HashMap and improving performance
I have added all lat/lng to arraylist and .Iwould like to map it to string name and another string along with its id which is an int .Basically I would like to get :
Association : (arraylist)-->Name---->Another Name------>id
how do I do the above association.I am a noob in android and I am using hashmap but it only puts(key,value) which does not satisfy the above condition.
Please let me know how I can implement the above requirement.
I appreciate any help.
Thanks in Advance.
There are a number of ways you could accomplish this. Here are two different ways off the top of my head:
Create an object that will hold the two names and id. Then create a hashmap that maps each lat/long value to the appropriate object. The prototype would be like:
HashMap<Long/Lat, Object>
Or if you don't want to create a new object, then create a hashmap that maps each lat/long value to a hashmap that contains the two names and id. The prototype would be like:
HashMap<Long/Lat, HashMap<String, String>>
I created a list based on this example:
http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/
In GetView method, need to access information that is stored only in the HashMap.
I can access using "adapter.getItem (position). ToString ()" but this way, keys and values come together in a single string, like this:
{date=2011-07-25 19:30:00, id=1, caption=Test Caption, title=Test Title, bookmark=true}
You can retrieve this data separately? For example, only the value of "bookmark" field
Please include an example. I am new to developing for Android.
remember that you know the data type that is used with the adapter.
instead of using toString, just get the item and use it as you usually do.
for example:
HashMap<String, String> i = (HashMap<String, String>) adapter.getItem(position);
i.get(ITEM_TITLE);
I am attempting to set up a simple Morse encoder using a hashmap in android. Putting values in the hashmap seems pretty straightforward like so:
HashMap<String, String> translate = new HashMap<String, String>();
//initializing translate
translate.put("A",".-");
//same for all letters of alphabet and numbers
However I am having difficulty finding an effective way to utilize the data of the key values for export to another method. I plan to use these values in a string method and simply display it on the phone screen for the user when they type a letter. For example, if they type in "A" the hash map will be queried for "A" and return a ".-". I have never worked with hashmaps before and can't find a suitable example.
Any help on how to access these keys within an android environment will be appreciated!
Use HashMap.get(), so:
translate.get("A"); // returns ".-"
The object returned is exactly the same as the object supplied in the 2nd argument to put(). So if you put a URL in (and the Map is suitably typed) you will get the same URL instance returned from get().
HashMap has keySet and entrySet(). You may start from here. Here is javadoc for complete list of methods in HashMap. Here is an example on how to use those methods.