Alert.Dialog 中的冲突

发布于 2024-12-09 08:02:54 字数 2293 浏览 0 评论 0原文

我的活动中有一个列表视图,当我单击按钮时,会以这种方式打开一个对话框:

private void openDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(FriendActivity.this);
    builder.setTitle("Add friend");
    builder.setMessage("Add the name and the phone number for your friend.");
    LayoutInflater fi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = fi.inflate(R.layout.friend_dialog, null);
    final EditText name = (EditText) v.findViewById(R.id.edit1);
    final EditText number = (EditText) v.findViewById(R.id.edit2);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String namee = name.getText().toString().trim();
            String numberr = number.getText().toString().trim();
            db.insertFriend(namee, numberr);
            //updateAdapter();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do nothing
        }
    });
    builder.setView(v);
    builder.show();
}

好的,可以了! 问题是,当我单击 EditText 时,键盘会弹出,但是当我按返回键禁用它或什至按“确定”添加它时,列表视图的元素始终会重复。我不明白为什么?有谁知道这个问题吗?

谢谢。

更新:

问题似乎出在我的 listAdapter =/ 中,当我向上或向下滚动时,文本由于某种原因而重复,这是我的代码:

 class ListAdapter extends ArrayAdapter<Friend>{

    private ImageView img;
    private TextView name, number;
    private Context context;

    public ListAdapter(Context context, int textViewResourceId, List<Friend> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        LayoutInflater vi;

        if(v == null){
            vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }

        Friend item = getItem(position);

        name = (TextView) v.findViewById(R.id.name);
        number = (TextView) v.findViewById(R.id.number);

        name.append(item.getName());
        number.append(item.getNumber());

        return v;
    }
}

I have a listview in my activity, and when I click on a button a dialog is opened in this way:

private void openDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(FriendActivity.this);
    builder.setTitle("Add friend");
    builder.setMessage("Add the name and the phone number for your friend.");
    LayoutInflater fi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = fi.inflate(R.layout.friend_dialog, null);
    final EditText name = (EditText) v.findViewById(R.id.edit1);
    final EditText number = (EditText) v.findViewById(R.id.edit2);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String namee = name.getText().toString().trim();
            String numberr = number.getText().toString().trim();
            db.insertFriend(namee, numberr);
            //updateAdapter();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do nothing
        }
    });
    builder.setView(v);
    builder.show();
}

Okay, that works!
Problem is when I click on the EditText they keyboard pops up, but when I press back to disable it or even press "OK" to add it, the listview's elements gets duplicated all the time. I cant see why? Anyone who knows this problem?

Thanks.

UPDATE:

The problem seems to be in my listAdapter =/ when I scroll up or down the text get duplicated for some reason, here's my code:

 class ListAdapter extends ArrayAdapter<Friend>{

    private ImageView img;
    private TextView name, number;
    private Context context;

    public ListAdapter(Context context, int textViewResourceId, List<Friend> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        LayoutInflater vi;

        if(v == null){
            vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }

        Friend item = getItem(position);

        name = (TextView) v.findViewById(R.id.name);
        number = (TextView) v.findViewById(R.id.number);

        name.append(item.getName());
        number.append(item.getNumber());

        return v;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们的影子 2024-12-16 08:02:54

您的意思是调用 name.append 和 number.append 吗?我认为 name.setText 和 number.setText 听起来更合适。追加方法将不断添加越来越多的文本。

Did you mean to call name.append and number.append? I think name.setText and number.setText sounds more appropriate. The append methods will keep adding more and more text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文