如何从文本文件中获取价值并与Firebase链接

时间:2018-11-21 13:16:58

标签: java android firebase firebase-realtime-database

我有一个文本文件,其中包含userPhone和相关的foodId,以便在菜单应用程序中进行推荐。 我已经基于每个userPhone登录成功读取了相关的foodId。如何从文本文件中获取foodId并从Firebase检索以显示在回收者视图中? foodId存储在firebase中。 (不同的用户登录名将根据每个电话号码显示相关的foodId)。我只是一个初学者。预先感谢

.txt文件

0122233444=47
0125547890=48
9876543210=54,29,49

Recommendation.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recommendation);

    TextView recList = (TextView)findViewById(R.id.rec_list);

    //Firebase
    database = FirebaseDatabase.getInstance();

    String userPhone = Common.currentUser.getPhone();
    String recommend = findRecommendation(userPhone);

    if (recommend != null) {
        recList.setText(recommend);
    } else {
        recList.setText("Not enough transaction");
    }
}

private String findRecommendation(String userPhone) {

    //open raw resource and connect to input stream variable
    InputStream input = getResources().openRawResource(R.raw.foodrecommendation);

    //scanner
    Scanner scan = new Scanner(input);

    while (scan.hasNext()) {
        String line = scan.nextLine(); //phoneNo + foodId
        String [] pieces = line.split("=");
        if (pieces[0].equalsIgnoreCase(userPhone.trim())) {
            return pieces[1];
        }
    }

    return null;
}

Firebase结构

foodId

user phone number

0 个答案:

没有答案