hash map android issues - android

Why we add hash map in array list.For example
HashMap<String,String> hashMap = new HashMap<String,String>();
hashMap.put("1","bmw");
hashMap.put("2","mercedez");
hashMap.put("3","audi");
hashMap.put("4","sunny");
ArrayList<HashMap<String,String>> arrayList =new ArrayList<HashMap<String,String>>();
arrayList.add(hashMap);
Can we directly fetch data from hashmap like hashMap("1"); like this

We can directly fetch data from hashmap like hashMap.get("1");

Related

How I can convert the hashmap to array?

I want hashmap to array
I create the hashmap
Map<Integer,File> selectedFiles = new Hashmap<>();
and put it some Map data
And I convert this hashmap's values to array so
File[] files = (File[]) selectedFiles.values().toArray();
But errors occur;
java.lang.Object[] cannot be cast to java.io.File[]
I know that when I want the hashmap's values to array, use .values.toArray() but maybe it is not corret;
This way is wrong?
Map<Integer, File> selectedFiles = new HashMap<>();
selectedFiles.put(1, null);
File[] files = selectedFiles.values().toArray(
new File[selectedFiles.size()]);
System.out.println(files);// We will get the object [Ljava.io.File;#15db9742
arrays. toArray without parameters creates an object array because the type information of the list is lost at runtime.
Please use below Code to convert HashMap to Array ,You can change String to File in your case.
//Creating a HashMap object
HashMap<String, String> map = new HashMap<String, String>();
//Getting Collection of values from HashMap
Collection<String> values = map.values();
//Creating an ArrayList of values
ArrayList<String> listOfValues = new ArrayList<String>(values);
// Convert ArrayList to Array
String stringArray[]=listOfValues.toArray(new String[listOfValues.size()])

How to convert HashMap<String, JSONObject> object into JSONArray

I have HashMap with jsonobject I want to store all those values into JSONArray.
Is it possible ?
Please help me.
Suppose you have two json objects stored in hashmap :
HashMap<String, JSONOnject> stringJson = new HashMap<String, JSONObject>();
stringJson.put("key", jsonobject1);
stringJson.put("key1", jsonobject2); //remember the hashmap structure, its key value structure.
You can access this jsonObject by get method of hashmap:
JSONArray jarray = stringjson.get("key").getJsonArray("json array name"); //stringjson.get("key") will return jsonObject you stored.
But you can do this directly without saving json object to hashmap.

How can i use iterator to get each data from arraylist of hashmaps in android?

I created the arraylist of hashmaps that pass value from previous intent like this:
final ArrayList<HashMap<String, String>> saleArrList = (ArrayList<HashMap<String, String>>) intent.getSerializableExtra("saleArrList");
Then, I want to get data from this arraylist of hashmaps and i use iterator to point it.
Iterator<HashMap<String, String>> it = saleArrList.iterator();
while(it.hasNext()){
HashMap<String, String> value = it.next();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sProductID", saleArrList.get(.....).get("ProductID").toString() ));
}
I use Hashmap as iterator to point each of Hashmap in Arraylist.
How do I get specifies Hashmaps data form This Arraylist ??
(What code or value that I should put in the get(...) )
Thanks for all answer, Sorry for my wrong grammatically question.
Tell me if you want more code :)
Try
for (HashMap<String, String> arrValue : saleArrList) {
params.add(new BasicNameValuePair("sProductID", arrValue.get("ProductID") ));
}
You don't need the toString() in the HashMap values as they are already Strings.

android error when i try to put Array which contains Map<string, string> to String for my auto complete

public ArrayList<HashMap<String,String>> c_tmp= new ArrayList<HashMap<String,String>>();
public HashMap<String,String>mapa=new HashMap<String, String>();
String[] name_Val = null;
//i fill the map and the array list in a while()
mapa.put(numberz,nnn);
c_tmp.add(mapa);
and an error occurs( force close)when trying to fill the next line name_Val=....
name_Val = (String[]) c_tmp.toArray(new String[c_tmp.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,name_Val);
txtPhoneName.setAdapter(adapter);
/// am I writing that line wrong ( because i've copied it from ArrayList<String>)
You should try to do it first with a map and when you initialize it then to hash map, and use a simple adapter
mAdapter = new SimpleAdapter(this, peopleList, R.layout.row ,new String[]
{ "Name", "Phone" }, new int[] { R.id.text1, R.id.text2 });
txtPhoneName.setAdapter(mAdapter);
But before all in another function populate the list of people...
What I would suggest is, you can simply use a HashMap<String,String> Data type as map has the property of adding values for a unique key
After you are done with storing the Data in the hashmap you can use the Iterator to iterate and get all keys from the HashMap and thus the Values from all those keys which shall be Stored in an ArrayList , and then converting it to your String Array would not be a problem for you.

Hashmap error,i can't load data from string

I m using hashmap for a listview.
map = new HashMap<String, Object>();
map.put("name", R.string.information);
map.put("address", R.drawable.info3);
mylist.add(map);
// ...
my problem is that i cant load string in my hashmap (R.string.information)..when i run it,i can only see some numbers instead of my text..am i doing something wrong?thanks
<string name="information">Informations</string>
R.string.information is just an int. If you want the actual String, use
map.put("name", getString(R.string.information));
string.information returns a integer instead use
getString(R.string.information);
which will give you the string that you have stored in the String.xml
Yes you are doing wrong because you have print the id the the name and address not value
you should put value of the name and address tag using this one according to your requirement.
String mess = getResources().getString(R.string.name);
this.getString(R.string.name)//if you are in context
String mess = Resources.getText(R.string.name);
R.string.information gives you int value not String
You have to make change in creating hashmap
Your hasmap should be
HashMap map = new HashMap<String, Integer>(); instead of `HashMap map = new HashMap<String, Object>();`
Finally code wil be
HashMap map = new HashMap<String, Integer>();
map.put("name", R.string.information);
map.put("address", R.drawable.info3);
mylist.add(map);
Thanks
Deepak

Categories

Resources