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;
}
}
数据很容易添加到数据库中但我无法从列表视图中的数据库中获取数据我在代码中找不到任何错误。 当我打开列表视图活动时,应用程序变为空白并重定向回主要活动 如果你能提供帮助请回复.......
答案 0 :(得分:0)
getCount方法用于获取数组中的项目数。对于ArrayLists,您使用size方法。将此行添加到适配器
public int getCount() {
return trainlist.size( );
}
答案 1 :(得分:0)
我认为主要问题在于
private Activity context ;
这应该是Context
,而不是Activity
。
将此代码更改为
private Context mContext ;
public Trainlist(Context context, List<TrainTimeTable> trainlist) {
super(context, R.layout.shedulelayout,trainlist);
this.mContext = context;
this.trainlist = trainlist;
}
然后使用mContext
来扩充布局。
LayoutInflater inflater = LayoutInflater.from(mContext);
View listViewItem = inflater.inflate(R.layout.row_item, parent, false);
试试这个。希望这会有所帮助。
答案 2 :(得分:0)
在课程开始时制作Adpater实例 在获取数据快照时, 直接执行adapter.add(object),以便从firebase返回的每个对象都将传入listview适配器并传递空列表。
这里获取listview为空的问题是,flow将首先在datasnapshot方法中一次并直接设置listview,之后你得到的任何数据都没有进入listview, 尝试在onPause()方法中设置listview并使你的应用程序暂停,返回该活动,你将获得listview输出, 所以你应该直接使用adapter.add()方法。