更新从多个EditTexts到数据库的更改?

时间:2018-01-06 01:08:01

标签: android

我有ListView的对话框,其中填充了ArrayList<用户>用户,每个my_list_item包含三个编辑文本(名称,地址,城市)。

当我打开对话框时,编辑字段会填充数据。假设我想改变第一个对象的名称和第七个对象的城市。因此,按“保存”按钮想要应用更改,将它们保存在DB(只是这两个字段)中。

最好的方法是,我需要使用textWatcher,OnFocusChangeListener还是其他什么?

此对话框用于填充和更新数据。

private void editUsers(){

        AlertDialog.Builder mBuilder = new AlertDialog.Builder(MyActivity.this);
        View mView = getLayoutInflater().inflate(R.layout.dialog_add_edit, null);
        mBuilder.setTitle("all user of this case: ");
        ListView listView = mView.findViewById(R.id.list_all_users);

        try {

            usersList = getDatabaseHelper().getmUser().queryBuilder()
            .where()
            .eq(User.ID_CASE, case.getId())
            .query();

            MyAdapterEditUser myadaper = new MyAdapterEditUser (MyActivity.this, (ArrayList<User>) userList);
            listView.setAdapter(myadaper);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    mBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener()  {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                //when i click "Save" button i want to save all changes...

            }
        });

        mBuilder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });

        mBuilder.setView(mView);
        AlertDialog dialog = mBuilder.create();
        dialog.show();
    }

这是MyAdapterEditUser

public class MyAdapterEditUser extends BaseAdapter {

    Context context;
    ArrayList<User> allUsersList;

    public MyAdapteAddEditStranke(Context context, ArrayList<User> allUsersList;) {
        this.context = context;
        this.allUsersList = allUsersList;;
    }

    @Override
    public int getCount() {
        return allUsersList;.size();
    }

    @Override
    public UserDetail getItem(int i) {
        return allUsersList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.my_list_item, null);


            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.ref = i;
        viewHolder.populateFrom(allUsersList.get(i));


        return convertView;
    }

    private static class ViewHolder {
        EditText name;
        EditText address;
        EditText city;
        int ref;

        ViewHolder(View row) {
            name = row.findViewById(R.id.my_list_et_name);
            adresa = row.findViewById(R.id.my_list_et_address);
            city = row.findViewById(R.id.my_list_et_city);
        }

        void populateFrom(User user) {

            name.setText(user.getName());
            address.setText(user.getAddress());
            mesto.setText(user.getCity());
        }

    }
}

列出项目xml文件

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


    <EditText android:id="@+id/my_list_et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <EditText android:id="@+id/my_list_et_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <EditText android:id="@+id/my_list_et_city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

在你的正面按钮试试这个!它会有所帮助!

 mBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener()  {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            //here update the array list on the items you want 


//how about checking out which edit text has changed the different values   
//than the old ones like simple if else over each edit Text you require


 try {

        updatedList = //your update query

        MyAdapterEditUser myadaper = new MyAdapterEditUser (MyActivity.this, (ArrayList<User>) updatedList);
        listView.setAdapter(myadaper);

    } catch (SQLException e) {
        e.printStackTrace();
    }

        }
    });