bauerca / drag-sort-listview

Android ListView with drag and drop reordering.
3.2k stars 1.44k forks source link

Support extra object (ImageView) #76

Closed Learning-from-masters closed 11 years ago

Learning-from-masters commented 11 years ago

I'm trying to implement an imageView down the text of every item. I have modified the method getView of DragSortListView.java and now I can click in every imageView and do things in a particular imageView but I'm having problems trying to modify the image because I need to access to the cursor. I have thougth that I can add an extra column so now the third column is going to be the route of the image but how can I change the cursor in public void onClick(View bv) so I can update the data? Is there a better way to do this?

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;  //holder to modify image
        DragSortItemView v;
        View child;

        if (convertView != null) {
            v = (DragSortItemView) convertView;
            View oldChild = v.getChildAt(0);

            child = mAdapter.getView(position, oldChild, v);
            if (child != oldChild) {
                // shouldn't get here if user is reusing convertViews properly
                v.removeViewAt(0);
                v.addView(child);
            }
            holder = (ViewHolder) v.getTag(); //set holder
        } else {
            v = new DragSortItemView(getContext());
            v.setLayoutParams(new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            child = mAdapter.getView(position, null, v);                
            v.addView(child);
            //set holder
            holder = new ViewHolder();
            holder.img = (ImageView) v.findViewById(R.id.nuevo_pasos_img);
            v.setTag(holder);
        }
        holder.img.setTag(position);
        //ImageView click
        holder.img.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View bv) {
                            //I get all the matrix with the position
                MatrixCursor b = (MatrixCursor) mAdapter.getItem((Integer) bv.getTag());
                            //I make a temp matrix
                MatrixCursor cursortemp = new MatrixCursor(new String[] {"_id", "name", "img"});
                for(b.moveToFirst(); !b.isAfterLast(); b.moveToNext()) {
                    int a = b.getInt(0);
                                //If it's not the image that I modify I copy
                    if (a!=((Integer) bv.getTag()+1)){
                        cursortemp.newRow()
                            .add(a)
                            .add(b.getString(1))
                            .add(b.getString(2));
                    }else{
                                //If it's the image I'm modifying I change the image. R.drawable only for test
                        cursortemp.newRow()
                            .add(a)
                            .add(b.getString(1))
                            .add(R.drawable.an_image);
                    }                  
                }
                            //now I need to replace the cursor to cursortemp
                String a ="yay";
            }        
        });

        // Set the correct item height given drag state; passed
        // View needs to be measured if measurement is required.
        adjustItem(position + getHeaderViewsCount(), v, true);

        return v;
    }
}

static class ViewHolder {
    ImageView img;      
}
bauerca commented 11 years ago

If the adapter you pass to dslv is a CursorAdapter, just call

((CursorAdapter) mAdapter).swapCursor(cursortemp);
Learning-from-masters commented 11 years ago

Nice!

Just for other people, in my case I changed CursorAdapter to SimpleDragSortCursorAdapter.