0

ListView 内の ImageView の座標を見つける必要がありますが、作成できません。getItemAtPosition() を試しましたが、APP がクラッシュします。各 ImageView には異なるタグがあり、ListView の各行にあります。

私の機能:

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

これは私の 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();
}

これは私の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>

これはエラーです:

    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)
4

2 に答える 2

0

それはClassCastException

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

エラーはこの行にあると思います

long value = 2723250804L;

これは long 値であり、これをタグとして設定してから、この long を integer に変換しようとしています。どれが無理だと思う

実際には v.getTag(); オブジェクトを返します。そして、オブジェクトIntegerに変換しようとしています。私はあなたv.getTag();が整数を返すと確信しています。それだけで動作します(参照

このデータ型は何EventActivity.currentButtonですか??

于 2013-04-05T17:14:17.463 に答える
0

この投稿をチェックしてください。setTagメソッドとgetTagメソッドを使用して特定の位置にあるものを見つける方法についての質問に答えていると思います。

エラーはClassCastException. ViewHolderコードの最後でタグ id を にキャストする場所、またはビュー タグを にキャストする場所のいずれかで発生していますInteger。上記の例を見て、このエラーを回避するようにコードを設定してください。

于 2013-04-05T17:24:42.697 に答える