Reputation: 34
I have a listView of objects each containing one EditText, I used the code from this page. My problem is that when I type something in an edittext, or when I add one to the listView, every one of them gets the same value.
This is my xml for the object : `
<Button
android:layout_margin="16dp"
android:layout_width="56dp"
android:layout_height="56dp"
android:background="@drawable/button_normal"
android:id="@+id/row_button"/>
<EditText
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_toRightOf="@id/row_button"
android:id="@+id/row_edittext"
android:saveEnabled="false"
android:hint="@string/NomDuJoueur"/>
`
My object class is the same as in the webpage.
...and my GetView from the adapter:
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
//Log.wtf("New", "Holder");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_player, null, true);
holder.editText = convertView.findViewById(R.id.row_edittext);
holder.button = convertView.findViewById(R.id.row_button);
convertView.setTag(holder);
}else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)convertView.getTag();
}
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editModelArrayList.remove(position);
notifyDataSetChanged();
}
});
holder.editText.setText(editModelArrayList.get(position).getEditTextValue());
holder.editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
try {
editModelArrayList.get(position).setEditTextValue(holder.editText.getText().toString());
//Log.wtf("AAAD", Integer.toString(holder.editText.getId()));
//Log.wtf("HNCZ", Integer.toString(position)+holder.editText.getText());
}catch (Exception e){
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
return convertView;
}
I think this is caused by the Edittexts all having the same Id, but I wasn't able to find a way to modify their Id that would work.
Upvotes: 0
Views: 76
Reputation: 1924
I'm suspecting that your TextChangedListener
doesn't have access to the correct position. Here's something that you can try.
Add a variable to store the position wherever your ViewHolder
is defined:
class ViewHolder{
// Just add this after wherever you're defining your 'editText' and 'button'
// variables for your holder
int position;
}
Then, inside your getView()
, store the position after your holder is set up:
if(convertView == null){
// your holder set up
}else{
// your holder retrieval
}
// Store the position inside the holder
holder.position = position;
And use the position from the holder inside your onTextChanged()
:
try{
editModelArrayList.get(holder.position).setEditTextValue(holder.editText.getText().toString());
}catch (Exception e){
}
Upvotes: 1