0

I am not able to get the hidden field value of the clicked item.

I followed SO Post to get the hidden value of the clicked listview item but unable to get the value. I am getting ClassCastException.

The code -

listView.setOnItemClickListener(new OnItemClickListener() 
{
   //////////list view on click
   public void onItemClick(AdapterView<?> view, View arg1,
      int position, long arg3) {

      String S = (String)view.getItemAtPosition(position); //This line gives exception
      System.out.println("TextView vehicleSrc>>"+S);


     //OR

    String itemSelected =  ((TextView)view.findViewById(R.id.vehicle_source)).getText().toString();
   System.out.println("TextView vehicleSrc"+itemSelected); // this is giving blank
   }

My error log -

03-20 12:47:08.742: E/AndroidRuntime(26965): FATAL EXCEPTION: main
03-20 12:47:08.742: E/AndroidRuntime(26965): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
03-20 12:47:08.742: E/AndroidRuntime(26965):    at com.iddl.main.EntryFragment$13$2.onItemClick(UserFragment.java:907)

UPDATE:

I have defined the above textview in hidden field in xml-

<TextView
     android:id="@+id/vehicle_source"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="" 
     android:visibility="gone"/>

Now I am trying to set its value for each listview item as -

TextView vehicleSrc = (TextView) vi.findViewById(R.id.vehicle_source);

if(isCondition1)
vehicleSrc.setText("Normal");
else
vehicleSrc.setText("Other");
end
Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • Try Integer value = (Integer)view.getItemAtPosition(position); – Yasitha Waduge Mar 20 '13 at 07:24
  • You can also use String s = String.valueOf(view.getItemAtPosition(position)); – the-ginger-geek Mar 20 '13 at 07:28
  • http://stackoverflow.com/questions/8973381/why-cannot-cast-integer-to-string-in-java try this... – vlio20 Mar 20 '13 at 07:28
  • @Neil this is giving me the clicked item position but I am not able to get the clicked item hidden field value. See the update. – sjain Mar 20 '13 at 07:50
  • @SaurabhJain : `android:visibility="gone"` completely removes a view, it doesn't just hide it. – Squonk Mar 20 '13 at 07:54
  • @Squonk yes basically my idea is to just set each item with some hidden value so that I can use that value on item click. So what you said is correct in my context. The problem is that, I am unable to get the set hidden field value on item click. – sjain Mar 20 '13 at 08:01
  • @SaurabhJain : Please read documentation http://developer.android.com/reference/android/view/View.html#setVisibility(int) `gone` means the view doesn't exist at all - it's not just hidden. To see a view, make it `visible` but to hide it and still have it exist set it to `invisible`. If you set a view's visibility to `gone` you will never find it. – Squonk Mar 20 '13 at 10:29

4 Answers4

1

Finally I got the hidden field value for the clicked item.

So, it should be

String itemSelected = ((TextView)arg1.findViewById(R.id.vehicle_source)).getText().toString();

instead of

String itemSelected =  ((TextView)view.findViewById(R.id.vehicle_source)).getText().toString();

Complete code -

listView.setOnItemClickListener(new OnItemClickListener() 
{
   //////////list view on click
   public void onItemClick(AdapterView<?> view, View arg1,
      int position, long arg3) {

    String itemSelected =  ((TextView)arg1.findViewById(R.id.vehicle_source)).getText().toString();
   System.out.println("TextView vehicleSrc"+itemSelected);
   }
}
sjain
  • 23,126
  • 28
  • 107
  • 185
0

You cannot cast integer to string like what been warned here

03-20 12:47:08.742: E/AndroidRuntime(26965): FATAL EXCEPTION: main
03-20 12:47:08.742: E/AndroidRuntime(26965): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
03-20 12:47:08.742: E/AndroidRuntime(26965):    at com.iddl.main.EntryFragment$13$2.onItemClick(UserFragment.java:907)

if you want to to convert the integer to string you can use

Integer.toString( i )

like this one

String S = Integer.toString(view.getItemAtPosition(position)); //This line gives exception
      System.out.println("TextView vehicleSrc>>"+S);

and I think you should use android Log.i/Log.d/Log.e to log anything instead of system.out.println :)

Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
  • there is a syntax error in your first line. It is giving - `The method toString(int) in the type Integer is not applicable for the arguments (Object)` – sjain Mar 20 '13 at 07:59
0
  1. Never cast anything without using instancof

    So your code ideally should be

    Object item = view.getItemAtPosition(position);

       if(item instanceof String){
         //cast it
       }
    
  2. Just check What type of adapter you have used.. getItem returns TYPE Adapter<TYPE>

ngesh
  • 13,398
  • 4
  • 44
  • 60
0

Try out this you will get the value of list list which is clicked

            String textOfSelectedItem;
                  listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int id,
                long position) {
            // TODO Auto-generated method stub
             textOfSelectedItem  =((TextView) v).getText().toString();
            Toast.makeText(getApplicationContext(), "Clicked is " + textOfSelectedItem, Toast.LENGTH_SHORT).show();

        }
    });
user1835052
  • 455
  • 1
  • 5
  • 11