使用自定义适配器时清空RecyclerView

时间:2015-10-27 00:56:21

标签: java android android-recyclerview

我正在尝试使用来自云(RecyclerView)的数据填充我的parse.com。但是,最终结果是空RecyclerView。虽然我能够从云中检索数据,但我在屏幕上看不到任何数据。

以下是主要的活动代码:

public class BigBoard extends ActionBarActivity {

private List<Person> persons;
private RecyclerView rv;
RVAdapter adapter;
String a,b;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Parse.initialize(this, "app-id", "client-key");
    setContentView(R.layout.activity_big_board);
    adapter = new RVAdapter(persons);


    rv=(RecyclerView)findViewById(R.id.rv);

    LinearLayoutManager llm = new LinearLayoutManager(this);
    rv.setLayoutManager(llm);
    rv.setHasFixedSize(true);

    initializeData();
    initializeAdapter();

}

private void initializeData(){
    persons = new ArrayList<>();


    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Credentials");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> credentialList, ParseException e) {
            if (e == null) {
                for(int i=0;i<credentialList.size();i++)
                {
                    a=credentialList.get(i).getString("Name");
                    b=credentialList.get(i).getString("SurName");
                    persons.add(new Person(a,b));



                    Log.d("OUT", "So the Val::------> " +a +b);
                    adapter.notifyDataSetChanged();

                }
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });
    //persons.add(new Person("Emma Wilson", "23 years old"));
    //persons.add(new Person("Lavery Maiss", "25 years old"));
    //persons.add(new Person("Lillie Watts", "35 years old"));

}

private void initializeAdapter(){

    rv.setAdapter(adapter);
}

我能够记录从云端收到的值,如果我使用一些预定义的值,代码也能正常工作。正如您可以看到注释行:

   //persons.add(new Person("Emma Wilson", "23 years old"));
   //persons.add(new Person("Lavery Maiss", "25 years old"));
   //persons.add(new Person("Lillie Watts", "35 years old"));

以下是Adapter类的代码:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {

public static class PersonViewHolder extends RecyclerView.ViewHolder {

    CardView cv;
    TextView personName;
    TextView personAge;
    //ImageView personPhoto;

    PersonViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.cv);
        personName = (TextView)itemView.findViewById(R.id.person_name);
        personAge = (TextView)itemView.findViewById(R.id.person_age);
        //personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
    }
}

List<Person> persons;

RVAdapter(List<Person> persons){
    this.persons = persons;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
    PersonViewHolder pvh = new PersonViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
    personViewHolder.personName.setText(persons.get(i).name);
    personViewHolder.personAge.setText(persons.get(i).surname);
   // personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}

@Override
public int getItemCount()
{
    if(persons!=null)
    {
        return persons.size();
    }
    else
    {
        return 0;
    }
}
}

Person类:

    class Person
{
    String name;
    String surname;


    Person(String name, String sur)
    {
        this.name = name;
        this.surname = sur;

    }
}

虽然我使用了notifyDataSetChanged()方法,但为什么我无法获得任何结果,如果我考虑使用其他线程或AsyncTask(),寻找答案,谢谢!

1 个答案:

答案 0 :(得分:0)

问题是,您实际上并未将数据设置到适配器中。首先在适配器中添加以下方法:

public void setPersons(List<Person> persons){
        this.persons = persons;
        notifyDataSetChanged();
    }

然后在调用类中的initializeData()方法之后调用adapter.setPersons(persons)。您不必再次拨打以下电话:

`private void initializeAdapter(){

    rv.setAdapter(adapter);
}`

因此,每当从服务器获取新数据时,基本上只需调用adapter.setPersons(persons)