这是一个简单的方法。我正在搜索准确定义表标题的位置。
ATM就像
“ post_05”:{
“ -LjaxpXonCsmzIrdR89M”:{
“ description”:“ 5. Beschreibung”,
“ image”:“ http”,
“ member”:“ 5. Post”,
“ name”:“ 5. Beschreibung”,
“状态”:“在线”,
“时间戳记”:1562948098382,
“ title”:“ 5.发表”
}}
要么
“ -LjaxpXonCsmzIrdR89M”:{
“ description”:“ 5. Beschreibung”,
“ image”:“ http”,
“ member”:“ 5. Post”,
“ name”:“ 5. Beschreibung”,
“状态”:“在线”,
“时间戳记”:1562948098382,
“ title”:“ 5.发表”
}
但我想要这样:
“ post_05”:{
“ description”:“ 5. Beschreibung”,
“ image”:“ http”,
“ member”:“ 5. Post”,
“ name”:“ 5. Beschreibung”,
“状态”:“在线”,
“时间戳记”:1562948098382,
“ title”:“ 5.发表”
}
所以我的问题是,如何停止这种自动创建?
谢谢!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.posts_newpost_activity);
btnCreate = (Button) findViewById(R.id.new_post_btn);
etTitle = (EditText) findViewById(R.id.new_post_title);
etImage = (EditText) findViewById(R.id.new_post_image);
etDescription = (EditText) findViewById(R.id.new_post_description);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
FirebaseUser mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (mFirebaseUser != null)
{
currentUserID = mFirebaseUser.getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
DBRef = FirebaseDatabase.getInstance().getReference();
}
// Read from the USER database
fAuth = FirebaseAuth.getInstance();
btnCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DBRef.addValueEventListener(new ValueEventListener()
{
public void onDataChange(DataSnapshot dataSnapshot) {
//currentGroupID = dataSnapshot.child("group_1").getValue().toString();
currentGroupID = dataSnapshot.child("Users").child(currentUserID).child("group_1").getValue().toString();
size = (int) dataSnapshot.child("post_list").child(currentGroupID).getChildrenCount();
/*
try
{
//postID = getIntent().getStringExtra("noteId");
post_count = size+1;
String text = (post_count < 10 ? "0" : "") + post_count;
postID = "post_"+text;
//Toast.makeText(this, noteID, Toast.LENGTH_SHORT).show();
if (!postID.trim().equals("")) {
isExist = true;
} else {
isExist = false;
}
} catch (Exception e) {
e.printStackTrace();
}
UserInformation user_i = new UserInformation();
user_i.setGroupId(currentGroupID);
user_i.setpostID(postID);
putData(user_i);
//getGroupID(currentGroupID);
*/
}
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("New Tag", "Failed to read value.", error.toException());
}
});
String title = etTitle.getText().toString().trim();
String image = etImage.getText().toString().trim();
String description = etDescription.getText().toString().trim();
final String groupId = currentGroupID;
Log.w("New Tag", "Ln 143 currentGruppID: "+currentGroupID);
Log.w("New Tag", "Ln 144 currentPostID: "+postID);
if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(description)) {createPost(title, description, groupId, image, postID);}
else {Snackbar.make(view, "Fill empty fields", Snackbar.LENGTH_SHORT).show();}
}
});
if(mFirebaseUser != null)
{
// Read from the database
DBRef.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
currentGroupID = dataSnapshot.child("Users").child(currentUserID).child("group_1").getValue().toString();
size = (int) dataSnapshot.child("post_list").child(currentGroupID).getChildrenCount();
post_count = size+1;
String text = (post_count < 10 ? "0" : "") + post_count;
postID = "post_"+text;
UserInformation user_group = new UserInformation();
user_group.setGroupId(currentGroupID);
UserInformation user_key = new UserInformation();
user_key.setpostID(postID);
Log.d("New Tag", "onCreate()Ln 171 GroupID is: " + currentGroupID);
Log.d("New Tag", "onCreate()Ln 171 postID is: " + postID);
putData(user_group, user_key);
}
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("New Tag", "Failed to read value.", error.toException());
}
});
}
}
private void putData(UserInformation user_group, UserInformation user_key)
{
final String groupId = user_group.getGroupId();
final String postID = user_key.getpostID();
Log.w("New Tag", "puData() 189 is the currentGruppID: "+groupId);
Log.w("New Tag", "puData() 189 is the currentPostID: "+postID);
fPostsDatabase = FirebaseDatabase.getInstance().getReference().child("post_list").child(groupId);
if (isExist)
{
fPostsDatabase.child(postID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if (dataSnapshot.hasChild("title") && dataSnapshot.hasChild("description")) {
//postID = "post_" + post_count;
Log.w("New Tag", "The post-Count is: " + post_count);
String title = dataSnapshot.child("title").getValue().toString();
Toast.makeText(posts_newPost_Activity.this, "Post Details" + postID, Toast.LENGTH_SHORT).show();
String description = dataSnapshot.child("description").getValue().toString();
String status = dataSnapshot.child("status").getValue().toString();
String name = dataSnapshot.child("name").getValue().toString();
String image = dataSnapshot.child("image").getValue().toString();
if (isExist) {
etTitle.setText(title);
etDescription.setText(description);
//etStatus.setText(status);
//etName.setText(name);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(posts_newPost_Activity.this, "Database Error Ln 206", Toast.LENGTH_SHORT).show();
}
});
}
}
好的,这是creat脚本...我认为它应该在该处发生。
private void createPost(String title, String description, String group_id, String image, String key) {
final String groupId = group_id;
Log.d("New Tag", "GROUPiD Ln 253 is: " + groupId);
UserInformation user_key = new UserInformation();
postID = key;
Log.d("New Tag", "postID Ln 253 is: " + postID);
Log.d("New Tag", "postID Ln 253 is: " + postID);
fConDatabase = FirebaseDatabase.getInstance().getReference().child("Connection").child(groupId).child("posts");
fPostsDatabase = FirebaseDatabase.getInstance().getReference().child("post_list").child(groupId);
if (fAuth.getCurrentUser() != null) {
if (isExist) {
// UPDATE A Post
Map updateMap = new HashMap();
updateMap.put("title", etTitle.getText().toString().trim());
updateMap.put("description", etDescription.getText().toString().trim());
updateMap.put("name", etTitle.getText().toString().trim());
updateMap.put("timestamp", -1 * new Date().getTime());
updateMap.put("image", etImage.getText().toString().trim());
updateMap.put("status", "Online");
fPostsDatabase.child(postID).updateChildren(updateMap);
fConDatabase.child(postID).updateChildren(updateMap);
Toast.makeText(this, "Post updated (UPDATE).", Toast.LENGTH_SHORT).show();
} else {
// CREATE A NEW Post
final DatabaseReference newPostRef = fPostsDatabase.push();
final DatabaseReference newPostCon = fConDatabase.push();
final Map postMap = new HashMap();
postMap.put("title", title);
postMap.put("description", description);
postMap.put("name", description );
postMap.put("image", image);
postMap.put("status", "Online");
postMap.put("member", title);
postMap.put("timestamp", ServerValue.TIMESTAMP);
final Map postMap2 = new HashMap();
postMap2.put("Posts", "Saved");
Thread mainThread = new Thread(new Runnable() {
@Override
public void run() {
newPostRef.setValue(postMap).addOnCompleteListener(new OnCompleteListener<Void>() {
Thread mainThread2 = new Thread(new Runnable() {
@Override
public void run() {
newPostCon.setValue(postMap2).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(posts_newPost_Activity.this, "Post added to database1", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(posts_newPost_Activity.this, "ERROR: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
//ENDE 2ter aufruf
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(posts_newPost_Activity.this, "Post added to database2", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(posts_newPost_Activity.this, "ERROR: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
mainThread2.start();
}
});
}
});
mainThread.start();
}
} else {Toast.makeText(this, "USERS IS NOT SIGNED IN", Toast.LENGTH_SHORT).show();}
}
最佳答案
-L...
键来自对push()
的调用。如果要完全在JSON中跳过该级别,则可能需要寻找:
final DatabaseReference newPostRef = fPostsDatabase;
假设您的
fPostsDatabase
已经包含了您共享的JSON中显示的post_05
密钥。