Android 实现自定义GridView
我有一个Android 实现自定义GridView,android,firebase,android-studio,firebase-realtime-database,Android,Firebase,Android Studio,Firebase Realtime Database,我有一个GridView适配器,还有一个我正试图插入其中的自定义对象 它不允许我将ArrayList放入GridAdapter中: 冒犯的界线: GridAdapter adapter = new GridAdapter(this, contactList); 给出的错误是: GridAdapter (android.content.Context, com.example.user.myapp.CustomContact[])in GridAdapter cannot be applie
GridView
适配器,还有一个我正试图插入其中的自定义对象
它不允许我将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>)
编辑#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; }
}
首先,更改此:
GridAdapter adapter = new GridAdapter(this, contactList);
GridAdapter=新的GridAdapter(此,联系人列表)代码>
至
GridAdapter=新的GridAdapter(getApplicationContext(),contactList)代码>
然后,contactList对象应该是CustomContact[]而不是ArrayList
祝你好运 好吧,我能弄明白。我将我的列表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;
}
}
公共类GridAdapter扩展BaseAdapter{
私人语境;
私人名单联系人名单;
公共GridAdapter(上下文上下文、列表联系人列表){
this.context=上下文;
this.contactList=联系人列表;
}
// 2
@凌驾
public int getCount(){
返回contactList.size();
}
// 3
@凌驾
公共长getItemId(int位置){
返回位置;
}
// 4
@凌驾
公共对象getItem(int位置){
返回位置;
}
// 5
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//获取我们正在显示的用户
CustomContact CustomContact=contactList.get(位置);
LayoutInflater充气器=(LayoutInflater)context.getSystemService(Activity.LAYOUT\u充气器\u SERVICE);
视图=充气机。充气(R.layout.mylist,null);
ImageView image=(ImageView)view.findViewById(R.id.friend\u icon2);
TextView-friend_-name=(TextView)view.findViewById(R.id.Itemname);
//获取并设置用户名
字符串completeesername=customcontact.getUsername();
friend_name.setText(completeesername);
//设置图像
Picasso.get().load(customcontact.getImageURL()).placeholder(R.drawable.placeholder_image)。调整大小(86,86)
.centerCrop()。插入(图像);
返回视图;
}
}
像这样吗<代码>字符串[]联系人列表=新字符串[]代码>?我仍然会遇到类似的错误:GridAdapter(Context,com.example.user.myapp.CustomContact[])中的GridAdapter无法应用于(ContactsActivity,java.lang.String[])
,而且.add()和.get()方法也不再有效。啊哈!现在我明白了!在GridAdapter
类中,您使用的是publicgridadapter(上下文上下文,CustomContact[]CustomContact){…}
。因此,定义新的GridAdapter
时,插入的数组必须是CustomContact[]
(不是String[]
也不是ArrayList
)。另外,上下文
必须是getApplicationContext()
而不是Activity.this。我将编辑我的答案!