我有一个ListView
,可以通过调用notifyDataSetChanged();
并将行数增加1,将新行追加到底部。ListView
包含EditTexts.
的行
调用notifyDataSetChanged();
之后,我要保留用户先前输入的值。只要我不返回并更新在调用notifyDataSetChanged();
后已经输入的值之一,它似乎就可以正常工作。代码如下:
public class AddPeopleNewProcedureActivity extends AppCompatActivity {
private ListView addPeopleListView;
private CustomAdapterClass customAdapter;
private FloatingActionButton fab;
private int lineCount;
private EditText personsNameET;
private EditText personsPhoneET;
private List<String> personsName;
private List<String> personsPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addpeoplenew_activity);
setupActivityReferences();
inflateListView();
setupClickListeners();
}
private void setupClickListeners() {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
lineCount = lineCount +1;
customAdapter.notifyDataSetChanged();
}
});
}
private void inflateListView() {
customAdapter = new CustomAdapterClass();
addPeopleListView.setAdapter(customAdapter);
}
private void setupActivityReferences() {
addPeopleListView = (ListView) findViewById(R.id.listViewAddPeople);
fab = findViewById(R.id.fabAddPerson);
lineCount = 1;
personsName = new ArrayList<String>();
personsPhone = new ArrayList<String>();
}
public class CustomAdapterClass extends BaseAdapter {
@Override
public int getCount() {
return lineCount;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null){
view = getLayoutInflater().inflate(R.layout.row_addpersonnew,null);
}
personsNameET = (EditText) view.findViewById(R.id.personNameEditText);
personsPhoneET = (EditText) view.findViewById(R.id.personPhoneNoEditText);
final int rowClicked = i;
if (personsName != null && !personsName.isEmpty() && !(rowClicked >= personsName.size())){
personsNameET.setText(personsName.get(rowClicked));
}
if (personsPhone != null && !personsPhone.isEmpty() && !(rowClicked >= personsPhone.size())){
personsPhoneET.setText(personsPhone.get(rowClicked));
}
personsNameET.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) {
if(!personsName.isEmpty()){
if (!(rowClicked >= personsName.size())){
personsName.set(rowClicked,personsNameET.getText().toString());
System.out.println("Set "+rowClicked + "Value is: " + personsName.get(rowClicked));
}else{
personsName.add(rowClicked,personsNameET.getText().toString());
System.out.println("Added" +rowClicked + "Value is: " + personsName.get(rowClicked));
}
} else{
personsName.add(rowClicked,personsNameET.getText().toString());
System.out.println("Added" +rowClicked + "Value is: " + personsName.get(rowClicked));
}
}
});
personsPhoneET.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) {
}
});
return view;
}
}
}
最佳答案
您在代码中使用了两次此代码段。请删除一个空白:
personsPhoneET.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) {
}
});
这样做:
public class AddPeopleNewProcedureActivity extends AppCompatActivity {
private ListView addPeopleListView;
private CustomAdapterClass customAdapter;
private FloatingActionButton fab;
private int lineCount;
private EditText personsNameET;
private EditText personsPhoneET;
private List<String> personsName;
private List<String> personsPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addpeoplenew_activity);
setupActivityReferences();
inflateListView();
setupClickListeners();
}
private void setupClickListeners() {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
lineCount = lineCount +1;
customAdapter.notifyDataSetChanged();
}
});
}
private void inflateListView() {
customAdapter = new CustomAdapterClass();
addPeopleListView.setAdapter(customAdapter);
}
private void setupActivityReferences() {
addPeopleListView = (ListView) findViewById(R.id.listViewAddPeople);
fab = findViewById(R.id.fabAddPerson);
lineCount = 1;
personsName = new ArrayList<String>();
personsPhone = new ArrayList<String>();
}
public class CustomAdapterClass extends BaseAdapter {
@Override
public int getCount() {
return lineCount;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null){
view = getLayoutInflater().inflate(R.layout.row_addpersonnew,null);
}
personsNameET = (EditText) view.findViewById(R.id.personNameEditText);
personsPhoneET = (EditText) view.findViewById(R.id.personPhoneNoEditText);
final int rowClicked = i;
if (personsName != null && !personsName.isEmpty() && !(rowClicked >= personsName.size())){
personsNameET.setText(personsName.get(rowClicked));
}
if (personsPhone != null && !personsPhone.isEmpty() && !(rowClicked >= personsPhone.size())){
personsPhoneET.setText(personsPhone.get(rowClicked));
}
personsNameET.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) {
if(!personsName.isEmpty()){
if (!(rowClicked >= personsName.size())){
personsName.set(rowClicked,personsNameET.getText().toString());
System.out.println("Set "+rowClicked + "Value is: " + personsName.get(rowClicked));
}else{
personsName.add(rowClicked,personsNameET.getText().toString());
System.out.println("Added" +rowClicked + "Value is: " + personsName.get(rowClicked));
}
} else{
personsName.add(rowClicked,personsNameET.getText().toString());
System.out.println("Added" +rowClicked + "Value is: " + personsName.get(rowClicked));
}
}
});
}