0

I have to find the coordinates of a ImageView inside my ListView but I can't make it. I have tried with getItemAtPosition() but the APP crashes. Each ImageView has a different tag and in each row of my ListView.

My function:

ListView listStudents = (ListView) findViewById(R.id.listStudents);
View view = (View) listStudents.getItemAtPosition(0);

This is my ListViewAdapter:

private ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();


public ListViewAdapter(Context context, ArrayList<HashMap<String,String>> items) {
     mContext = context;
     this.list = items;
}

public int getCount() {
     return list.size();
}

@Override
public Object getItem(int position) {
     return list.get(position);
}

public long getItemId(int position) {
     return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.row_layout, null);

    /*
     * Button 1
     */
    holder.imageStudent   = (ImageView)convertView.findViewById(R.id.firstStudentImage);
    holder.imageStudent.setAlpha(50);
    holder.imageStudent.setOnClickListener(this);
    holder.imageStudent.setTag(this.numElements);
    holder.nameStudent   = (TextView)convertView.findViewById(R.id.firstStudentName);

    /*
     * Button 2
     */
    holder.imageStudent2   = (ImageView)convertView.findViewById(R.id.secondStudentImage);
    holder.imageStudent2.setAlpha(50);
    holder.imageStudent2.setOnClickListener(this);
    holder.imageStudent2.setTag(this.numElements+1);
    holder.nameStudent2   = (TextView)convertView.findViewById(R.id.secondStudentName);


    /*
     * Button 3
     */
    holder.imageStudent3   = (ImageView)convertView.findViewById(R.id.thirdStudentImage);
    holder.imageStudent3.setAlpha(50);
    holder.nameStudent3 = (TextView)convertView.findViewById(R.id.thirdStudentName);
    holder.imageStudent3.setTag(this.numElements+2);
    holder.imageStudent3.setOnClickListener(this);


    holder.imageStudent4   = (ImageView)convertView.findViewById(R.id.fourthStudentImage);
    holder.imageStudent4.setAlpha(50);
    holder.nameStudent4 = (TextView)convertView.findViewById(R.id.fourthStudentName);
    long value = 2723250804L;
    holder.imageStudent4.setTag(value);
    holder.imageStudent4.setOnClickListener(this);

    /*
     * Button 5
     */
    holder.imageStudent5   = (ImageView)convertView.findViewById(R.id.fivethStudentImage);
    holder.imageStudent5.setAlpha(50);
    holder.nameStudent5   = (TextView)convertView.findViewById(R.id.fivethStudentName);
    holder.imageStudent5.setTag(this.numElements+4);
    holder.imageStudent5.setOnClickListener(this);

    /*
     * Button 6
     */
    holder.imageStudent6   = (ImageView)convertView.findViewById(R.id.sixthStudentImage);
    holder.imageStudent6.setAlpha(50);
    holder.nameStudent6   = (TextView)convertView.findViewById(R.id.sixthStudentName);
    holder.imageStudent6.setTag(this.numElements+5);
    holder.imageStudent6.setOnClickListener(this);


    convertView.setTag(holder);

    int  idImage = this.mContext.getResources().getIdentifier(this.list.get(position).get("studentImage"),"drawable", this.mContext.getPackageName());
    Drawable myDrawable = this.mContext.getResources().getDrawable(idImage);
    holder.imageStudent.setImageDrawable(myDrawable);
    holder.nameStudent.setText(this.list.get(position).get("studentName"));


    idImage = this.mContext.getResources().getIdentifier(this.list.get(position).get("studentImage2"),"drawable", this.mContext.getPackageName());
    myDrawable = this.mContext.getResources().getDrawable(idImage);
    holder.imageStudent2.setImageDrawable(myDrawable);
    holder.nameStudent2.setText(this.list.get(position).get("studentName2"));

    idImage = this.mContext.getResources().getIdentifier(this.list.get(position).get("studentImage3"),"drawable", this.mContext.getPackageName());
    myDrawable = this.mContext.getResources().getDrawable(idImage);
    holder.imageStudent3.setImageDrawable(myDrawable);
    holder.nameStudent3.setText(this.list.get(position).get("studentName3"));

    idImage = this.mContext.getResources().getIdentifier(this.list.get(position).get("studentImage4"),"drawable", this.mContext.getPackageName());
    myDrawable = this.mContext.getResources().getDrawable(idImage);
    holder.imageStudent4.setImageDrawable(myDrawable);
    holder.nameStudent4.setText(this.list.get(position).get("studentName4"));

    idImage = this.mContext.getResources().getIdentifier(this.list.get(position).get("studentImage5"),"drawable", this.mContext.getPackageName());
    myDrawable = this.mContext.getResources().getDrawable(idImage);
    holder.imageStudent5.setImageDrawable(myDrawable);
    holder.nameStudent5.setText(this.list.get(position).get("studentName5"));

    idImage = this.mContext.getResources().getIdentifier(this.list.get(position).get("studentImage6"),"drawable", this.mContext.getPackageName());
    myDrawable = this.mContext.getResources().getDrawable(idImage);
    holder.imageStudent6.setImageDrawable(myDrawable);
    holder.nameStudent6.setText(this.list.get(position).get("studentName3"));
}
else
{
    holder = (ViewHolder)convertView.getTag();
}


return convertView;
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    EventActivity.currentButton = (Integer) v.getTag();
Activity activity = (Activity) mContext;
activity.showDialog(1);
EventActivity.currentView = (View) v.getParent();
}

This is my row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="10dp"
    android:orientation="horizontal" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginRight="40dp"  
    android:layout_marginLeft="20dp"
    >
    <ImageView 
        android:id="@+id/firstStudentImage"
        android:contentDescription="@string/test"
        android:maxWidth="250dp"    
        android:maxHeight="150dp"
        android:layout_width="81dp"
        android:layout_height="115dp"
        >
    </ImageView>
    <TextView
        android:id="@+id/firstStudentName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textIsSelectable="false">
    </TextView>

    <Button
        android:id="@+id/firstStudentButton"
        android:layout_width="81dp"
        android:layout_height="wrap_content"
        android:background="@color/attendance_no_data"
        android:textColor="@color/settings_text_color"
        android:text="@string/attendance_no_data" >
    </Button>
</LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="40dp"   
    android:orientation="vertical">
    <ImageView 
        android:id="@+id/secondStudentImage"
        android:contentDescription="@string/test"
        android:maxWidth="250dp"    
        android:maxHeight="150dp"
        android:layout_width="81dp"
        android:layout_height="115dp"
        >
    </ImageView>
    <TextView
        android:id="@+id/secondStudentName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textIsSelectable="false">
    </TextView>
    <Button
        android:id="@+id/firstStudentButton"
        android:layout_width="81dp"
        android:layout_height="wrap_content"
        android:background="@color/attendance_no_data"
        android:textColor="@color/settings_text_color"
        android:text="@string/attendance_no_data" >
    </Button>
</LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="40dp"  
    android:orientation="vertical">
    <ImageView 
        android:id="@+id/thirdStudentImage"
        android:contentDescription="@string/test"
        android:maxWidth="250dp"    
        android:maxHeight="150dp"
        android:layout_width="81dp"
        android:layout_height="115dp"
        >
    </ImageView>
    <TextView
        android:id="@+id/thirdStudentName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textIsSelectable="false">
    </TextView>
    <Button
        android:id="@+id/firstStudentButton"
        android:layout_marginLeft="25dp"
        android:layout_width="81dp"
        android:layout_height="wrap_content"
        android:background="@color/attendance_no_data"
        android:textColor="@color/settings_text_color"
        android:text="@string/attendance_no_data" >
    </Button>
</LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginRight="40dp"  
                android:orientation="vertical">
    <ImageView 
        android:id="@+id/fourthStudentImage"
        android:contentDescription="@string/test"
        android:maxWidth="250dp"    
        android:maxHeight="150dp"
        android:layout_width="81dp"
        android:layout_height="115dp"
        >
    </ImageView>
    <TextView
        android:id="@+id/fourthStudentName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textIsSelectable="false">
    </TextView>
    <Button
        android:id="@+id/firstStudentButton"
        android:layout_marginLeft="25dp"
        android:layout_width="81dp"
        android:layout_height="wrap_content"
        android:background="@color/attendance_no_data"
        android:textColor="@color/settings_text_color"
        android:text="@string/attendance_no_data" >
    </Button>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
         android:layout_height="match_parent"
     android:layout_marginRight="40dp"
         android:orientation="vertical">
    <ImageView 
        android:id="@+id/fivethStudentImage"
        android:contentDescription="@string/test"
        android:maxWidth="250dp"    
        android:maxHeight="150dp"
        android:layout_width="81dp"
        android:layout_height="115dp">
    </ImageView>
    <TextView
        android:id="@+id/fivethStudentName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textIsSelectable="false">
    </TextView>
    <Button
        android:id="@+id/firstStudentButton"
        android:layout_marginLeft="25dp"
        android:layout_width="81dp"
        android:layout_height="wrap_content"
        android:background="@color/attendance_no_data"
        android:textColor="@color/settings_text_color"
        android:text="@string/attendance_no_data" >
    </Button>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="40dp"
    android:orientation="vertical">
    <ImageView 
        android:id="@+id/sixthStudentImage"
        android:contentDescription="@string/test"
        android:maxWidth="250dp"    
        android:maxHeight="150dp"
            android:layout_width="81dp"
        android:layout_height="115dp">
    </ImageView>
    <TextView
        android:id="@+id/sixthStudentName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textIsSelectable="false">
    </TextView>
    <Button
        android:id="@+id/firstStudentButton"
        android:layout_marginLeft="25dp"
        android:layout_width="81dp"
        android:layout_height="wrap_content"
        android:background="@color/attendance_no_data"
        android:textColor="@color/settings_text_color"
        android:text="@string/attendance_no_data" >
    </Button>
</LinearLayout>

This is the error:

    04-05 15:57:46.238: E/AndroidRuntime(22906): FATAL EXCEPTION: main
04-05 15:57:46.238: E/AndroidRuntime(22906): java.lang.ClassCastException: java.lang.Integer cannot be cast to com.nsts.eventmarker.ListViewAdapter$ViewHolder
04-05 15:57:46.238: E/AndroidRuntime(22906):    at com.nsts.eventmarker.ListViewAdapter.getView(ListViewAdapter.java:187)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.widget.AbsListView.obtainView(AbsListView.java:2143)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.widget.ListView.onMeasure(ListView.java:1158)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.View.measure(View.java:15513)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.View.measure(View.java:15513)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.View.measure(View.java:15513)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.View.measure(View.java:15513)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.View.measure(View.java:15513)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.Choreographer.doFrame(Choreographer.java:532)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.os.Handler.handleCallback(Handler.java:725)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.os.Looper.loop(Looper.java:137)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at android.app.ActivityThread.main(ActivityThread.java:5039)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at java.lang.reflect.Method.invokeNative(Native Method)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at java.lang.reflect.Method.invoke(Method.java:511)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-05 15:57:46.238: E/AndroidRuntime(22906):    at dalvik.system.NativeStart.main(Native Method)
grouser
  • 618
  • 8
  • 23

2 Answers2

0

it's a ClassCastException

EventActivity.currentButton = (Integer) v.getTag(); 

i think error is on this line

long value = 2723250804L;

this is long value and you are setting this as tag and then you are trying to convert this long to integer . I think which is not possible

Actually v.getTag(); returns object .And you are trying to convert object to Integer . i are you sure v.getTag(); returns integer . Then only it works (Refer)

what is this data type of EventActivity.currentButton ??

Community
  • 1
  • 1
edwin
  • 7,985
  • 10
  • 51
  • 82
0

Check out this post. I think it answers your question of how to get a view by using the setTag and getTag methods to find something at a specific position.

The error is a ClassCastException. It is either occuring where you cast the tag id to a ViewHolder at the end of your code or where you are casting the view tag to an Integer. Look at the example above and set up the code that way to avoid this error.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46