我有一个回收站视图,它只是行的列表。每行包含两个编辑文本。我有一个按钮,当单击该按钮时,应在最后一行的下面添加新行,并仍然保留所有已填写的EditText。由于某种原因,我在单击按钮时会得到一些非常随机的行为,即这些行的添加位置。
以下代码:
Adapter.class:
package b.calvin.com.evaccheck;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class PersonAdapter extends
RecyclerView.Adapter<PersonAdapter.PersonViewHolder>{
private Context mCtx;
private List<String> peopleNames;
private List<String> peoplePhones;
public PersonAdapter(Context mCtx, List<String> peopleNames, List<String> peoplePhones) {
this.mCtx = mCtx;
this.peopleNames = peopleNames;
this.peoplePhones = peoplePhones;
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.row_addpersonnew,null);
PersonViewHolder holder = new PersonViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
holder.personsName.setTag(R.id.personNameEditText, position);
String person = peopleNames.get(position);
if(!person.equals("HOLDER")){
holder.personsName.setText(peopleNames.get(position));
}
}
@Override
public int getItemCount() {
return peopleNames.size();
}
class PersonViewHolder extends RecyclerView.ViewHolder{
EditText personsName;
EditText personsContact;
public PersonViewHolder(View itemView) {
super(itemView);
personsName = itemView.findViewById(R.id.personNameEditText);
personsContact = itemView.findViewById(R.id.personPhoneNoEditText);
personsName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
int position = (int) personsName.getTag(R.id.personNameEditText);
peopleNames.set(getAdapterPosition(),personsName.getText().toString());
System.out.println(peopleNames.get(getAdapterPosition()));
}
});
}
}
}
AddPeopleNewProcedureActivity.class:
package b.calvin.com.evaccheck;
public class AddPeopleNewProcedureActivity extends AppCompatActivity {
private FloatingActionButton fab;
private EditText personsNameET;
private EditText personsPhoneET;
private RecyclerView recyclerView;
private PersonAdapter personAdapter;
private List<String> peopleNames;
private List<String> peoplePhones;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addpeoplenew_activity);
setupActivityReferences();
setupClickListeners();
}
private void setupClickListeners() {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
peopleNames.add("HOLDER");
personAdapter.notifyDataSetChanged();
}
});
}
private void setupActivityReferences() {
fab = findViewById(R.id.fabAddPerson);
peopleNames = new ArrayList<String>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
peopleNames.add("HOLDER");
personAdapter = new PersonAdapter(this,peopleNames,peoplePhones);
recyclerView.setAdapter(personAdapter);
}
}