Эй, я новичок в работе с Android и взял это приложение, чтобы я следовал учебнику, а учебник читает и записывает данные из MainActivity. java, пока я пытаюсь работать во фрагменте.
Хотя ошибки нет, приложение ничего не показывает, база данных Firebase Realtime содержит JSON данных, которые не отображаются в RecylerView должным образом.
Вот мои файлы
MainActivity. java
package com.example.plutus;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
private TaskFragment taskFragment;
private TimeFragment timeFragment;
private HabitFragment habitFragment;
private HourFragment hourFragment;
private BottomNavigationView mMainNav;
private FrameLayout mMainFrame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
taskFragment = new TaskFragment();
timeFragment = new TimeFragment();
habitFragment = new HabitFragment();
hourFragment = new HourFragment();
setFragment(taskFragment);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_task:
setFragment(taskFragment);
return true;
case R.id.nav_time:
setFragment(timeFragment);
return true;
case R.id.nav_habit:
setFragment(habitFragment);
return true;
case R.id.nav_hour:
setFragment(hourFragment);
return true;
default:
return false;
}
}
});
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
}
TaskAdapter. java
package com.example.plutus;
import android.content.Context;
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.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.MyViewHolder> {
ValueEventListener context;
ArrayList<MyTasks> myTasks;
public TaskAdapter(ValueEventListener c, ArrayList<MyTasks> p) {
context = c;
myTasks = p;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from((Context) context).inflate(R.layout.task_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.tasktitle.setText(myTasks.get(position).getTasktitle());
}
@Override
public int getItemCount() {
return myTasks.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tasktitle;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tasktitle = (TextView) itemView.findViewById(R.id.tasktitletext);
}
}
}
TaskFragment. java
package com.example.plutus;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
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;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class TaskFragment extends Fragment {
//Firebase Json integration
RecyclerView ourTasks;
ArrayList<MyTasks> list;
TaskAdapter taskAdapter;
public TaskFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_task, container, false);
ourTasks = rootView.findViewById(R.id.ourTasks);
ourTasks.setLayoutManager(new LinearLayoutManager(getActivity()));
list = new ArrayList<MyTasks>();
// Inflate the layout for this fragment
DatabaseReference taskDatabaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference taskuser = taskDatabaseReference.child("Task1").child("tasktitle");
taskuser.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
MyTasks p = dataSnapshot1.getValue(MyTasks.class);
list.add(p);
}
taskAdapter = new TaskAdapter(this, list);
ourTasks.setAdapter(taskAdapter);
taskAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getContext(), "I'm fetching this", Toast.LENGTH_SHORT).show();
}
});
return rootView;
};
}
MyTasks. java
package com.example.plutus;
public class MyTasks {
String tasktitle;
public MyTasks() {
}
public MyTasks(String tasktitle) {
this.tasktitle = tasktitle;
}
public String getTasktitle() {
return tasktitle;
}
public void setTasktitle(String tasktitle) {
this.tasktitle = tasktitle;
}
}
Я не могу понять, где на самом деле проблема.
Редактировать: Включено JSON Файлы.
{
"Plutus":{
"Task1":{
"tasktitle":"Create an awesome app"
},
"Task2":{
"tasktitle":"Learn More Android"
},
"Task3":{
"tasktitle":"Learn Android Pentesting too"
},
"Task4":{
"tasktitle":"Oi don't forget Bug Hunting"
}
}
}