请帮我这个,我创建了一个简单的recyclerview
,但我没有在屏幕上输出,它是空白的
这是我的适配器类,我一直试图调试这一整天没有成功,而且执行时没有错误
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ViewHolder>
{
private Activity context;
private LayoutInflater mLayoutInflater;
private List<Contact> contacts;
public ContactAdapter(Activity context)
{
this.context=context;
mLayoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contacts = new ArrayList<>();
}
@Override
public ContactAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate( R.layout.contact,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ContactAdapter.ViewHolder holder, int position) {
Contact contact = contacts.get(position);
holder.name.setText(contact.getName());
holder.email.setText(contact.getEmail());
}
@Override
public int getItemCount() {
return contacts.size();
}
static class ViewHolder extends RecyclerView.ViewHolder
{
TextView name,email;
ViewHolder(View itemView)
{
super(itemView);
name= (TextView)itemView.findViewById(R.id.name);
email=(TextView)itemView.findViewById(R.id.email);
}
}
}
这是我的主要活动课
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ContactAdapter adapter;
List<Contact> contacts = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter=new ContactAdapter(this);
Contact c1=new Contact("potato","xyz@gmail.com");
adapter.setHasStableIds(true);
recyclerView.setAdapter(adapter);
contacts.add(c1);
adapter.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
你是适配器
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ViewHolder>
{
private Activity context;
private LayoutInflater mLayoutInflater;
private List<Contact> contacts;
public ContactAdapter(Activity context,List<Contact> _list)
{
this.context=context;
this.contacts=_list;
mLayoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contacts = new ArrayList<>();
}
public void setDataChange(List<Contact> asList) {
this.contacts = asList;
//now, tell the adapter about the update
notifyDataSetChanged();
}
@Override
public ContactAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate( R.layout.contact,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ContactAdapter.ViewHolder holder, int position) {
Contact contact = contacts.get(position);
holder.name.setText(contact.getName());
holder.email.setText(contact.getEmail());
}
@Override
public int getItemCount() {
return contacts.size();
}
static class ViewHolder extends RecyclerView.ViewHolder
{
TextView name,email;
ViewHolder(View itemView)
{
super(itemView);
name= (TextView)itemView.findViewById(R.id.name);
email=(TextView)itemView.findViewById(R.id.email);
}
}
}
您的Activity
public class MainActivity extends AppCompatActivity
{
RecyclerView recyclerView;
ContactAdapter adapter;
List<Contact> contacts = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Contact c1=new Contact("potato","xyz@gmail.com");
contacts.add(c1);
adapter=new ContactAdapter(this,contacts);
recyclerView.setAdapter(adapter);
}
}
答案 1 :(得分:0)
在Adapter
内,您必须创建该添加方法:
public void add(Contact c1) {
chatModelList.add(c1);
notifyItemInserted(contacts.size() - 1);
}
另一种方式:
List<Contact> result = getResult();
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new ContactAdapter(this, result);
recyclerView.setAdapter(adapter);
private List<Contact> getResult() {
List<Contact> contactList = new ArrayList<>();
contactList.add(new Contact("A","B"));
return contactList;
}
现在在适配器类中:
private Context context;
private List<Contact> contact = new ArrayList<>();
public ContactAdapter(Context context, List<Contact> contact) {
this.context = context;
this.contact = contact;
}
答案 2 :(得分:0)
您的主管中有联系人列表,但您没有将这些联系人添加到适配器中。 (在您的情况下,适配器列表始终为空)。
在适配器中创建一个方法,将联系人设置为适配器。
void setContacts(List<Contact> contacts) {
this.contacts = contacts;
notifiyDataSetChanged();
}
void addContacts(Contact contact) {
this.contacts.add(contact);
notifyItemInserted(getItemCount()-1);
}
然后在主调用setContacts中,如果您想一次添加多个联系人,或使用addContacts一次添加一个联系人。