【问题标题】:Dynamic ListView With EditTexts Android带有 EditText Android 的动态 ListView
【发布时间】:2018-11-25 09:08:22
【问题描述】:

我的 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>

行.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;
    }
}

}

感谢任何帮助!

【问题讨论】:

    标签: java android listview android-edittext


    【解决方案1】:

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

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

    onClick() 中写下真正想做的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 2016-12-11
      • 2014-03-23
      相关资源
      最近更新 更多