TrainShedule.java
public class TrainShedule extends AppCompatActivity {
ListView listview;
private DatabaseReference traindb;
List<TrainTimeTable> tlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_train_shedule);
listview = (ListView)findViewById(R.id.trainlist);
traindb = FirebaseDatabase.getInstance().getReference("TrainTT");
tlist = new ArrayList<>();
}
@Override
protected void onStart()
{
super.onStart();
traindb.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
tlist.clear();
for(DataSnapshot trainsnapshot: dataSnapshot.getChildren())
{
TrainTimeTable ttt = trainsnapshot.getValue(TrainTimeTable.class);
tlist.add(ttt);
}
Trainlist adapter = new Trainlist(TrainShedule.this,tlist);
listview.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
TrainTimeTable.java
public class TrainTimeTable
{
private String id;
private String to;
private String from;
private String time;
private String type;
public TrainTimeTable(String id,String to, String from, String time, String type) {
this.id = id;
this.to = to;
this.from = from;
this.time = time;
this.type = type;
}
public String getId() {
return id;
}
public String getType() {
return type;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
}
Trainlist.java
public class Trainlist extends ArrayAdapter<TrainTimeTable>
{
private Activity context ;
private List<TrainTimeTable> trainlist;
public Trainlist(Activity context, List<TrainTimeTable> trainlist) {
super(context, R.layout.shedulelayout,trainlist);
this.context= context;
this.trainlist=trainlist;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem =inflater.inflate(R.layout.shedulelayout, null);
TextView fromsource=(TextView) listViewItem.findViewById(R.id.from);
TextView todestination=(TextView) listViewItem.findViewById(R.id.to);
TextView traintime=(TextView) listViewItem.findViewById(R.id.time);
TextView speedtype=(TextView) listViewItem.findViewById(R.id.type);
final TrainTimeTable traintt = trainlist.get(position);
fromsource.setText(traintt.getFrom());
todestination.setText(traintt.getTo());
traintime.setText(traintt.getTime());
speedtype.setText(traintt.getType());
return listViewItem;
}
}
data is easily been added to the database but I can't fetch the data from the database in the list view I can't find any error in the code.
when I open the list view activity the app goes blank and redirects back to the Main activity.
please reply if you can help.......