具有EditTexts Android的动态ListView

时间:2018-11-25 09:08:22

标签: java android listview android-edittext

我的Android应用程序中有一个使用CustomAdapterClass的ListView。 ListView填充的行每行包含2个“编辑文本”。很好,我遇到的问题是ListView是动态的,即我添加了一个按钮,该按钮将通过调用notifyDataSetChanged()追加到新行上;但这清除了整个列表。

当添加新行时,如何保留输入到EditTexts中的文本?下面的代码:

Activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">



    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="afterDescendants"
        android:id="@+id/listViewAddPeople">
    </ListView>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabAddPerson"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@mipmap/ic_launcher"
        app:backgroundTint="#FFFFFF"
        android:elevation="6dp"
        />


</RelativeLayout>

Row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res
/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/personNameEditText"
    android:hint="Enter Persons Full Name"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/personPhoneNoEditText"
    android:layout_below="@+id/personNameEditText"
    android:hint="Enter Phone No. (Optional)"/>

</RelativeLayout>

Java类:

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;
            personsName.clear();
            personsPhone.clear();
            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);
    personsNameET = (EditText) findViewById(R.id.personNameEditText);
    personsPhoneET = (EditText) findViewById(R.id.personPhoneNoEditText);
    lineCount = 1;

}

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) {

        view =                
       getLayoutInflater().inflate(R.layout.row_addpersonnew,null); 

        final int rowClicked = i;

        return view;
    }
}

}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

  

我添加了一个按钮,该按钮将通过调用添加到新行上   notifyDataSetChanged();

当某人单击该按钮时,我看不到任何追加操作。我看到您清除了两个列表personsNamepersonsPhone,然后您通知适配器这些列表为空

onClick()内写出真正想要做的事情。