0

I'm developing an android application using eclipse. I have a listview and each row of this contains an EditText. When I click on an EditText I want two things to do.

1: Reload all the EditText with texts from my data array.

2: Set the text of the clicked EditText to a constant string(say "0").

For this I used the below onclicklisetner for EditText.

  myEdittext.setOnClickListener(new View.OnClickListener() 
{
     @Override
      public void onClick(View v) 
       {
        adapter.notifyDataSetChanged();//adapter is my arrayadapter of the listview
        EditText clickedText = (EditText)v;  
        clickedText.setText("0");
       }
 });

As you can see I'm using 'notifyDataSetChanged' method to reload the EditTexts in the listview and 'setText' method to set the clicked EditText's text to "0".

But the 'setText' method is not working. If I comment the line 'adapter.notifyDataSetChanged()' the 'setText' method is working.

I also tried to do this by calling the setText method after a delay(to check if the notifyDataSetChanged is starting a new thread) but failed.

How can I make both the methods to work in my onClick method.

Thanks in Advance.

jjpp
  • 1,298
  • 1
  • 15
  • 31

2 Answers2

0

You can do it using another approach.

You might be setting the ListView adapter using an ArrayList say mList.

Now in the onItemClick listener of the list view you get the position of the item clicked in the ListView. In the onClick() set the value 0 at that position of the arrayList eg mList.set(position, 0). and then call notifyDataChanged()

pvn
  • 2,016
  • 19
  • 33
  • I cant do that. If the user doesnot entered the correct value in the textfield the array should not be changed. I want to set the text just when the user clicks on a textfield. – jjpp Feb 11 '13 at 08:54
  • @jjpp Simple in the onClick() check whether the textField was set properly and change the arrayList only if it was set properly – pvn Feb 11 '13 at 08:57
  • In case by the proper setting of the textView you mean only integers then you can set the property of the TextView so that only integers can be entered – pvn Feb 11 '13 at 08:58
0

try :

So the reason for such behavior is that when you call adapter.notifyDataSetChanged(), there is a good chance that your View V is recycled hence it no longer refers to the one that is clicked. What you really want to do is to change the list so that when notifyDataSetChanged is called then all the values will be loaded accordingly.

Please share your adapter code.

  myEdittext.setOnClickListener(new View.OnClickListener() 
{
     @Override
      public void onClick(View v) 
       {
        //adapter is my arrayadapter of the listview
        //EditText clickedText = (EditText)v;  
        //clickedText.setText("0");

         //Do some thing to change the adapter data list.
         adapter.notifyDataSetChanged();
       }
 });
Nimish Choudhary
  • 2,048
  • 18
  • 17