Android添加联系人后数据丢失求助:重复添加时Activity刷新丢失历史数据
解决添加联系人后数据丢失的问题
这个问题我之前也碰到过,核心原因是你每次添加新联系人时,都重新创建了一个RentalAdapter实例并替换给RecyclerView——其实你的contactPojolist全局变量里的数据并没有丢,但频繁替换适配器会让RecyclerView重置视图状态,看起来就像之前的联系人消失了。
具体修复步骤
- 把适配器设为类成员变量,只在初始化时创建一次
- 添加新联系人后,仅通知适配器数据更新,而非重新创建适配器
- 记得关闭Cursor,避免资源泄漏
修改后的完整代码示例
public class EmergencyContact extends AppCompatActivity { private static final int REQUEST_CODE = 1; private ArrayList<ContactPojo> contactPojolist = new ArrayList<>(); private RecyclerView emerg_recycler; private RecyclerView.LayoutManager mLayoutManager; // 新增:将适配器设为类成员变量 private RentalAdapter emergencyContactAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_emergency_contact); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); emerg_recycler = (RecyclerView) findViewById(R.id.emerg_contact_recycler); mLayoutManager = new LinearLayoutManager(this); emerg_recycler.setLayoutManager(mLayoutManager); // 初始化适配器并绑定到RecyclerView(只做一次) emergencyContactAdapter = new RentalAdapter(EmergencyContact.this, contactPojolist); emerg_recycler.setAdapter(emergencyContactAdapter); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show(); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Uri uri = Uri.parse("content://contacts"); Intent intent = new Intent(Intent.ACTION_PICK, uri); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); startActivityForResult(intent, REQUEST_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); // 别忘了调用父类方法 if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { Uri uri = intent.getData(); String[] projection = { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }; Cursor cursor = getContentResolver().query(uri, projection, null, null, null); if (cursor != null) { try { cursor.moveToFirst(); int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String number = cursor.getString(numberColumnIndex); int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); String name = cursor.getString(nameColumnIndex); ContactPojo contactPojo = new ContactPojo(); contactPojo.setName(name); contactPojo.setNumber(number); contactPojolist.add(contactPojo); // 通知适配器新增了一条数据(高效局部更新) emergencyContactAdapter.notifyItemInserted(contactPojolist.size() - 1); // 如果你的适配器不支持局部更新,也可以用全局刷新: // emergencyContactAdapter.notifyDataSetChanged(); } finally { // 必须关闭Cursor,释放系统资源 cursor.close(); } } } } }
修复逻辑说明
- 复用同一个适配器实例,RecyclerView会保留之前的视图状态,不会出现重置清空的情况
notifyItemInserted()只会刷新新增的列表项,比重新设置适配器更高效- 新增Cursor的关闭操作,避免了潜在的内存泄漏问题
内容的提问来源于stack exchange,提问作者Hemant Thakare
相关产品推荐
相关产品推荐

