Alert.Dialog中的冲突

时间:2011-10-11 21:05:03

标签: android listview dialog

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

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时,他们会弹出键盘,但当我按回去禁用它或者甚至按“OK”添加它时,listview的元素会一直重复。我不明白为什么?谁知道这个问题?

感谢。

更新:

问题似乎在我的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;
    }
}

1 个答案:

答案 0 :(得分:1)

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