我试图保存我的arraylist"人员"在onSaveInstanceState中,然后在我切换方向时在create()中恢复它。通过日志,我看到人们恢复了,但适配器没有显示人员的屏幕项目! 不幸的是,我不明白如何解决它。有人可以帮忙吗?
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Person> persons;
private RecyclerView rv;
final String LOG_TAG = "myLogs";
RVAdapter adapter;
Person person;
ProductCardList mProductCard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
rv = (RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
persons = new ArrayList<>();
adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
if ((savedInstanceState != null) && (savedInstanceState.getSerializable("card") != null)) {
persons = (List<Person>) savedInstanceState.getSerializable("card");
adapter.notifyDataSetChanged();
Log.d(LOG_TAG, "restore card with persons" + persons);
}
adapter.setOnItemClickListener(new RVAdapter.MyClickListener() {
@Override
public void onItemClick(int position, View v) {
Toast.makeText(MainActivity.this, "push on item" + position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, ProductCard.class);
startActivity(intent);
}
});
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
person= new Person("Emma Wilson", "23 years old", R.drawable.im_beach);
persons.add(person);
Log.d(LOG_TAG, "push add" + persons);
adapter.notifyDataSetChanged();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("card", (Serializable) persons);
Log.d(LOG_TAG, "save cards" + persons);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的适配器
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Dmitry on 01.11.2015.
*/
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
List<Person> persons;
private static String LOG_TAG = "MyRecyclerViewAdapter";
private static MyClickListener myClickListener;
RVAdapter(List<Person> persons){
this.persons = persons;
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card, 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).age);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return persons.size();
}
public static class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
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);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
更新部分
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
rv = (RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
persons = new ArrayList<>();
adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
if ((savedInstanceState != null) && (savedInstanceState.getSerializable("card") != null)) {
persons = (List<Person>) savedInstanceState.getSerializable("card");
adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
Log.d(LOG_TAG, "restore card with persons" + persons);
}
答案 0 :(得分:3)
您没有将反序列化人员列表传递给适配器。 您正在替换人员实例,而不是更新列表本身。
移动:
adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
阅读savedInstanceState。
或者做这样的事情:
persons = new ArrayList<>();
adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
if (savedInstanceState != null && savedInstanceState.getSerializable("card") != null) {
persons.clear();
persons.addAll((List<Person>) savedInstanceState.getSerializable("card"));
adapter.notifyDataSetChanged();
Log.d(LOG_TAG, "restore card with persons" + persons);
}