Skip to content

generateView int position is always 0 #14

Closed

Description

@vaggelis-net

Why the position is always 0 when generateView is called?
I want to make a listview and i hold a reference to a list with all the objects in the swipeadapter.
I have a trash imageview in the bottom view (just like in the tutorial) which when clicked it deletes that item, but i can't notify the adapter with the position since it's always 0. So the wrong item gets deleted.

Activity

  1. daimajia commented on Sep 1, 2014

    @daimajia
    Owner

    Could you please paste your code?

  2. vaggelis-net commented on Sep 2, 2014

    @vaggelis-net
    Author

    I did some more testing and i found out that if i pass a list that aready contains some items everything seem to work fine. The position is correct and the right item is deleted.
    If the list is empty and start adding later, the position will always be 0 on the new items.

    public class MyAdapter extends SwipeAdapter
    {
    
        private Context context;
        //holding the reference, i have an add button on the main activity that adds more strings in the list
        private List<String> mylist;
    
        public MyAdapter(Context context, List<String> mylist)
        {
            this.context = context;
            this.mylist = mylist;
        }
    
        @Override
        public View generateView(final int position, ViewGroup viewGroup)
        {
            View v = LayoutInflater.from(context).inflate(R.layout.list_layout, null);
            final SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
            swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
    
            swipeLayout.findViewById(R.id.myimageview).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    //The position here is always 0 for the new items
                    Toast.makeText(context, "my position : " + String.valueOf(position), Toast.LENGTH_SHORT).show();
                    //mylist.remove(position);
                    //notifyDataSetChanged();
                }
            });
    
            return v;
        }
    
       @Override
        public void fillValues(int position, View view)
        {
            TextView textView = (TextView) view.findViewById(R.id.mytextview);
            textView.setText(mylist.get(position));
        }
    
        @Override
        public int getCount()
        {
            return mylist.size();
        }
    
        @Override
        public Object getItem(int position)
        {
            return mylist.get(position);
        }
    
        @Override
        public long getItemId(int position)
        {
            return position;
        }
  3. daimajia commented on Sep 2, 2014

    @daimajia
    Owner

    Hey guy.

    You should view the source of the SwipeAdapter, and try to know the principle of the SwipeAdapter.

    remember: generateView should only do as it's name, just generate views.. do not bind any listeners or fill values.

    So, a quick way to solve this problem:

    move the following code:

            swipeLayout.findViewById(R.id.myimageview).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    //The position here is always 0 for the new items
                    Toast.makeText(context, "my position : " + String.valueOf(position), Toast.LENGTH_SHORT).show();
                    //mylist.remove(position);
                    //notifyDataSetChanged();
                }
            });

    to

       @Override
        public void fillValues(int position, View view)
        {
            TextView textView = (TextView) view.findViewById(R.id.mytextview);
            textView.setText(mylist.get(position));
        }
  4. daimajia commented on Sep 2, 2014

    @daimajia
    Owner

    Have a try, if any problem, feel free to contact me.

  5. vaggelis-net commented on Sep 2, 2014

    @vaggelis-net
    Author

    Yes, fillValues position will work since you pass the position from getView. My mistake then i though at first the position from geverateView is the same as getView too

  6. daimajia commented on Sep 2, 2014

    @daimajia
    Owner

    It works right?

    I think I should add this notice in Wiki. It's also my mistake. :-D

  7. vaggelis-net commented on Sep 2, 2014

    @vaggelis-net
    Author

    Yes i just tested it. Well i fall for it because of the View in both generateView and the usual getView

  8. daimajia commented on Sep 2, 2014

    @daimajia
    Owner

    ask a favor:

    The problem you met is really a pitfall (my mistake)... My English is not very well, I'm not good at writing, explaining. If you are available, could you please help this project to improve the wiki to prevent other developers get into this trouble again:
    Only this page:
    https://github.com/daimajia/AndroidSwipeLayout/wiki/SwipeAdapter

  9. vaggelis-net commented on Sep 2, 2014

    @vaggelis-net
    Author

    Yes i can do some editing if you want :) But to let you know English is not my native language too!

  10. daimajia commented on Sep 2, 2014

    @daimajia
    Owner

    Do your best !
    I trust you ! 😄

  11. angelen10 commented on Nov 30, 2014

    @angelen10

    Click and swipe have some comflict.I want to enter another Activity when I click an item, but also I want to swipe left to show the BottomView and handle the BottomView.My code has something wrong?
    @OverRide
    public void fillValues(int position, View convertView) {

        SwipeLayout swipeLayout = (SwipeLayout) convertView
                .findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
    
        LinearLayout linoutMemo = (LinearLayout) swipeLayout
                .findViewById(R.id.linoutMemo);
    
        linoutMemo.setOnClickListener(new OnClickListener() {...});
    
    }
    

    My layout code:

    <com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- BottomView -->
    <LinearLayout
        android:layout_width="40dp"
        android:layout_height="120dp"
        android:background="#333"
        android:gravity="center"
        android:tag="Bottom2" >
    
        <ImageView
            android:id="@+id/trash"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:src="@drawable/ic_trash" />
    </LinearLayout>
    
    <!-- SurfaceView -->
    
    <LinearLayout
        android:id="@+id/linoutMemo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical"
        android:padding="10dp" >
    
        <TextView
            android:id="@+id/txvTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is the title" />
    
        <TextView
            android:id="@+id/txvContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is the content" />
    </LinearLayout>
    

    </com.daimajia.swipe.SwipeLayout>
    How to handle this problem?
    Sorry for my poor English.

  12. vaggelis-net commented on Nov 30, 2014

    @vaggelis-net
    Author

    Try this:
    LinearLayout linoutMemo = (LinearLayout) convertview.findViewById(R.id.linoutMemo);

    instead of
    LinearLayout linoutMemo = (LinearLayout) swipeLayout.findViewById(R.id.linoutMemo);

  13. angelen10 commented on Nov 30, 2014

    @angelen10

    @de-Stan I have tried:LinearLayout linoutMemo = (LinearLayout) convertview.findViewById(R.id.linoutMemo);But result is: the item can click to enter a new Activity,but this item can not swipe again.

  14. vaggelis-net commented on Nov 30, 2014

    @vaggelis-net
    Author

    Ok, what i believe you should do is set an ItemClickListener on the listview from your activity/fragment:

    listView.setOnItemClickListener ... ;

    and start the activity from there.
    no need to do that inside the adapter's fillview. And maybe that's your problem, if i understand it correctly.

  15. angelen10 commented on Nov 30, 2014

    @angelen10

    @de-Stan OK,what you answer is handle the problem.But if you set an ItemClickListener on the listview from your Activity,the click effect is always.No matter the SwipeLayout is open, the item can click always.But I what I want is: when the SwipeLayout is open, the SurfaceView in this item can not clicked, but the BottomView in the item can click because I want do something in the BottomView.
    How to resolve this problem?

  16. vaggelis-net commented on Nov 30, 2014

    @vaggelis-net
    Author

    You can use the SwipeLayout's status. From the ItemClickListener you will get a view which is a SwipeLayout. Do something like this:

    @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
        {
            if((((SwipeLayout) view).getOpenStatus() == SwipeLayout.Status.Close))
            {
               //Start your activity
            }
  17. angelen10 commented on Nov 30, 2014

    @angelen10

    @de-Stan My problem is solved, thank you very much!

  18. vaggelis-net commented on Nov 30, 2014

    @vaggelis-net
    Author

    Glad i could help!

  19. vaggelis-net commented on Jul 8, 2015

    @vaggelis-net
    Author

    Sorry i can't understand what you asking. Some code could be helpful to see what you are up to.

  20. vaggelis-net commented on Jul 8, 2015

    @vaggelis-net
    Author

    I haven't used the librady almost a year now, but i didn't have any problems as you describe. Maybe some things change or broke with an update? Or are you doing something wrong in your side?
    Some code should give some more info, also @daimajia should notice this also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions