借助AlertDialog将新对象添加到自定义数组适配器

时间:2017-12-31 23:53:53

标签: java android dialog android-alertdialog

我尝试在对话框的帮助下添加新客户端,但看起来我的编辑文本不存在。

看起来所有的ID都是正确的,活动现场周期一切都很好,但我得到了:

     String nameValue = clientName.getText().toString().trim();

在线:

public class MainActivity extends Activity{

    ArrayList<Client> clientListItems = new ArrayList<Client>();
    ClientAdapter clientAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        initClients();

        clientAdapter = new ClientAdapter(this, clientListItems);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(clientAdapter);

        Button buttonView = (Button)findViewById(R.id.addListItem);

        buttonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                setAddingDialog();

            }
        });



    }

    /*
    @Override
    protected void onStart() {
        super.onStart();
        Button buttonView = (Button)findViewById(R.id.addListItem);

        buttonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //listItems.add(0, Utils.hashGenerator(25));
                //adapter.notifyDataSetChanged();

                //DialogFragment newFragment = new AddingDialog();
                //newFragment.show(getFragmentManager(), "adding");
                setAddingDialog();

            }
        });
    }
    */

    void initClients(){
        clientListItems.add(new Client("alex", 1265, new Wallet("151516456464564654", 4564.56), R.drawable.ic_launcher_foreground));
        clientListItems.add(new Client("mike", 26, new Wallet("465456445644123231", 1645.2), R.drawable.ic_launcher_foreground));
        clientListItems.add(new Client("tray", 145, new Wallet("12315465489789", 0.00), R.drawable.ic_launcher_foreground));
        clientListItems.add(new Client("vincent", 999, new Wallet("3213546549789", 1000000.01), R.drawable.ic_launcher_foreground));
        clientListItems.add(new Client("venom", 666, new Wallet("321154654654798", 145.6), R.drawable.ic_launcher_foreground));
    }

    void setAddingDialog(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();

        Dialog alertDialog = builder.create();

        final EditText clientName = (EditText) alertDialog.findViewById(R.id.newClientName);
        final EditText clientId = (EditText) alertDialog.findViewById(R.id.newClientId);
        final EditText clientWallet = (EditText) alertDialog.findViewById(R.id.newClientWallet);
        final EditText clientBalance = (EditText) alertDialog.findViewById(R.id.newClientBalance);

        builder.setView(inflater.inflate(R.layout.dialog_add_new_client, null))
                .setPositiveButton("add", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String nameValue = clientName.getText().toString().trim();
                        int idValue = Integer.valueOf(clientId.getText().toString().trim());
                        String walletValue = clientWallet.getText().toString().trim();
                        double balanceValue = Double.valueOf(clientBalance.getText().toString().trim());
                        clientListItems.add(new Client(nameValue, idValue, new Wallet(walletValue, balanceValue), R.drawable.ic_launcher_foreground));
                    }
                })
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                })
                .show();

    }
}

任何人都可以解释出了什么问题吗?

MainActivity.java

<?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="match_parent">

    <EditText
        android:id="@+id/newClientName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="16dp"
        android:hint="name"
        android:inputType="text" />

    <EditText
        android:id="@+id/newClientId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="16dp"
        android:hint="id"
        android:inputType="number" />

    <EditText
        android:id="@+id/newClientWallet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="16dp"
        android:hint="walletNumber"
        android:inputType="text" />

    <EditText
        android:id="@+id/newClientBalance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="16dp"
        android:hint="balance"
        android:inputType="numberDecimal" />

</LinearLayout>

dialog_add_new_client.xml

{{1}}

2 个答案:

答案 0 :(得分:0)

移动此代码:

 Dialog alertDialog = builder.create();

 final EditText clientName = (EditText) alertDialog.findViewById(R.id.newClientName);
 final EditText clientId = (EditText) alertDialog.findViewById(R.id.newClientId);
 final EditText clientWallet = (EditText) alertDialog.findViewById(R.id.newClientWallet);
 final EditText clientBalance = (EditText) alertDialog.findViewById(R.id.newClientBalance);

builder.setView(inflater.inflate(R.layout.dialog_add_new_client, null))
            .setPositiveButton("add", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    String nameValue = clientName.getText().toString().trim();
                    int idValue = Integer.valueOf(clientId.getText().toString().trim());
                    String walletValue = clientWallet.getText().toString().trim();
                    double balanceValue = Double.valueOf(clientBalance.getText().toString().trim());
                    clientListItems.add(new Client(nameValue, idValue, new Wallet(walletValue, balanceValue), R.drawable.ic_launcher_foreground));
                }
            })
            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });

分配EditTexts时,对话框自定义视图尚未膨胀,因此它们都为空。此外,您还需要在膨胀的视图上调用findViewById而不是警告对话框。

请勿在设置自定义布局时调用show(),但只能在文本视图初始化后通过调用以下方式调用:

alertDialog.show();

修改

将您的EditTexts声明为您活动的全局字段,因此他们不需要是最终的。

总结一下,用这种方式改变你的setAddingDialog()

private EditText clientName;
private EditText clientId;
private EditText clientWallet;
private EditText clientBalance;

void setAddingDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();

    View dialogView = inflater.inflate(R.layout.dialog_add_new_client, null);
    builder.setView(dialogView)
           .setPositiveButton("add", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    String nameValue = clientName.getText().toString().trim();
                    int idValue = Integer.valueOf(clientId.getText().toString().trim());
                    String walletValue = clientWallet.getText().toString().trim();
                    double balanceValue = Double.valueOf(clientBalance.getText().toString().trim());
                    clientListItems.add(new Client(nameValue, idValue, new Wallet(walletValue, balanceValue), R.drawable.ic_launcher_foreground));
                }
            })
           .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });

    Dialog alertDialog = builder.create();

    clientName = (EditText) dialogView.findViewById(R.id.newClientName);
    clientId = (EditText) dialogView.findViewById(R.id.newClientId);
    clientWallet = (EditText) dialogView.findViewById(R.id.newClientWallet);
    clientBalance = (EditText) dialogView.findViewById(R.id.newClientBalance);

    alertDialog.show();
}

<强>更新 您需要在膨胀的视图上调用findViewById而不是警告对话框。检查我更新的代码。

答案 1 :(得分:0)

只需在此之前添加对话框布局 - :

builder.setContentView(R.layout.dialog_add_new_client);

 final EditText clientName = (EditText) alertDialog.findViewById(R.id.newClientName);
        final EditText clientId = (EditText) alertDialog.findViewById(R.id.newClientId);
        final EditText clientWallet = (EditText) alertDialog.findViewById(R.id.newClientWallet);
        final EditText clientBalance = (EditText) alertDialog.findViewById(R.id.newClientBalance);

        builder.setPositiveButton("add", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String nameValue = clientName.getText().toString().trim();
                        int idValue = Integer.valueOf(clientId.getText().toString().trim());
                        String walletValue = clientWallet.getText().toString().trim();
                        double balanceValue = Double.valueOf(clientBalance.getText().toString().trim());
                        clientListItems.add(new Client(nameValue, idValue, new Wallet(walletValue, balanceValue), R.drawable.ic_launcher_foreground));
                    }
                })
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                })
                .show();

    }
}

您需要先添加布局内容,然后初始化edittexts。