Android 获取ListView项布局中的视图标记
我在ListView中使用了以下布局(这意味着列表中的每个项目只使用一次):Android 获取ListView项布局中的视图标记,android,android-layout,android-intent,android-activity,android-xml,Android,Android Layout,Android Intent,Android Activity,Android Xml,我在ListView中使用了以下布局(这意味着列表中的每个项目只使用一次): 您可以使用getTag()和setTag(tagObject)方法设置和获取视图的标记。有关更多信息,请参阅 您可以将getTag()方法应用于布局中的任何视图对象。如果在XML或代码中设置了某些内容,则可以获得标记,但它是一个对象,因此需要进行类转换 e、 在您的例子中,inttextviewtag=(int)total.getTag() 编辑 这很像是一个伪代码,让您了解这个想法。这不是处理适配器的getView方
您可以使用
getTag()
和setTag(tagObject)
方法设置和获取视图的标记。有关更多信息,请参阅
您可以将getTag()
方法应用于布局中的任何视图对象。如果在XML
或代码中设置了某些内容,则可以获得标记
,但它是一个对象,因此需要进行类转换
e、 在您的例子中,inttextviewtag=(int)total.getTag()
编辑
这很像是一个伪代码,让您了解这个想法。这不是处理适配器的getView
方法的理想方法。请参阅以进一步了解列表视图
、视图支架
模式和适配器
/* Somewhere in your adapter class, define them Globally */
ImageView imageView;
TextView total;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = yourActivityInstance.getLayoutInflater().inflate(R.layout.your_list_item_xml, parent, false);
imageView = (ImageView) convertView.findViewById(R.id.image);
total = (TextView) convertView.findViewById(R.id.total);
return convertView;
}
/**
* Called when the ImageView is Clicked.
*/
public void increaseComments() {
/* There you have the Tag of your Text View */
int textViewTag = (int) total.getTag();
}
您可以使用
getTag()
和setTag(tagObject)
方法设置和获取视图的标记。有关更多信息,请参阅
您可以将getTag()
方法应用于布局中的任何视图对象。如果在XML
或代码中设置了某些内容,则可以获得标记
,但它是一个对象,因此需要进行类转换
e、 在您的例子中,inttextviewtag=(int)total.getTag()
编辑
这很像是一个伪代码,让您了解这个想法。这不是处理适配器的getView
方法的理想方法。请参阅以进一步了解列表视图
、视图支架
模式和适配器
/* Somewhere in your adapter class, define them Globally */
ImageView imageView;
TextView total;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = yourActivityInstance.getLayoutInflater().inflate(R.layout.your_list_item_xml, parent, false);
imageView = (ImageView) convertView.findViewById(R.id.image);
total = (TextView) convertView.findViewById(R.id.total);
return convertView;
}
/**
* Called when the ImageView is Clicked.
*/
public void increaseComments() {
/* There you have the Tag of your Text View */
int textViewTag = (int) total.getTag();
}
ListView本身在哪里?在提出此类问题之前,先进行谷歌搜索。您将在不到1分钟的时间内找到您的答案
(int)imageView.getTag()
@osayilgan重新阅读问题。我在问如果我在我提供的onClick方法中单击ImageView
,如何获得TextView
的标签…@AlexK我没有必要提供ListView(…
)布局。但是,列表项的布局在我的问题中给出了。@user5477909看到我的答案了吗?ListView本身在哪里?在提出此类问题之前,请进行谷歌搜索。您将在不到1分钟的时间内找到您的答案(int)imageView.getTag()
@osayilgan重新阅读问题。我在问如果我在我提供的onClick方法中单击ImageView
,如何获得TextView
的标签…@AlexK我没有必要提供ListView(…
)布局。但是,列表项的布局在我的问题中给出。@user5477909请参阅我的答案。我不确定您是否理解我的问题。查看ImageView
(increaseComments()
)的onClick方法。在该方法中,如何在布局中获取TextView
的标记?@user5477909当您在适配器类的getView()
方法中膨胀项目布局时,您需要保留对TextView
的引用,然后您可以访问它的标记。我不确定您是否理解我的问题。查看ImageView
(increaseComments()
)的onClick方法。在该方法中,如何在布局中获取TextView
的标记?@user5477909当您在适配器类的getView()
方法中膨胀项目布局时,您需要保留对TextView
的引用,然后才能访问它的标记。
/* Somewhere in your adapter class, define them Globally */
ImageView imageView;
TextView total;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = yourActivityInstance.getLayoutInflater().inflate(R.layout.your_list_item_xml, parent, false);
imageView = (ImageView) convertView.findViewById(R.id.image);
total = (TextView) convertView.findViewById(R.id.total);
return convertView;
}
/**
* Called when the ImageView is Clicked.
*/
public void increaseComments() {
/* There you have the Tag of your Text View */
int textViewTag = (int) total.getTag();
}