我正在尝试实现一个简单的TextView-Database-Changer。但这不起作用,我真的尝试了许多不同的种类,即: yourGroupId =(TextView)view.findViewById(R.id.groupid); yourGroupId.setText(currentGroupID);
文本不会改变...
我已经尝试过不同的样式,这是我在SO上找到的。没有任何结果。我也尝试了一些手动输入,例如yourGroupId.setText(“ HAI”);
这是片段帐户
package com.nic.htv;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class frag_account extends Fragment {
private Spinner spinner;
private static final String[] paths = {"item 1", "item 2", "item 3"};
private DatabaseReference UsersRef;
private String currentUserID, currentGroupID;
View view;
TextView yourGroupId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_account, container, false);
currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
UsersRef.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot)
{
currentGroupID = dataSnapshot.child("group_1").getValue(String.class);
//currentGroupID = dataSnapshot.child("group_1").getValue(String.class);
// dataSnapshot.child("image").getValue().toString();
yourGroupId = (TextView)view.findViewById(R.id.groupid);
yourGroupId.setText(""+currentGroupID);
UserInformation user_i = new UserInformation();
user_i.setGroupId(currentGroupID);
list_account_data(user_i);
}
public void onCancelled(DatabaseError error)
{
// Failed to read value
Log.w("New Tag", "Failed to read value.", error.toException());
}
});
return inflater.inflate(R.layout.fragment_account, container, false);
}
public void list_account_data(UserInformation user_info)
{
final String groupId = user_info.getGroupId();
Log.d("New Tag", "GroupID-Inside list_acc_data is: " + currentGroupID);
Log.d("New Tag", "Value is: " + groupId);
}
}
这是帐户XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".frag_account">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#03A9F4">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account" />
</LinearLayout>
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/appversion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="App-Version"
android:textStyle="bold" />
<TextView
android:id="@+id/appversionnr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_version"
android:layout_marginLeft="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/yourMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Deine Email"
android:textStyle="bold" />
<TextView
android:id="@+id/yourmaildb"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/yourMail"
android:layout_marginLeft="20dp" />
<TextView
android:id="@+id/groupselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/header"
android:text="Gruppenauswahl"
android:textStyle="bold" />
<TextView
android:id="@+id/groupid"
android:text="Gruppen-ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/header"
android:layout_marginLeft="20dp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
答案 0 :(得分:2)
在Fragment的onCreateView()
中,您正在将布局视图变量创建为“ view”(如下所示),它应该位于返回值中。但是,在此方法块的最后,您将返回一个新的视图实例,例如inflater.inflate(R.layout.fragment_account, container, false)
。因此,将这个新实例替换为已经创建的view
。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_account, container, false);
.....rest
return view; //replace this instead of creating a new view variable using inflater
}