Selvarathinam Vinoch
Selvarathinam Vinoch

Reputation: 1100

Get text from listview on listview dismiss

I'm using custom listview and that listview have 'SwipeDismissListViewTouchListener'. When I swipe it, it should display the text as toast message. How can I achieve that? please help me. Here is my code :

 SwipeDismissListViewTouchListener touchListener =
            new SwipeDismissListViewTouchListener(
                    lv,
                    new SwipeDismissListViewTouchListener.DismissCallbacks() {
                        @Override
                        public boolean canDismiss(int position) {
                            return true;
                        }

                        @Override
                        public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                TextView text = (TextView) listView.findViewById(R.id.Details);
                                String s = text.getText().toString();
                                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
                            }
                            inAdapter.notifyDataSetChanged();
                        }
                    });
    lv.setOnTouchListener(touchListener);

Here is my custom listview xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="5dip" >

<TextView
    android:id="@+id/Category_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="18sp"
    android:typeface="normal" />

<TextView
    android:id="@+id/Details"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/Category_name"
    android:layout_marginTop="5dip"
    android:text=""
    android:textColor="#343434"
    android:textSize="12sp"
    android:singleLine="true" 
    android:layout_marginRight="50dp"/>

<TextView
    android:id="@+id/Price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text=""
    android:textColor="#343434"
    android:textSize="17sp" />

This code runs perfectly, Only when I swipe to dismiss It throws nullpointerexception.

Upvotes: 0

Views: 316

Answers (1)

Gopal Gopi
Gopal Gopi

Reputation: 11131

write your onDismiss() method like this...

@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
    if(listView != null && reverseSortedPositions != null) {
       for (int position : reverseSortedPositions) {
          View child = listView.getChildAt(position);
          if(child != null) {
              TextView text = (TextView) child.findViewById(R.id.Details);
              String s = text.getText().toString();
              Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
          }
       }
    }
   inAdapter.notifyDataSetChanged();
}

Upvotes: 1

Related Questions