【问题标题】:Get access to checked item and position outside setOnItemClickListener访问 setOnItemClickListener 之外的选中项目和位置
【发布时间】:2013-12-29 00:48:51
【问题描述】:

我有一个单选模式的列表视图,并且想访问 setOnItemClickListener 事件之外的位置

到目前为止,我可以访问它的位置,如下所示

final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position,
                long id) {

            //btnNxt.setEnabled(true);
            Intent i = new Intent(getApplicationContext(),
                    NewActivity.class);
            // Pass a single position
            i.putExtra("position", position);
            // Open SingleItemView.java Activity
            startActivity(i);

        }
});

现在我想知道如何在外部访问所选位置(即..on 按钮单击事件)。

btnNxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {               
            //Here I need to get the position and selected item
                }
});

Listview.xml

 <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:divider="#3366CC"
        android:dividerHeight="2dp" 
        android:choiceMode="singleChoice"/>

ItemDetails.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.abc.widget.CheckableRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">
    <ImageView 
        android:id="@+id/sampleimg"
        android:layout_width="150dip"
        android:layout_height="100dip"
        android:paddingRight="15dp"
        android:paddingLeft="15dp"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="10dp"
        android:descendantFocusability="blocksDescendants">
    <TextView android:id="@+id/itemname"
        android:textSize="14sp" 
        android:textStyle="bold" 
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/itemdesc"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_below="@id/carname"
         android:focusable="false"/>
    <TextView android:id="@+id/itemprice" 
        android:textSize="19sp" 
        android:textStyle="bold" 
        android:textColor="#003399" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_below="@id/cardesc"/>

   <com.example.abc.widget.InertCheckBox android:id="@+id/itemCheckBox"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_alignParentRight="true" 
        android:layout_centerVertical="true" 
        android:focusable="false" 
        android:button="@drawable/checkbox" />
</RelativeLayout>
</com.example.abc.widget.CheckableRelativeLayout>

否则,我如何检查按钮单击时是否检查了任何列表视图项?

编辑:

这是我尝试通过单击按钮访问位置的方式

btnNxt.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {              
                Toast.makeText(getBaseContext(),
                            myposition,
                            Toast.LENGTH_SHORT).show();
                }
        });

【问题讨论】:

  • 您可以使用 sparsebooleanarray 来获取我昨天展示的检查项目
  • @Rqaghunandan-正如您之前所说,我使用了 sparsebooleanarray,如我之前的解决方案中所述,但在我的情况下它给了我 null。
  • 如果你编码正确就不会
  • 其实我的不是多选,而是单选,所以我认为它在使用 sparsebooleanarray 时没用
  • 如果你认为它没用,我不能评论任何进一步的好运

标签: android listview position checked


【解决方案1】:

只需创建一个成员变量(在 onCreate() 之前的方法之外声明它并使用它。

public class ...
{
    int pos;

    // onCreate() and such



     final ListView lv1 = (ListView) findViewById(R.id.list);
     lv1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position,
                long id) {

            pos = position;   // initialize it

          ...
        }
});

然后在您的onClick() 中使用pos 或在Activity 中您需要的任何位置。

如果我完全遗漏了您问题中的某些内容,请解释一下。

【讨论】:

  • 我只是在想,如果用户不点击列表项然后点击按钮,那么 pos 将无法获得位置
  • @Raghunandan 好点!但他已将btnNext.setEnabled(true) 注释掉,所以我认为他会取消注释该行。我想我应该在答案中提到这一点。
  • @coder 当您单击列表项时,您将获得该位置。 pos 获得职位。然后你点击按钮你可以用pos做你想做的事。您是否在使用位置初始化 pos 之前单击按钮?
  • 如果myPositionint,那么它在Toast 中不起作用。查看 [文档](developer.android.com/reference/android/widget/…, int, int))。它将寻找resource id。您需要将其更改为 String
  • 在这个答案上查看我们的第一个 cmets。您可以禁用Button,然后在选择项目时启用它。你有代码,但注释掉了,所以我想你会理解的。
猜你喜欢
  • 1970-01-01
  • 2017-10-29
  • 2014-05-05
  • 2012-03-27
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多