Firebase数据库部分列表视图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Firebase数据库部分列表视图相关的知识,希望对你有一定的参考价值。
我需要从Firebase(部分列表适配器firebase数据库)中检索数据。比如Firebase数据库。
答案
我用这个适配器
public class MatcmobikraAdapter extends ArrayAdapter<MatchmobikooraModel> {
private static final String TAG = "MatchAdapter";
private LayoutInflater mInflater;
private int mLayoutResource;
private Context mContext;
private Typeface myTypeface;
public MatcmobikraAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<MatchmobikooraModel> objects) {
super(context, resource, objects);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mLayoutResource = resource;
this.mContext = context;
}
static class ViewHolder {
ImageView team1logo, team2logo;
TextView team1, team2, matchtime;
MatchmobikooraModel matchmobikooraModel;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final ViewHolder holder;
if (convertView == null){
convertView = mInflater.inflate(mLayoutResource, parent, false);
holder = new ViewHolder();
holder.matchtime = (TextView) convertView.findViewById(R.id.text_time);
holder.team1 = (TextView) convertView.findViewById(R.id.homescore);
holder.team2 = (TextView) convertView.findViewById(R.id.awayscore);
holder.team1logo = (ImageView) convertView.findViewById(R.id.homeicon);
holder.team2logo = (ImageView) convertView.findViewById(R.id.awayicon);
holder.matchmobikooraModel = getItem(position);
myTypeface = Typeface.createFromAsset(mContext.getAssets(), "bein.ttf");
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
// MatchmobikooraModel matchmobikooraModel = getItem(position);//Modelmatch modelmatch = getItem(position);
holder.team1.setText(getItem(position).getTeam1());
holder.team1.setTypeface(myTypeface);
holder.team2.setText(getItem(position).getTeam2());
holder.team2.setTypeface(myTypeface);
holder.matchtime.setText(getItem(position).getMatch_time());
holder.matchtime.setTypeface(myTypeface);
PicassoClient.downloadimg(mContext, getItem(position).getTeam1logo(), holder.team1logo);
PicassoClient.downloadimg(mContext, getItem(position).getTeam2logo(), holder.team2logo);
return convertView;
}
另一答案
这段代码从firebase中检索数据
public class CameraFragment extends Fragment {
private static final String TAG = "Match Fragment";
private ListView mListView;
private ArrayList<MatchmobikooraModel> mMatchmobikooraModels;
private MatcmobikraAdapter matcmobikraAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera, container, false);
mListView = (ListView) view.findViewById(R.id.listMatch3);
mMatchmobikooraModels = new ArrayList<>();
getmobara();
return view;
}
private void getmobara(){
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("match").child("leagues");
Query query = myRef;
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
DataSnapshot dataSnapshot2 = dataSnapshot1.child("league_matches");
for (DataSnapshot dataSnapshot3 : dataSnapshot2.getChildren()){
Log.d(TAG, "found mobara time : " +dataSnapshot3.child("match_time").getValue());
MatchmobikooraModel matchmobikooraModel = new MatchmobikooraModel();
Map<String, Object> objectMap = (HashMap<String, Object>) dataSnapshot3.getValue();
Log.d(TAG, "found match time : " + dataSnapshot3.child("match_time").getValue());
// matchmobikooraModel.setMatch_id(objectMap.get("match_id").toString());
matchmobikooraModel.setMatch_time(objectMap.get("match_time").toString());
matchmobikooraModel.setTeam1(objectMap.get("team1").toString());
matchmobikooraModel.setTeam1logo(objectMap.get("team1logo").toString());
matchmobikooraModel.setTeam2(objectMap.get("team2").toString());
matchmobikooraModel.setTeam2logo(objectMap.get("team2logo").toString());
mMatchmobikooraModels.add(matchmobikooraModel);
}
Log.d(TAG, "onDataChange: get match count " + mMatchmobikooraModels.size());
matcmobikraAdapter = new MatcmobikraAdapter(getActivity(), R.layout.row_game_logos, mMatchmobikooraModels);
mListView.setAdapter(matcmobikraAdapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
另一答案
public class MeetingShow extends AppCompatActivity {
ListView listView;
List<MeetingInfo> list;
ProgressDialog progressDialog;
private DatabaseReference databaseReference;
MeetingAdapter eventAdapter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meeting_show);
listView=findViewById(R.id.meetinglistview);
list=new ArrayList<>();
progressDialog=new ProgressDialog(this);
progressDialog.setTitle("Fetching.... ");
progressDialog.show();
// change according to your node detail
databaseReference= FirebaseDatabase.getInstance().getReference("Meeting");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override public void onDataChange(DataSnapshot dataSnapshot) {
progressDialog.dismiss();
MeetingInfo imageinfo=dataSnapshot.getValue(MeetingInfo.class);
list.clear();
for(DataSnapshot snapshot :dataSnapshot.getChildren())
{
MeetingInfo imageInfo=snapshot.getValue(MeetingInfo.class);
imageInfo.setMeetingkey(snapshot.getKey());
list.add(imageInfo);
}
eventAdapter =new MeetingAdapter(MeetingShow.this,R.layout.activity_meeting_format,list);
listView.setAdapter(eventAdapter);
}
@Override public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MeetingShow.this, databaseError.getCode(),Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> adapterView, final View view, final int i, long l) {
// progressDialog=new ProgressDialog(MeetingShow.this);
view.setBackgroundColor(Color.RED);
AlertDialog.Builder alert = new AlertDialog.Builder(MeetingShow.this);
alert.setTitle("Delete entry");
alert.setMessage("Are you sure you want to delete?");
alert.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//delete entry by clicking on listview item
final MeetingInfo meetingInfo=list.get(i);
final String selecteditem=meetingInfo.getMeetingkey();
databaseReference.child(selecteditem).removeValue();
Toast.makeText(MeetingShow.this,"Deleted",Toast.LENGTH_SHORT).show();
listView.setAdapter(eventAdapter);
}
});
alert.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
view.setBackgroundColor(Color.WHITE); }
});
aler以上是关于Firebase数据库部分列表视图的主要内容,如果未能解决你的问题,请参考以下文章