我想将GridView与ArrayList绑定。这是我的适配器:
public class GridAdapter extends BaseAdapter {
ArrayList<Upload>uploads;
ImageView preview;
public GridAdapter (ArrayList<Upload>uploads) {
this.uploads=uploads;
}
@Override
public int getCount() {
return uploads.size();
}
@Override
public Object getItem(int position) {
return uploads.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=layoutInflater.inflate(R.layout.row_data,null);
preview=(ImageView) convertView.findViewById(R.id.gridImageView);
Picasso.with(getApplicationContext()).load(uploads.get(position).getImageUrl()).into(preview);
return null;
}}
这就是我设置适配器的方式以及向ArrayList添加项目的方式
pb = (ProgressBar) findViewById(R.id.pb);
uploadList=new ArrayList<>();
adapter = new GridAdapter(uploadList);
intent=getIntent();
directory=intent.getStringExtra("directory");
gridView= findViewById(R.id.gridView);
mDatabaseRef=FirebaseDatabase.getInstance().getReference("uploads/"+directory);
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren()) {
Upload upload=postSnapshot.getValue(Upload.class);
uploadList.add(upload);
}
gridView.setAdapter(adapter);
pb.setVisibility(View.INVISIBLE);
}
在活动中,没有显示任何内容,没有可单击的内容,因此没有创建GridView项