Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在自定义适配器列表视图中获取选定项_Android_Android Listview_Android Adapter - Fatal编程技术网

Android 在自定义适配器列表视图中获取选定项

Android 在自定义适配器列表视图中获取选定项,android,android-listview,android-adapter,Android,Android Listview,Android Adapter,我的每个列表视图项都有其自己的linearlayout组件。列表视图位于活动中,其本身带有onItemClick 在我的自定义适配器文件中,我在这个linearlayout上有onclick private ArrayList<Book> bookArray; // this is the data source ......... LinearLayout imgLayout = (LinearLayout) rowView.findViewById(R.id.imageLay

我的每个列表视图项都有其自己的linearlayout组件。列表视图位于活动中,其本身带有onItemClick

在我的自定义适配器文件中,我在这个linearlayout上有onclick

private ArrayList<Book> bookArray; // this is the data source
 .........

LinearLayout imgLayout = (LinearLayout) rowView.findViewById(R.id.imageLayout);
imgLayout.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) { // fire user Like
        //*** how can I get which item is selected here?
private ArrayList bookArray;//这是数据源
.........
LinearLayout imgLayout=(LinearLayout)rowView.findviewbyd(R.id.imageLayout);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){//fire类似于用户
//***如何获取此处选择的项目?
但是如何获取单击了linearlayout的项目索引

问候 锤击

使用和记住位置,并在单击LinerLayout时使用

在getView中

LinearLayout imgLayout = (LinearLayout) rowView.findViewById(R.id.imageLayout);
imgLayout .setTag(position);
在一次线布局中,将其视为

imgLayout.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        int position = (Integer)v.getTag();
        //.... 
    }
}

您可以为线性布局设置标记,在onClick中可以获得标记,该标记将返回clickEvent获取触发器的位置

见以下代码:

private ArrayList<Book> bookArray; // this is the data source
 .........

LinearLayout imgLayout = (LinearLayout) rowView.findViewById(R.id.imageLayout);
imgLayout.setTag(position);
imgLayout.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) { // fire user Like
        //*** how can I get which item is selected here?
Log.d(TAG,"Clicked Pos of Row"+v.getTag().toString();
private ArrayList bookArray;//这是数据源
.........
LinearLayout imgLayout=(LinearLayout)rowView.findviewbyd(R.id.imageLayout);
imgLayout.setTag(位置);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){//fire类似于用户
//***如何获取此处选择的项目?
Log.d(标记,“单击行的位置”+v.getTag().toString();

使用
getView(…)
方法中的
位置
,您可以获取特定记录。检查此链接,它将帮助您@M.D.LinearLayout imgLayout=(LinearLayout)rowView.findViewById(R.id.imageLayout);…..位于getView()alredeady@Mohan,这几乎没什么帮助。请先通读我的问题。bookArray.get(position)将为您提供单击了linearlayout的项目索引