实施自定义GridView

时间:2018-11-04 22:10:03

标签: android firebase android-studio firebase-realtime-database

我有一个GridView适配器,还有一个我要插入到其中的自定义对象。

I've been using this tutorial as reference.

这不允许我将ArrayList放在GridAdapter中:

违规行

GridAdapter adapter = new GridAdapter(this, contactList);

给出的错误:

GridAdapter (android.content.Context, 
com.example.user.myapp.CustomContact[])in GridAdapter cannot be applied
to (anonymous com.google.firebase.database.ValueEventListener,
java.util.ArrayList<com.example.user.myapp.CustomContact>)

这是整个ContactsActivity.java

public class ContactsActivity extends AppCompatActivity {
private DatabaseReference mFriendDatabase;
private DatabaseReference mRootRef;
private DatabaseReference mUsersDatabase;
private FirebaseAuth mAuth;
private String mCurrent_user_id;
private View mMainView;
private RecyclerView mFriendsList;
private ArrayAdapter dataAdapter;
private RelativeLayout individual_contact;
private GridView contacts_list;




protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);

    ArrayList<CustomContact> contactList = new ArrayList<>();


    mAuth = FirebaseAuth.getInstance();
    mRootRef = FirebaseDatabase.getInstance().getReference();
    mCurrent_user_id = mAuth.getCurrentUser().getUid();
    mFriendDatabase = FirebaseDatabase.getInstance().getReference().child("friends").child(mCurrent_user_id);
    mFriendDatabase.keepSynced(true);



    contacts_list = (GridView) findViewById(R.id.scrollView1);



    individual_contact = (RelativeLayout) findViewById(R.id.mylist);


    DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("users");







    usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot friendKeySnapshot: dataSnapshot.getChildren()) {
                String friendKey = friendKeySnapshot.getKey();



                usersRef.child(friendKey).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot friendSnapshot) {
                        String friendName = friendSnapshot.child("username").getValue(String.class);
                        String friendPicture = friendSnapshot.child("image_url").getValue(String.class);
                        contactList.add(
                                new CustomContact(friendName, friendPicture)
                        );

                        GridAdapter adapter = new GridAdapter(this, contactList);
                        contacts_list.setAdapter(adapter);

                        //add event listener so we can handle clicks
                        AdapterView.OnItemClickListener adapterViewListener = new AdapterView.OnItemClickListener() {

                            //on click
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                                CustomContact customContact = contactList.get(position);

                                Intent intent = new Intent(ContactsActivity.this, MessageActivity.class);
                                intent.putExtra("username", customContact.getUsername());
                                intent.putExtra("ImageURL", customContact.getImageURL());

                                startActivity(intent);
                            }
                        };
                        //set the listener to the list view
                        contacts_list.setOnItemClickListener(adapterViewListener);



                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        throw databaseError.toException();
                    }
                });
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
}

}

编辑#1:

这是我的CustomContact.java

public class CustomContact {

//Store username string
private String username;

//Store image url
private String ImageURL;

private Boolean featured;

//Constructor
public CustomContact(String username, String ImageURL){
    this.username = username;
    this.ImageURL = ImageURL;
    this.featured = featured;
}

//getters
public String getUsername() { return username; }
public String getImageURL() { return ImageURL; }
public Boolean getFeatured(){return featured; }

}

2 个答案:

答案 0 :(得分:1)

首先,更改

""

GridAdapter adapter = new GridAdapter(this, contactList);

然后,contactList对象应该是 CustomContact [] ,而不是 ArrayList <>

祝你好运!

答案 1 :(得分:0)

好的,我能够弄清楚。我将List<CustomContact> contactList传递到了GridAdapter的参数中。

这是最终代码:

public class GridAdapter extends BaseAdapter {

private Context context;
private List<CustomContact> contactList;


public GridAdapter(Context context,List<CustomContact> contactList) {
    this.context = context;
    this.contactList=contactList;

}

// 2
@Override
public int getCount() {
    return contactList.size();
}

// 3
@Override
public long getItemId(int position) {
    return position;
}

// 4
@Override
public Object getItem(int position) {
    return position;
}

// 5
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //get the user we are displaying
    CustomContact customcontact = contactList.get(position);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.mylist, null);

    ImageView image = (ImageView) view.findViewById(R.id.friend_icon2);
    TextView friend_name = (TextView) view.findViewById(R.id.Itemname);


    //get & set username
    String completeUsername = customcontact.getUsername();
    friend_name.setText(completeUsername);



    //set image
    Picasso.get().load(customcontact.getImageURL()).placeholder(R.drawable.placeholder_image).resize(86, 86)
            .centerCrop().into(image);


    return view;
}

}