RecylerView中的Edittext开始丢失数据

时间:2016-01-13 12:56:46

标签: android android-recyclerview recycler-adapter

我在Android应用程序中工作,其中有回收站视图,每行都是edittext,因为我点击浮动按钮添加了新的edittext,用户可以输入电子邮件,我的问题是,在说出第9个位置后,先前输入电子邮件值替换为某个位置,我在下面调用notifydatasetchanged()方法代码... 如果有人知道请求分享的方式..

1)挑战Invite_other.java

/**
 * Add custom workout detail
 */
public class ChallengeInviteOtherActivity extends GlobalAppCompactActivity implements ResponseListener {

    private Gson gson;
    private List<ChallengeParticipant> challengeParticipants;
    private RecyclerView mRecyclerView;
    private ChallengeInviteOtherAdapter adapter;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.challenge_invite_other);
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        initlization();
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floating_add_custom_challenge);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                challengeParticipants.add(new ChallengeParticipant());
                adapter.addAll(challengeParticipants);
            }
        });
        setTitle(getString(R.string.nav_invite_participant));
        //progressDialog = new ProgressDialog(this, R.style.CustomProgressDialog);
    }


    private void initlization() {
        gson = CommonUtil.getGson();

        challengeParticipants = new ArrayList<ChallengeParticipant>();
        mRecyclerView = (RecyclerView) findViewById(R.id.challenge_list_view);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


        challengeParticipants.add(new ChallengeParticipant());
        adapter = new ChallengeInviteOtherAdapter(this);
        adapter.addAll(challengeParticipants);
        mRecyclerView.setAdapter(adapter);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }else if (item.getItemId() == R.id.action_bar_trace_friend) {
            List<ChallengeParticipant> challengeParticipantList = new ArrayList<ChallengeParticipant>();
            for (ChallengeParticipant participant : challengeParticipants) {
                if (CommonUtil.isNotNull(participant.getEmailAddress()) || CommonUtil.isNotNull(participant.getContactNo())) {
                    participant.setChallengeId(CommonUtil.CHALLENGE.getId());
                    challengeParticipantList.add(participant);
                }
            }
            if(challengeParticipantList.size()==0){
                AlertMsg.showToast(this, getString(R.string.at_least_fill_one_contact));
            }else{
                Type listType = new TypeToken<ArrayList<ChallengeParticipant>>() {
                }.getType();
                String json = CommonUtil.getGson().toJson(challengeParticipantList, listType);
                VolleyRequest volleyRequest = VolleyRequest.getInstance();
                volleyRequest.sendRequest(VolleyRequest.INVITE_PARTICIPANTS, json, CommonUtil.getObject(this, this));
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.clear();
        getMenuInflater().inflate(R.menu.challenge_invite_participant_memu, menu);
        getMenuInflater().inflate(R.menu.common, menu);
        return true;
    }



    @Override
    public void onResponse(Object... result) {
        AlertMsg.showToast(this, getString(R.string.invite_successfully));
        finish();
    }

    //private ProgressDialog progressDialog;
}

2)适配器类

 private List<ChallengeParticipant> itemDetailsrrayList;
    private LayoutInflater layoutInflater;
    private Context mContext;

    public ChallengeInviteOtherAdapter(Context context) {
        mContext = context;
        layoutInflater = LayoutInflater.from(context);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private EditText email;
        public MyViewHolder(View view) {
            super(view);
            email = (EditText) view.findViewById(R.id.email);

        }
    }


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

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final ChallengeParticipant c = itemDetailsrrayList.get(position);
        holder.email.setText(c.getEmailAddress());
        //holder.phone.setText(c.getContactNo());
        //RecyclerView recyclerView = (RecyclerView) ((Activity) mContext).findViewById(R.id.price_listview);
        //recyclerView.getLayoutParams().height = 150*itemDetailsrrayList.size();
        holder.email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(CommonUtil.isEmail(holder.email.getText().toString())){
                    c.setEmailAddress(holder.email.getText().toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });
        /*holder.email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {

                }
            }
        });*/
       /* holder.phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {
                    if(CommonUtil.isPhone(holder.phone.getText().toString())){
                        c.setContactNo(holder.phone.getText().toString());
                    }
                }
            }
        });*/
    }


    public void addAll(List<ChallengeParticipant> list) {

        itemDetailsrrayList = list;
       notifyItemInserted(itemDetailsrrayList.size());
    }



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


}

3) xml活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dim_background"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <include layout="@layout/toolbar" />

    <!--<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/or"/>-->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:elevation="@dimen/general_elevation"
        android:layout_margin="10dp"
        android:background="@color/cardview_light_background">

        <include layout="@layout/textview_no_record_found" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/challenge_list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/floating_add_custom_challenge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:elevation="12dp"
            android:src="@drawable/ic_add_white_24dp"
            app:backgroundTint="@color/colorAccent"
            app:borderWidth="0dp"
            app:fabSize="normal"/>
    </FrameLayout>



    <include
        layout="@layout/ad_system"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

将新数据添加到设置适配器的阵列时比通知适配器在调用adapter.addAll(challengeParticipants);后添加以下行

adapter.notifyDataSetChanged();
adapter.notifyItemInserted(challengeParticipants.size());

答案 1 :(得分:0)

试试这个

public void addAll(List<ChallengeParticipant> list) {
    itemDetailsrrayList = list;
    notifyItemRangeChanged(0,challengeParticipants.size()-1);
}