简体   繁体   中英

ListView HashMap Firebase

i'm trying to populate a firebase listview using a hashmap, but i'm just having trouble figuring out how, and i have a feeling my logic might be broken. This is my code. What this code is "supposed" to do is, catelog an array of spells, charms, etc. in hashmap. The hashmaps, then store all the spells in that in an array list with the key, being the letter the spells started with. The outcome should be that when i populate the view, it should seem a lot like a contact list... but more like spells, where they are grouped together by the letter they start with.

The spell object is just a POJO, with params, name, type, description, first seen/mentioned, notes and entymology. I haven't initialised the adapters, because i don't really know how to structure the adapter to populate the view

Spell[] spell = new Spell[96];
private String curse = "curse";
private String charm = "charm";
private String hex = "hex";
private String spells = "spell";

private HashMap<String, ArrayList<Spell>> spellMap;
private HashMap<String, ArrayList<Spell>> charmMap;
private HashMap<String, ArrayList<Spell>> curseMap;
private HashMap<String, ArrayList<Spell>> hexMap;

public InsertSpellData() {
    spellMap = new HashMap<String, ArrayList<Spell>>();
    //hashMaps are made for charms,hexs, and curses too

    setUpData(spell);
    insertToFirebase(spell);
}

private void insertToFirebase(Spell[] spell) {


    for (int i = 0; i < spell.length; i++) {
        String ch = spell[i].getName().charAt(0) + "";
        if (spell[i].getType().equals(spells)) {
            if(spellMap.get(ch) != null){
                ArrayList<Spell> tempArrayList = spellMap.get(ch);
                tempArrayList.add(spell[i]);
                spellMap.put(ch, tempArrayList);
            }else{
                ArrayList<Spell> tempArrayList = new ArrayList<Spell>();
                tempArrayList.add(spell[i]);
                spellMap.put(ch, tempArrayList);
            }
        } //else if for charm,curse and hex
        }

        Firebase firebase = new Firebase(Constants.FIREBASE_SPELLS_URL);
        Firebase spellRef = firebase.child(spells);
        //Refs for charm, hex, and curse are also created


        //charmMap, curseMap, and hexMap are set to the respective ref's
        spellRef.setValue(spellMap);
    }
}

Adapter:

public class SpellAdapter extends FirebaseListAdapter<HashMap<String, Spell>>{

  public SpellAdapter(Activity activity, Class<HashMap<String, Spell>> modelClass, int modelLayout, Query ref) {
      super(activity, modelClass, modelLayout, ref);
      this.mActivity = activity;
  }

  @Override
  protected void populateView(View view, HashMap<String, Spell> stringSpellHashMap) {
   /*
   No idea what to put here
    */
  }
}

You have been passed a view which is the layout you passed to the constructor inflated. You have been passed the map of data which is synced with firebase.

You look up the views you want to populate on the view, and set the text (or images or whatever) from your domain.

  @Override
  protected void populateView(View view, HashMap<String, Spell> stringSpellHashMap) {
        String spellName = stringSpellHashMap.get("something").getName(); 
        ((TextView) view.findViewById(R.id.my_text_view)).setText(spellName);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM