ArrayList<String> parsedPODAY_ID=new ArrayList<>();
ArrayList<String> parsedPODAY_P_IMG=new ArrayList<>();
ArrayList<String> parsedPODAY_NAME=new ArrayList<>();
ArrayList<String> parsedPODAY_PRICE=new ArrayList<>();
ArrayList<String> parsedPODAY_OFFPRICE=new ArrayList<>();
I have created arraylist.But how can i create a arraylist or array of these arraylist.
Thanks in advance!!
Simply use
ArrayList<ArrayList<String>> parent = new ArrayList<>();
add like
parent.add(parsedPODAY_OFFPRICE);
Try following
ArrayList<ArrayList<String>> mainArrayList = new ArrayList<ArrayList<String>>();
ArrayList<String> subOneArray = new ArrayList<String>();
subOneArray.Add("first");
subOneArray.Add("second");
ArrayList<String> subTwoArray = new ArrayList<String>();
subTwoArray.Add("first");
mainArrayList.Add(subOneArray);
mainArrayList.Add(subTwoArray);
above code will bit complicated when change made, another way is
// create map to store
Map<String, List<String>> map = new HashMap<String, List<String>>();
// create list one and store values
List<String> valSetOne = new ArrayList<String>();
valSetOne.add("Apple");
valSetOne.add("Aeroplane");
// create list two and store values
List<String> valSetTwo = new ArrayList<String>();
valSetTwo.add("Bat");
valSetTwo.add("Banana");
// create list three and store values
List<String> valSetThree = new ArrayList<String>();
valSetThree.add("Cat");
valSetThree.add("Car");
// put values into map
map.put("A", valSetOne);
map.put("B", valSetTwo);
map.put("C", valSetThree);
// iterate and display values
System.out.println("Fetching Keys and corresponding [Multiple] Values n");
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
List<String> values = entry.getValue();
System.out.println("Key = " + key);
System.out.println("Values = " + values + "n");
}
Related
I need to create the table classes in the realtime database in firebase based on the code source below
And this is my code to add new record in the database
String AllClasses = dataSnapshot.child("staff").child(uid).child("class_ids").getValue().toString();
if(!AllClasses.equals("0")) {
HasClasses = true;
String[] myList = AllClasses.split(",");
SetClassIdsList(myList);
List<String> ClassNameList = new ArrayList<String>();
List<String> ClassTimingsList = new ArrayList<String>();
for (String currClassID : myList) {
ClassNameList.add(dataSnapshot.child("classes").child(currClassID).child("class_name").getValue().toString());
ClassTimingsList.add(dataSnapshot.child("classes").child(currClassID).child("class_timings").getValue().toString());
}
String[] ClassName = new String[ClassNameList.size()];
ClassNameList.toArray(ClassName);
String[] ClassTimings = new String[ClassTimingsList.size()];
ClassTimingsList.toArray(ClassTimings);
SetListVIew(ClassName, ClassTimings);
Here I am creating a list of items using expandable list view.
Here is the code for the list items.
public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData() {
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
List<String> business = new ArrayList<String>();
business.add("India");
List<String> entertainment = new ArrayList<String>();
entertainment.add("Brazil");
List<String> gaming = new ArrayList<String>();
gaming.add("United States");
List<String> general = new ArrayList<String>();
general.add("United States");
List<String> music = new ArrayList<String>();
music.add("United States");
List<String> sciandnat = new ArrayList<String>();
sciandnat.add("United States");
List<String> sports = new ArrayList<String>();
sports.add("United States");
List<String> technology = new ArrayList<String>();
technology.add("United States");
expandableListDetail.put("BUSINESS", business);
expandableListDetail.put("ENTERTAINMENT",entertainment);
expandableListDetail.put("GAMING",gaming);
expandableListDetail.put("GENERAL", general);
expandableListDetail.put("MUSIC", music);
expandableListDetail.put("SCIENCE AND NATURE", sciandnat);
expandableListDetail.put("SPORTS", sports);
expandableListDetail.put("TECHNOLOGY", technology);
return expandableListDetail;
}
}
I want the list to be displayed in the order as BUSINESS,ENTERTAINMENT,GAMING,GENERAL,MUSIC,SCIENCE AND NATURE,SPORTS,TECHNOLOGY
so I have given here like what I want the items to be in that order
expandableListDetail.put("BUSINESS", business);
expandableListDetail.put("ENTERTAINMENT",entertainment);
expandableListDetail.put("GAMING",gaming);
expandableListDetail.put("GENERAL", general);
expandableListDetail.put("MUSIC", music);
expandableListDetail.put("SCIENCE AND NATURE", sciandnat);
expandableListDetail.put("SPORTS", sports);
expandableListDetail.put("TECHNOLOGY", technology);
but when I see the output it is like this
output image
Why??
but when i see the output it is like this, why
Because you are using a HashMap as Map fro your dataset, which doesn't preserve the inserti order of its items. Use a LinkedHashMap which preserves the insertion order. Change
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
with
Map<String, List<String>> expandableListDetail = new LinkedHashMap<String, List<String>>();
ArrayList<String> parsedPODAY_ID=new ArrayList<>();
ArrayList<String> parsedPODAY_P_IMG=new ArrayList<>();
ArrayList<String> parsedPODAY_NAME=new ArrayList<>();
ArrayList<String> parsedPODAY_PRICE=new ArrayList<>();
ArrayList<String> parsedPODAY_OFFPRICE=new ArrayList<>();
ArrayList<ArrayList<String>> fullPoday = new ArrayList<>();
ArrayList<ArrayList<String>> fullPoday = new ArrayList<>();
fullPoday.add(parsedPODAY_ID);
fullPoday.add(parsedPODAY_NAME);
fullPoday.add(parsedPODAY_P_IMG);
fullPoday.add(parsedPODAY_PRICE);
fullPoday.add(parsedPODAY_OFFPRICE);
I am having array of arraylist as fullPoday.But in another activity how can i get each index of array list
fullPoday.get(0).add("Test string 1");
String result = fullPoday.get(0).get(0);
Log.d("tag", result);
But you try manipulate with string tables. May be better to use data base mechanics.
I'm working on an app where user will be able to view a list with names, and when he clicks on one, another activity pops up with more details regarding the chosen profile. I've got a ListView. I create it using this code:
listContacts = (ListView) findViewById(R.id.listContacts);
data = new ArrayList<HashMap<String, String>>();
db = new DatabaseHandler(this);
List<Contact> contacts = db.getAllContacts();
List<String> names = new ArrayList<>();
List<String> sex = new ArrayList<>();
List<String> age = new ArrayList<>();
List<Integer> id = new ArrayList<>();
List<String> sexnage = new ArrayList<>();
for (Contact cn : contacts) {
names.add(cn.getName() + " " + cn.getSurname());
sex.add(cn.getSex());
age.add(cn.getAge());
id.add(cn.getID());
sexnage.add(cn.getSex() + " " + cn.getAge());
}
titleArray = new String[names.size()];
titleArray = names.toArray(titleArray);
subItemArray = new String[sexnage.size()];
subItemArray = sexnage.toArray(subItemArray);
for(int i=0;i<titleArray.length;i++){
HashMap<String,String> datum = new HashMap<String, String>();
datum.put("Name", titleArray[i]);
datum.put("Sex and Age", subItemArray[i]);
data.add(datum);
}
SimpleAdapter subAdapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2,
new String[] {"Name", "Sex and Age"}, new int[]
{android.R.id.text1, android.R.id.text2});
listContacts.setAdapter(subAdapter);
How can I set ID of each element in List, so that it corresponds to Database one?
Use a SimpleCursorAdapter as listview adapter.
if supplier id is !=0 then
if(user.getString("iSupplierID")!="0")
{
String SUPPLIER_NAME=user.getString(TAG_NAME);
String PRIMARY_SERVICE_CATEGORY = user.getString(TAG_PRIMARY);
String SECONDARY_SERVICE_CATEGORY = user.getString(TAG_SECONDARY);
String SOURCE_DIRECTORY_TYPE = user.getString(TAG_SOURCE);
Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NAME, SUPPLIER_NAME);
map.put(TAG_PRIMARY, PRIMARY_SERVICE_CATEGORY);
map.put(TAG_SECONDARY, SECONDARY_SERVICE_CATEGORY);
map.put(TAG_SOURCE, SOURCE_DIRECTORY_TYPE);
supplierlist.add(map);
creating an id for list view
lv=(ListView)findViewById(R.id.listView1);
listview adapter
ListAdapter adapter = new SimpleAdapter(SupplierDetails.this, supplierlist,
R.layout.list_view,
new String[] { TAG_NAME,TAG_PRIMARY,TAG_SECONDARY, TAG_SOURCE }, new int[] {
R.id.primary,R.id.secondary, R.id.source});
lv.setAdapter(adapter);