【发布时间】:2021-02-17 05:57:08
【问题描述】:
我正在使用SharedPreferences 将联系人存储在RecyclerView 中,以便我可以处理存储的联系人。在代码中,没有运行时错误,但仍然无法检索数据以显示在RecyclerView 中。
我使用了multicontactpicker 依赖项,我还尝试为首选项创建一个单独的类,但它不起作用
public class emergencyContact extends AppCompatActivity {
private static final int CONTACT_PICKER_REQUEST = 202;
private Button addContactsBtn;
private ArrayList<ContactResult> list;
public static final String SHARE_PREFS = "sheredPrefs";
RecyclerView recyclerView;
ContactAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emergency_contact);
loadData();
addContactsBtn = findViewById(R.id.add_contacts_btn);
addContactsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MultiContactPicker.Builder(emergencyContact.this) //Activity/fragment context
.theme(R.style.MyCustomPickerTheme) //Optional - default: MultiContactPicker.Azure
.hideScrollbar(false) //Optional - default: false
.showTrack(true) //Optional - default: true
.searchIconColor(Color.WHITE) //Option - default: White
.setChoiceMode(MultiContactPicker.CHOICE_MODE_MULTIPLE) //Optional - default: CHOICE_MODE_MULTIPLE
.handleColor(ContextCompat.getColor(emergencyContact.this, R.color.azureColorPrimary)) //Optional - default: Azure Blue
.bubbleColor(ContextCompat.getColor(emergencyContact.this, R.color.azureColorPrimary)) //Optional - default: Azure Blue
.bubbleTextColor(Color.WHITE) //Optional - default: White
.setTitleText("Select Contacts") //Optional - default: Select Contacts
.setLoadingType(MultiContactPicker.LOAD_ASYNC) //Optional - default LOAD_ASYNC (wait till all loaded vs stream results)
.limitToColumn(LimitColumn.NONE) //Optional - default NONE (Include phone + email, limiting to one can improve loading time)
.setActivityAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
android.R.anim.fade_in,
android.R.anim.fade_out) //Optional - default: No animation overrides
.showPickerForResult(CONTACT_PICKER_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CONTACT_PICKER_REQUEST){
if(resultCode == RESULT_OK) {
list = MultiContactPicker.obtainResult(data);
buildRecycleView();
saveData();
} else if(resultCode == RESULT_CANCELED){
System.out.println("User closed the picker without selecting items.");
}
}
}
private void saveData() {
SharedPreferences pref = getSharedPreferences(SHARE_PREFS,MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
Gson gson = new Gson();
String jsonString = gson.toJson(list);
editor.putString("List Key",jsonString);
Log.e("MyTag","size="+ list.size());
editor.apply();
}
private void loadData(){
if(list == null){
list = new ArrayList<>();
Log.e("loadData","Size="+list.size());
}
SharedPreferences pref = getSharedPreferences(SHARE_PREFS,MODE_PRIVATE);
Gson gson = new Gson();
String jsonString = pref.getString("List Key",null);
Type type = new TypeToken<ArrayList<ContactResult>>(){}.getType();
list = gson.fromJson(jsonString,type);
Log.e("MTag","Size="+list.size());
}
private void buildRecycleView(){
recyclerView = findViewById(R.id.contact_rv);
LinearLayoutManager LayoutManager= new LinearLayoutManager(this);
ContactAdapter adapter = new ContactAdapter(list,this);
recyclerView.setLayoutManager(LayoutManager);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
我也想对选定的联系人应用功能。
【问题讨论】:
标签: java android database android-fragments