Hashmap不断返回空值

时间:2018-07-16 13:20:10

标签: java android hashmap

我正在尝试在Firebase android平台上存储和删除数据。每个对象(例如Shirt)都存储由用户写入的标签。因此,我使用了一个哈希映射,它将帮助我将每个标记名称映射到tagHolder对象。这是我的部分代码。

final String name = editTextName.getText().toString().trim();
final String color = editTextColor.getText().toString().trim();
final String category = editTextCategory.getText().toString().trim();
final String tags = editTextTags.getText().toString().trim();
final DatabaseReference databaseRef = mDatabaseRef.child("/" + category);


mUploadTask = fileReference.putFile(imageUri).
                addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
                        fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                // handles the retrieval of tags when user enters each tag seperated by
                                // commas.
                                Shirt shirt = new Shirt(name, color, uri.toString());
                                List<String> stringTag = Arrays.asList(tags.split(","));
                                shirt.setTags(stringTag);

                                // for each tag in the list, upload it onto firebase database.
                                for (String shirtTag : stringTag) {

                                    // Handles the uploading of tags onto firebase for search
                                    // to be conducted.
                                    DatabaseReference dataRef = mDatabaseTagRef.child(shirtTag);
                                    String uniqueId = dataRef.push().getKey();
                                    TagHolder tagHolder = new TagHolder(name,uri.toString());
                                    shirt.putTag(shirtTag,tagHolder); // i put the tag here in the hashmap 
                                    Log.i("AddActivity",shirtTag);
                                    tagHolder.setmKey(uniqueId);
                                    dataRef.child(uniqueId).setValue(tagHolder);
                                }

                                String uploadId = databaseRef.push().getKey();
                                databaseRef.child(uploadId).setValue(shirt);
                            }
                        });

这是我的上衣课。

public class Shirt extends Item {



public Shirt() {}


public Shirt(String mName, String mColor, String mImageUrl) {
    super(mName,mColor,mImageUrl);
    }
}

和物品类别

public class Item {

private String mName;
private String mColor;
private String mImageUrl;
private List<String> tags = new ArrayList<>();
private String mKey;
private HashMap<String,TagHolder> tagHolderHashMap = new HashMap<>();

public Item(){

}



public Item(String mName, String mColor, String mImageUrl) {
    this.mName = mName;
    this.mColor = mColor;
    this.mImageUrl = mImageUrl;
}


public void setmName(String mName) {
    this.mName = mName;
}

public void setmImageUrl(String mImageUrl) {
    this.mImageUrl = mImageUrl;
}

public List<String> getTags() {
    return tags;
}

public String getmColor() {
    return mColor;
}

public String getmImageUrl() {
    return mImageUrl;
}

public void setmColor(String mColor) {
    this.mColor = mColor;
}

public String getmName() {
    return mName;
}

public void setTags(List<String> tags) {
    this.tags = tags;
}

@Exclude // dont need this in our firebase database
public String getKey() {
    return mKey;
}

@Exclude
public void setMkey(String key) {
    mKey = key;
}

public void putTag(String tag, TagHolder tagHolder) {
    tagHolderHashMap.put(tag,tagHolder);
}

public TagHolder retrieveTagHolder(String tag) {
    return tagHolderHashMap.get(tag);
    }
}

此代码用于在我要删除衬衫时检索标签夹,因为我也需要在firebase中删除tagHolder。

 List<Shirt> deletables = shirtAdapter.returnDeletables();
            for (final Shirt shirt : deletables) {
                final String selectedKey = shirt.getKey();
                StorageReference shirtStorageReference = mStorageReference.child("Shirts");
                StorageReference imageRef = FirebaseStorage.getInstance()
                        .getReferenceFromUrl(shirt.getmImageUrl());
                List<String> tags = shirt.getTags();
                for (String tag : tags) {
                    // points to appa,testing123,testing456 etc
                    final DatabaseReference tagRef = mDatabaseTagRef.child("/" + tag);
                    Log.i("ShirtActivity",tag);
                    // tag holder for each shirt that has this tag
                    TagHolder tagHolder = shirt.retrieveTagHolder(tag); // tagHolder is null
                    if (tagHolder == null) {
                        Log.i("ShirtActivity","tagHolder is null");
                    }
                    tagRef.child(tagHolder.getmKey()).removeValue();
                }
                imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        mDatabaseReference.child(selectedKey).removeValue();
                    }
                });

每次我尝试从哈希图中获取tagHolder时,即使我之前输入了值,它也会返回null。对不起,这么长的帖子,我希望所有阅读的读者都明白!

这是我的收听者,每次添加衬衫时都不确定是否会导致问题。

mDatabaseListener = mDatabaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mShirtList.clear();
            for (DataSnapshot postSnapShot : dataSnapshot.getChildren()) {
                Shirt shirt = postSnapShot.getValue(Shirt.class);
                shirt.setMkey(postSnapShot.getKey());
                mShirtList.add(shirt);
            }
            shirtAdapter.notifyDataSetChanged();
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

0 个答案:

没有答案