Firebase数据库更改节点ID

时间:2017-11-18 04:21:23

标签: android json firebase firebase-realtime-database hashmap

如何在下图中更改孩子节点的命名? questions_statsList<Integer>,我知道我将整数作为节点ID,因为这是一个List。我用0到1000之间的数字随机创建每个子节点。我将此ID设置为对象的一部分并找到它我循环通过列表。我想要的是在创建它时将“0671”设置为对象的键。

如何定义我的对象以便使用我定义为String的Id访问每个子节点。

每个questions_stats都是一个对象。

这是我的UserProfile类定义。

public class UserProfile implements Parcelable {
    private List<Integer> questions_list;
    private List<QuestionsStats> questions_stats;
    private String country_name, share_code, user_name;
    private int token_count;
    private Boolean is_guest;

    public UserProfile() {
    }

    public UserProfile(List<Integer> questions_list, List<QuestionsStats> questions_stats, String country_name, String share_code, String user_name, int token_count, Boolean is_guest) {
        this.questions_list = questions_list;
        this.questions_stats = questions_stats;
        this.country_name = country_name;
        this.share_code = share_code;
        this.user_name = user_name;
        this.token_count = token_count;
        this.is_guest = is_guest;
    }
 }

我知道我可以单独使用child("0159").setValue(QuestionStats)设置它们。

但出于我的目的,我需要整体检索 “user” 的数据,然后在questions_stats中进行迭代,就像它是一个List一样。

我应该如何定义我的UserProfile类以实现我想要的目标?

任何人都可以给我一个暗示吗?

Sample

2 个答案:

答案 0 :(得分:0)

data = pd.read_csv("data.csv", header=0, delimiter="\t", quoting=3, encoding="utf-8") y = data.label X_train, X_test, y_train, y_test = train_test_split(data, y, test_size=0.2) def data_genereator(data, batch_size): num_rows = int(data.shape[0]) # Initialize a counter counter = 0 while True: for content, label in zip(data['content'], data['label']): X_train[counter%batch_size] = transform(content) y_train[counter%batch_size] = np.asarray(label) counter = counter + 1 if(counter%batch_size == 0): yield X_train, y_train training_generator = data_genereator(X_train, batch_size=1024) validation_generator = data_genereator(X_test, batch_size=1024) model = Sequential() model.add(LSTM(64, input_shape=(1000, 2400), return_sequences=False, kernel_initializer='he_normal', dropout=0.15, recurrent_dropout=0.15, implementation=2)) model.add(Dropout(0.3)) model.add(Dense(1, activation = 'sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit_generator(training_generator, steps_per_epoch=8000, validation_data=validation_generator, epochs=3, verbose=1, workers=1, use_multiprocessing=False, validation_steps=2000)

答案: How can I change the node names of my children in the image below? 可以在其中更改Firebase数据库中节点的名称。这样做没有API。你可以做的是在该节点上附加一个监听器并获取no way对象。拥有该数据后,您可以使用其他名称将其写入其他位置。您不能简单地将它们重命名为dataSnapshotfrom 0 to 0000等等。

答案 1 :(得分:0)

也许我应该问过如何&#34; Set&#34;节点ID而不是&#34;更改&#34; 我所拥有的是List<QuestionsStats>,但是当使用List<QuestionsStats>时,您将索引作为键,我想要的是具有相同的List<QuestionsStats>,而不是索引,每个我的字符串键项目。 所以我将我的列表更改为Map<String, QuestionsStats>。现在最棘手的部分是分配Object。您可以在@David Wasser的回答中使用 readMap() writeMap() 来包裹here,但会发出警告:

  

请改用writeBundle(Bundle)。将地图展平为包裹   在当前的dataPosition()中,如果需要,增加dataCapacity()。该   映射键必须是String对象。 Map值使用   writeValue(Object)并且必须遵循那里的规范。它是   强烈建议使用writeBundle(Bundle)代替此   方法,因为Bundle类提供了允许的类型安全API   你可以避免在编组时出现神秘的类型错误。

因此,在我This Question的评论的帮助下,我使用此代码进行了分析,请注意我已经离开了&#34; easy&#34;方式评论,如果有人发现它有用或对此有任何评论:

    protected UserProfile(Parcel in) {
//        in.readMap(myMap, Object.class.getClassLoader());
        myMap = new HashMap<>();
        String[] array = in.createStringArray();
        Bundle bundle = in.readBundle(Object.class.getClassLoader());
        for (String s : array) {
            myMap.put(s, (Object) bundle.getParcelable(s));
        }
    }


    @Override
    public void writeToParcel(Parcel dest, int flags) {


    //        dest.writeMap(myMap);
            Bundle bundle = new Bundle();
            for (Map.Entry<String, Object> entry : myMap.entrySet()) {
                bundle.putParcelable(entry.getKey(), entry.getValue());
            }
            Set<String> keySet = myMap.keySet();
            String[] array = keySet.toArray(new String[keySet.size()]);
            dest.writeStringArray(array);
            dest.writeBundle(bundle);
        }

为什么我想要这个,目前我的列表中包含的项目少于100个,但它可以长到1000个,我没有Pro,但我相信如果我已经知道项目的关键我和#39;我感兴趣的总是比迭代列表找到它更好。最后我的主要问题是Map的使用,我不知道怎么做。