Pergunta

I have a list view with single selection mode and would like to get access the position outside the setOnItemClickListener event

As of now I can access its position as shown below

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

        }
});

Now I would like to know how do I access the selected position outside(ie..on button click event).

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>

OrElse how do I check If any listview item is checked or not on button click ?

EDIT:

This is the way I'm trying to access the position from button click

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

Solução

Simply make a member variable (declare it outside of a method like before onCreate() and use that.

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

          ...
        }
});

then use pos inside your onClick() or wherever you need to in the Activity.

If I completely missed something in your question then please explain.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top