当我使用“.getText()。toString();”时会出现什么问题。“

时间:2011-10-08 16:15:02

标签: android-widget

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            EditText nameText = (EditText) findViewById(R.id.editText1);
            name = nameText.getText().toString();
            nameText.setText("");
            EditText numberText = (EditText) findViewById(R.id.editText2);
            number = numberText.getText().toString();
            numberText.setText("");
            person = new Person(name, number);
            order.add(person);

            PersonAdapter pa = new PersonAdapter(con, R.layout.row, order);
            setListAdapter(pa);
        }
    });
}

@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
    // TODO Auto-generated method stub
    final int index = position;
    AlertDialog.Builder aDialog = new AlertDialog.Builder(this);
    aDialog.setTitle("What do you want??");
    aDialog.setPositiveButton("Delete",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    order.remove(index);
                    PersonAdapter pa = new PersonAdapter(con, R.layout.row,
                            order);
                    setListAdapter(pa);
                }
            });

    aDialog.setNeutralButton("Edit", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = (LayoutInflater) con.getSystemService(LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.root_layout));
            builder = new AlertDialog.Builder(con);
            builder.setView(layout);
            builder.setTitle("Fill EditText");
            builder.setIcon(R.drawable.icon);
            editNum = (EditText)findViewById(R.id.editNum);
            editName = (EditText)findViewById(R.id.editName);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                    name = editName.getText().toString();
                    number = editNum.getText().toString();

                    order.set(position, new Person(name,number));

                    PersonAdapter pa = new PersonAdapter(con, R.layout.row, order);
                    setListAdapter(pa);
                }
            });

            builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                }
            });

            alertDialog = builder.create();
            alertDialog.show();

        }
    });

    aDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            });
    AlertDialog ad = aDialog.create();
    ad.show();
}


public PersonAdapter(Context context, int textViewResourceId,
        ArrayList<Person> items) {
    super(context, textViewResourceId, items);
    this.items = items;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.row, null);
    }

    Person p = items.get(position);
    if (p != null) {
        TextView tt = (TextView) v.findViewById(R.id.android_toptext);
        TextView bt = (TextView) v.findViewById(R.id.android_bottomtext);
        if (tt != null) {
            tt.setText(p.getName());
        }
        if (bt != null) {
            bt.setText("TEL : " + p.getNumber());
        }
    }
    return v;
}

private String name;
private String number;

public Person(String name, String number) {
    this.name = name;
    this.number = number;
}

public String getName() {
    return name;
}

public String getNumber() {
    return number;
}

}

我正在练习Widget(我正在制作自定义对话框)和Listener在android中。但我现在停止因为“.getText()。toString();”犯错误......我无法理解.. T ^ T请帮助我......为什么会出错?我认为它没有问题

1 个答案:

答案 0 :(得分:1)

您应该全局声明EditText

EditText editName; 
EditText editNum;

@Override
    public void onCreate(Bundle savedInstanceState) {
...
}

.....

 @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = (LayoutInflater) con.getSystemService(LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.root_layout));
            builder = new AlertDialog.Builder(con);
            builder.setView(layout);
            builder.setTitle("Fill EditText modificated");
            builder.setIcon(R.drawable.icon);
            editName = (EditText)findViewById(R.id.editName);
            editNum = (EditText)findViewById(R.id.editNum);