Я все еще новичок ie в android студии. Можете ли вы, ребята, помочь мне.
, почему мой код не может отображать данные из Firebase? Я буду признателен за тех, кто хочет помочь мне.
, если вы хотите видеть мои файлы res на android studio, напишите об этом, если хотите.
это мой onCreate Метод.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init Firestore
db = FirebaseFirestore.getInstance();
//View
dialog = new SpotsDialog(this);
title = (MaterialEditText) findViewById(R.id.title);
description = (MaterialEditText) findViewById(R.id.description);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//ADD new
if (!isUpdate) {
setData(title.getText().toString(), description.getText().toString());
} else {
updateData(title.getText().toString(), description.getText().toString());
isUpdate = !isUpdate; //reset flag
}
}
});
listItem = (RecyclerView) findViewById(R.id.listTodo);
listItem.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
listItem.setLayoutManager(layoutManager);
loadData(); //load data from firestore
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle().equals("DELETE"))
deleteItem(item.getOrder());
return super.onContextItemSelected(item);
}
private void deleteItem(int index) {
db.collection("ToDoList")
.document(toDoList.get(index).getId())
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
loadData();
}
});
}
private void updateData(String title, String description) {
db.collection("ToDoList").document(idUpdate)
.update("title", title,"Description",description)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Updated!", Toast.LENGTH_SHORT).show();
}
});
//RealTime Update refresh data
db.collection("ToDoList").document(idUpdate)
.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
loadData(); //load DAta from firestore
}
});
}
private void setData(String title, String description) {
//random id
String id = UUID.randomUUID().toString();
Map<String,Object> todo = new HashMap<>();
todo.put("id",id);
todo.put("title",title);
todo.put("description",description);
//database algo
db.collection("ToDoList").document(id)
.set(todo).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//refresh data
loadData();
}
});
}
private void loadData(){
dialog.show();
if(toDoList.size() > 0)
toDoList.clear(); //Remove old value
db.collection("toDoList")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for(DocumentSnapshot doc:task.getResult()){
ToDo todo = new ToDo (doc.getString("id"),
doc.getString("title"),
doc.getString("description"));
toDoList.add(todo);
}
adapter = new ListItemAdapter(MainActivity.this,toDoList);
listItem.setAdapter(adapter);
dialog.dismiss();
}
}).addOnFailureListener(new OnFailureListener(){
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
это мой класс ListItemAdapter java.
package com.core.techtaskapp.Adapter;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.core.techtaskapp.MainActivity;
import com.core.techtaskapp.Model.ToDo;
import com.core.techtaskapp.R;
import java.util.List;
class ListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener{
ItemClickListener itemClickListener;
TextView item_title, item_description;
public ListItemViewHolder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(this);
itemView.setOnCreateContextMenuListener(this);
item_title = itemView.findViewById(R.id.item_title);
item_description = itemView.findViewById(R.id.item_description);
}
@Override
public void onClick(View view) {
itemClickListener.onClick(view, getAdapterPosition(),false);
}
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
contextMenu.setHeaderTitle("Select action");
contextMenu.add(0,0,getAdapterPosition(),"DELETE");
}
}
public class ListItemAdapter extends RecyclerView.Adapter<ListItemViewHolder>{
MainActivity mainActivity; //naka base aata ito sa mismong java activity
List<ToDo> toDoList;
public ListItemAdapter(MainActivity mainActivity, List<ToDo> toDoList) {
this.mainActivity = mainActivity;
this.toDoList = toDoList;
}
@NonNull
@Override
public ListItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mainActivity.getBaseContext());
View view = inflater.inflate(R.layout.list_item,parent,false);
return new ListItemViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ListItemViewHolder holder, int position) {
mainActivity.title.setText(toDoList.get(position).getTitle());
mainActivity.description.setText(toDoList.get(position).getDescription());
mainActivity.isUpdate = true; //set flag is update = true
mainActivity.idUpdate = toDoList.get(position).getId();
holder.item_title.setText(toDoList.get(position).getId());
holder.item_description.setText(toDoList.get(position).getDescription());
}
@Override
public int getItemCount() {
return toDoList.size();
}
}
Я всегда получаю эту ошибку, когда отправляю данные в базу данных и извлекаю их из базы.
2020-03-11 19:55:44.040 28822-28822/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-03-11 19:55:44.042 28822-28822/? E/Zygote: accessInfo : 1
2020-03-11 19:55:45.251 28822-28822/com.core.techtaskapp E/RecyclerView: No adapter attached; skipping layout
2020-03-11 19:55:47.643 28822-28822/com.core.techtaskapp E/ViewRootImpl: sendUserActionEvent() returned.