【问题标题】:Implementing a custom GridView实现自定义 GridView
【发布时间】:2018-11-04 22:10:03
【问题描述】:

我有一个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; }

}

【问题讨论】:

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


    【解决方案1】:

    首先,改变这个

    GridAdapter adapter = new GridAdapter(this, contactList);

    GridAdapter adapter = new GridAdapter(getApplicationContext(), contactList);

    那么,contactList对象应该是CustomContact[]而不是ArrayList

    祝你好运!

    【讨论】:

    • 喜欢这个? String[] contactList = new String[]; ?我仍然收到类似的错误:GridAdapter (Context, com.example.user.myapp.CustomContact[]) in GridAdapter cannot be applied to (ContactsActivity, java.lang.String[]) 以及 .add() 和 .get() 方法不再起作用。
    • 啊哈!现在我懂了!在您的GridAdapter 课程中,您使用的是public GridAdapter(Context context, CustomContact[] customContact) { ... }。所以当你定义一个新的GridAdapter时,你插入的数组必须是CustomContact[](不是String []也不是ArrayList&lt;&gt;)。此外,Context 必须是 getApplicationContext() 而不是 Activity.this。我将编辑我的答案!
    【解决方案2】:

    好吧,我能弄明白。我将List&lt;CustomContact&gt; 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;
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2014-10-14
      • 2011-06-04
      • 2010-10-29
      • 2011-01-10
      • 1970-01-01
      相关资源
      最近更新 更多