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;
}
}
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.