[英]Get View from ListView
I have to find the coordinates of a ImageView inside my ListView but I can't make it. 我必须在ListView内找到ImageView的坐标,但无法实现。 I have tried with getItemAtPosition() but the APP crashes. 我已经尝试过使用getItemAtPosition(),但是APP崩溃了。 Each ImageView has a different tag and in each row of my ListView. 每个ImageView在ListView的每一行中都有一个不同的标记。
My function: 我的功能:
ListView listStudents = (ListView) findViewById(R.id.listStudents);
View view = (View) listStudents.getItemAtPosition(0);
This is my ListViewAdapter: 这是我的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 这是我的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)
it's a ClassCastException
这是一个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 . 这是一个long值,您将其设置为tag,然后尝试将此long转换为integer。 I think which is not possible 我认为这是不可能的
Actually v.getTag(); 实际上是v.getTag(); returns object .And you are trying to convert object to Integer . 返回object。您正在尝试将object转换为Integer 。 i are you sure v.getTag();
我确定v.getTag();
returns integer . 返回integer。 Then only it works ( Refer ) 然后只有它有效( 请参阅 )
what is this data type of EventActivity.currentButton
?? EventActivity.currentButton
这种数据类型是EventActivity.currentButton
?
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. 我认为它回答了您有关如何通过使用setTag
和getTag
方法在特定位置查找getTag
方式获取视图的问题。
The error is a ClassCastException
. 错误是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
. 在代码末尾将标记ID ViewHolder
转换为ViewHolder
或将视图标记强制转换为Integer
。 Look at the example above and set up the code that way to avoid this error. 查看上面的示例,并以这种方式设置代码以避免此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.