Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

android - Adding Items in RecycleView Dynamically using a button

I have a button(save contact) to save contacts ,the button when pressed get name and email from edit text and should dynamically add 1 list item in recycle view

The save button is in fragment

This is the name which i am extracting

company_name=(EditText)view.findViewById(R.id.edittext_companyname_createMeeting);

When i click on lead it should add in the existing recycleview

 leads.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

     newValueAdapter.newAddeddata(company_name.getText().toString());*/
            }
        });

The recycle view already have a arraylist,how do i add new value to the arraylist and again call oncreate

I tried to extract company name and send it to the adapter of that recycler view but it dint work

Adapter of RecyclerView

public class NewleadsAdapter extends RecyclerView.Adapter<NewleadsAdapter.MyViewHolder> {

    Context context;
    LayoutInflater inflater;
    int positionbundle;
    ArrayList<NewleadsPOJO> datalist;
    public NewleadsAdapter(Context context, ArrayList<NewleadsPOJO> datalist,int positionbundle){

        this.context=context;
        this.datalist=datalist;
        this.positionbundle=positionbundle;

    }
   /* public NewleadsAdapter(){}*/

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(context).inflate(R.layout.custom_new_leads,parent,false);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

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


        holder.leads_company.setText(datalist.get(position).getLeads_company());
        holder.leads_date.setText(datalist.get(position).getLeads_date());
        holder.leads_time.setText(datalist.get(position).getLeads_time());
        if (positionbundle == 1){
          /*  holder.icon1.setVisibility(View.GONE);*/
            holder.icon1.setImageResource(R.drawable.taskcompletednewblue);
    }else{}

        Log.e("Create_meetingdate_ArrayList:", datalist.get(0).getLeads_company());   /* Arrays.deepToString(data.toArray())*/
        System.out.println(datalist.get(0).getLeads_company());
    }

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


    public class MyViewHolder extends RecyclerView.ViewHolder{

       TextView leads_company,leads_date,leads_time;
        ImageView icon1;
        public MyViewHolder(View itemView) {
            super(itemView);

            leads_company=(TextView)itemView.findViewById(R.id.leads_company);
            leads_date=(TextView)itemView.findViewById(R.id.leads_date);
            leads_time=(TextView)itemView.findViewById(R.id.leads_time);
             icon1=(ImageView)itemView.findViewById(R.id.leads_info);
        }
    }


    /*public void newAddeddata(String company_name){
        NewleadsPOJO newValue=new NewleadsPOJO();
        newValue.setLeads_company(company_name);
        datalist.add(datalist.size(),newValue);

    }*/
}

Createmeetingfrag.java

public class CreateMeetingFrag extends Fragment {

TextView leads,cold,warm,hot,closed;
    EditText company_name,email,date,time;
    public CreateMeetingFrag() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_create_meeting, container, false);


        company_name=(EditText)view.findViewById(R.id.edittext_companyname_createMeeting);

        leads=(TextView)view.findViewById(R.id.meeting_leads);
        cold=(TextView)view.findViewById(R.id.meeting_cold);
        warm=(TextView)view.findViewById(R.id.meeting_warm);
        hot=(TextView)view.findViewById(R.id.meeting_hot);
        closed=(TextView)view.findViewById(R.id.meeting_closed);

        leads.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //getSavedValues();
               /* NewleadsAdapter newValueAdapter=new NewleadsAdapter();
                newValueAdapter.newAddeddata(company_name.getText().toString());*/

                NewleadsAdapter newValue=new NewleadsAdapter();

                newValue.newAddeddata(company_name.getText().toString());


                 setDefaultValues();
                leads.setBackgroundColor(getResources().getColor(R.color.blue));
                Toast.makeText(getActivity(),"leads clicked and saved",Toast.LENGTH_SHORT).show();
                leads.setTextColor(getResources().getColor(R.color.white));
            }
        });



        cold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDefaultValues();
                cold.setBackgroundColor(getResources().getColor(R.color.blue));
                Toast.makeText(getActivity(),"leads clicked",Toast.LENGTH_SHORT).show();
                cold.setTextColor(getResources().getColor(R.color.white));
            }
        });


        warm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDefaultValues();
                warm.setBackgroundColor(getResources().getColor(R.color.blue));
                Toast.makeText(getActivity(),"leads clicked",Toast.LENGTH_SHORT).show();
                warm.setTextColor(getResources().getColor(R.color.white));
            }
        });


        hot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDefaultValues();
                hot.setBackgroundColor(getResources().getColor(R.color.blue));
                Toast.makeText(getActivity(),"leads clicked",Toast.LENGTH_SHORT).show();
                hot.setTextColor(getResources().getColor(R.color.white));
            }
        });


        closed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDefaultValues();
                closed.setBackgroundColor(getResources().getColor(R.color.blue));
                Toast.makeText(getActivity(),"leads clicked",Toast.LENGTH_SHORT).show();
                closed.setTextColor(getResources().getColor(R.color.white));
            }
        });




        return view;

    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setDefaultValues(){

        leads.setBackgroundColor(getResources().getColor(R.color.white));
        cold.setBackgroundColor(getResources().getColor(R.color.white));
        warm.setBackgroundColor(getResources().getColor(R.color.white));
        hot.setBackgroundColor(getResources().getColor(R.color.white));
        closed.setBackgroundColor(getResources().getColor(R.color.white));
        closed.setTextColor(getResources().getColor(R.color.black_semi_transparent));
        hot.setTextColor(getResources().getColor(R.color.black_semi_transparent));
        warm.setTextColor(getResources().getColor(R.color.black_semi_transparent));
        cold.setTextColor(getResources().getColor(R.color.black_semi_transparent));
        leads.setTextColor(getResources().getColor(R.color.black_semi_transparent));
        leads.setBackground(getResources().getDrawable(R.drawable.bordder_button));
        closed.setBackground(getResources().getDrawable(R.drawable.bordder_button));
        hot.setBackground(getResources().getDrawable(R.drawable.bordder_button));
        warm.setBackground(getResources().getDrawable(R.drawable.bordder_button));
        cold.setBackground(getResources().getDrawable(R.drawable.bordder_button));
    }




}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add this method to your adapter and call on button click.

public void newAddeddata(String company_name){
        NewleadsPOJO newValue=new NewleadsPOJO();
        newValue.setLeads_company(company_name);
        datalist.add(newValue);
        notifyDataSetChanged();
    }

Add following method to NewLeadFrag

public NewleadsAdapter getAdapter(){
   return adapter;
}

now in Createmeetingfrag

leads.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewLeadFrag fragment = getFragmentManager().findFragmentByTag("NewLeadFrag_TAG"); //set tag of fragment when you add with fragment manager. and if you are using support library use getSupportFragmentManager()
               if(fragment!= null){
                    fragment.getAdapter().newAddeddata(company_name.getText().toString());
                }

            }
        });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...