woxblom / DragListView

Drag and drop to reorder items in a list, grid or board for Android. Based on RecyclerView. Also supports swiping items in a list.
Apache License 2.0
693 stars 177 forks source link

trying to set up drag and drop for listview #167

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi :) I'm new to android programming and using android studio (i am used to using php) What I want to do is:

so far, I have a table: "tasks" in sqlite database and I have already displayed a list of tasks as a list using ListView (as per below code). And, now from this list of tasks, I would like to be able to change the task order (by drag and drop). [note: there is an int column "order_no" in the tasks table, which will need to be updated accordingly whenever drag and drop] .

so far, all I have set up is the list of tasks as per below code, but now I would like to add drag and drop functionality. I was hoping it might not be too difficult - any tips? thanks

package com.example.apptasks;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

ListView simpleList;
    ArrayList<String> taskList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SQLiteDatabase apptasksdb = openOrCreateDatabase("apptasksdb", MODE_PRIVATE, null);

        Cursor cursor = apptasksdb.rawQuery("SELECT task_title FROM tasks", null);

        while (cursor.moveToNext()) {
            taskList.add(cursor.getString(0));
        }

        cursor.close();

        simpleList = (ListView) findViewById(R.id.simpleListView);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_list_view, R.id.textView, taskList);
        simpleList.setAdapter(arrayAdapter);

    }

    }
woxblom commented 4 years ago

Down load the repo and check out the sample app. That should give you all the help you need :)

ghost commented 4 years ago

thanks :) i have downloaded repo and the sample is working great, so now i have start point - will comment again if i get stuck on an issue