单击“联系人”时,将项目添加到RecyclerView

时间:2017-12-01 04:14:38

标签: android

当我选择联系人联系方式

时,我想将项目添加到RecyclerView

它可以在文本视图中使用,但我可以找到如何在回收器中使用

我从联系簿中获得了两个类似于contactname和numberphne的字符串,我需要添加它以在recyclelerview中查看和显示

这是我的RecyclerView适配器代码

public class SettingCallAdapter extends RecyclerView.Adapter<SettingCallAdapter.ViewHolder>{


private Context context;
private List<Hospital> data;
private int lastSelectedPosition = 0;

public SettingCallAdapter( Context context , List<Hospital> data){
    this.context = context;
    this.data = data;

}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_setting_message_radio , parent , false);

    ViewHolder viewHolder = new ViewHolder(view);

    return viewHolder;
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    Hospital hospital = data.get(position);

    holder.name.setText(hospital.getHos_name());
    holder.num.setText(hospital.getHos_tel());
    holder.selectionState.setChecked(lastSelectedPosition == position);


}

@Override
public int getItemCount() {
    return data.size() ;
}

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView name , num;
    public RadioButton selectionState;



    public ViewHolder(View itemView) {
            super(itemView);


        name = itemView.findViewById(R.id.set_name);
        num = itemView.findViewById(R.id.set_num);
        selectionState = itemView.findViewById(R.id.select_state);
        selectionState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lastSelectedPosition = getAdapterPosition();
                notifyDataSetChanged();
            }
        });
    }
}

这是我的MainActivity

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_setting);
    TextView title = findViewById(R.id.toolbar_title);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_back));
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onBackPressed();
        }
    });

    title.setText(toolbar.getTitle());
    title.setText("การแจ้งเหตุฉุกเฉิน");

    mDatabase = FirebaseDatabase.getInstance();
    mRef = mDatabase.getReference().child("setting").child("hospital");

    set_button = findViewById(R.id.setting_edit_button);
    set_button.setOnClickListener(this);
    save_button = findViewById(R.id.setting_save_button);
    save_button.setOnClickListener(this);

    show_num = findViewById(R.id.show_num_call);
    add_num = findViewById(R.id.add_num_call);
    add_contact_message = findViewById(R.id.add_contact_message);

    message = findViewById(R.id.set_message);
    message.setEnabled(false);

    add_num_call = findViewById(R.id.recycler_add_num_call);

    show_num_message_list = findViewById(R.id.recycler_show_num_message);
    add_num_message_list = findViewById(R.id.recycler_add_num_message);

    select_contact = findViewById(R.id.add_contact_call);
    select_contact.setOnClickListener(this);

    //Check Keyboard
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
                Hospital mHospital = dataSnapshot1.getValue(Hospital.class);

                list_hospital.add(mHospital);
            }

            mAdapter = new SettingCallAdapter(SettingActivity.this , list_hospital);
            mAdapter.notifyDataSetChanged();
            add_num_call.setAdapter(mAdapter);
            add_num_call.setHasFixedSize(true);
            add_num_call.setLayoutManager(new LinearLayoutManager(SettingActivity.this));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
     @Override
public void onClick(View view) {
    switch (view.getId())
    {
        case R.id.setting_edit_button:
            set_button.setVisibility(View.GONE);
            save_button.setVisibility(View.VISIBLE);
            show_num.setVisibility(View.GONE);
            add_num.setVisibility(View.VISIBLE);
            add_contact_message.setVisibility(View.VISIBLE);
            show_num_message_list.setVisibility(View.GONE);
            message.setEnabled(true);

            addNumSOS();


            break;

        case R.id.setting_save_button:
            save_button.setVisibility(View.GONE);
            set_button.setVisibility(View.VISIBLE);
            show_num.setVisibility(View.VISIBLE);
            add_num.setVisibility(View.GONE);
            add_contact_message.setVisibility(View.GONE);
            show_num_message_list.setVisibility(View.VISIBLE);
            message.setEnabled(false);

            addMessageSOS();

            break;

        case R.id.add_contact_call:
            Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(contactIntent , RESULT_PICK_CONTACT);
    }
}

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_PICK_CONTACT && resultCode == RESULT_OK) {
        Uri contactUri = data.getData();
        Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
        cursor.moveToFirst();

        int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

        name_contact = cursor.getString(nameIndex);
        num_contact = cursor.getString(phoneIndex);
    }
                Hospital hospital= new  Hospital();
                hospital.setHos_name(name_contact);
                hospital.setHos_tel(num_contact);
                list_hospital.add(hospital);
                mAdapter.notifyItemInserted(list_hospital.size()-1);
        }

编辑:我如何使用差异在我的代码中查看它意味着我使用 view_1 提供表单数据,当我添加数据时使用 view_2

此图片显示差异视图,当我添加view_2有一个删除按钮,它形成ios手机

2 个答案:

答案 0 :(得分:1)

我发现你的代码有些问题。由于您添加了ValueEventListener onDataChange,因此只要您的mRef出现问题,就会触发。那没问题。但问题是你已经在该事件上设置了适配器和布局管理器,并且每次当recovery mRef发生某些事情时,recyclerview的适配器都会发生变化。

以下是如何正确完成的。 将以下代码放在onCreate行下面的add_num_call = findViewById(R.id.recycler_add_num_call);方法中。

    add_num_call.setLayoutManager(new LinearLayoutManager(SettingActivity.this));
    mAdapter = new SettingCallAdapter(SettingActivity.this , list_hospital);
            mAdapter.notifyDataSetChanged();
            add_num_call.setHasFixedSize(true);

然后在onDataChange内,您可以将项目添加到适配器并通知更改,如下所示。

 for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
                Hospital mHospital = dataSnapshot1.getValue(Hospital.class);
                list_hospital.add(mHospital);
 }
mAdapter.notifyDataSetChanged();

关于您的问题,您可以在onActivityResult上执行此操作。

Hospital hospital= new  Hospital();
hospital.setHos_name(name_contact);
hospital.setHos_tel(num_contact);
list_hospital.add(hospital);
mAdapter.notifyDataSetChanged();// or better to use ---> mAdapter.notifyItemInserted(list_hospita.size()-1);

享受!

答案 1 :(得分:0)

检查数字和名称是否为空