1

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.......

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50

3 Answers3

0

The getCount method is used to get how many items are in your array. For ArrayLists, you use the size method. Add this line to the adapter

public int getCount() {
   return trainlist.size( );
}
Jaymin
  • 2,879
  • 3
  • 19
  • 35
0

I think the main problem is here

private Activity context ;

This should be Context, not Activity.

Change this code to

private Context mContext ;

public Trainlist(Context context, List<TrainTimeTable> trainlist) {
    super(context, R.layout.shedulelayout,trainlist);
    this.mContext = context;
    this.trainlist = trainlist;    
}

and then use mContext for inflating layout.

 LayoutInflater inflater = LayoutInflater.from(mContext);
 View listViewItem = inflater.inflate(R.layout.row_item, parent, false);

Try this. Hope this will help.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
0

Make Adpater instance in the starting of class And when getting datasnapshot, Do adapter.add(object) directly,so that for every object returning from firebase will pass in the listview adapter and pass empty list.

Here problem of getting listview empty is, flow will go first in datasnapshot method once and directly set listview, after that whatever data you are getting is not going in listview, Try to set listview in onPause() method and make your app pause, return that acticity, you will get listview output, So that you should directly use adapter.add() method.

Denish Rana
  • 101
  • 2
  • 11