wdullaer / SwipeActionAdapter

The Mailbox-like Swipe gesture library for Android
Apache License 2.0
213 stars 61 forks source link

error when i try to filter my adapter #58

Closed SmoothCoffe closed 6 years ago

SmoothCoffe commented 6 years ago

this is my code


package com.my.package;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FilterQueryProvider;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.wdullaer.swipeactionadapter.SwipeActionAdapter;
import com.wdullaer.swipeactionadapter.SwipeDirection;

public class MainActivity extends AppCompatActivity {
    private BottomSheetDialog bottomSheetDialog;
    private SimpleCursorAdapter mycursoradapter;
    private ListView mylist;
    private DataBaseAdapter myDB;
    SwipeActionAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomSheetDialog = new BottomSheetDialog(this);
        View bottomsheetview = getLayoutInflater().inflate(R.layout.addbottomsheet, null);
        bottomSheetDialog.setContentView(bottomsheetview);
        LinearLayout addnewbtn = (LinearLayout) bottomSheetDialog.findViewById(R.id.addnewbtn);
        LinearLayout addexistbtn = (LinearLayout) bottomSheetDialog.findViewById(R.id.addexistbtn);

        addnewbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, AddNewContact.class));
                bottomSheetDialog.dismiss();
            }
        });
        addexistbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowToast("add exist");

            }
        });

        openDB();
        populateListView();

        mAdapter.setSwipeActionListener(new SwipeActionAdapter.SwipeActionListener() {
            @Override
            public boolean hasActions(int i, SwipeDirection swipeDirection) {
                if (swipeDirection.isLeft())
                    return true; // Change this to false to disable left swipes
                if (swipeDirection.isRight()) return true;
                return false;
            }

            @Override
            public boolean shouldDismiss(int i, SwipeDirection swipeDirection) {
                // Only dismiss an item when swiping normal left
                return false; //swipeDirection == swipeDirection.DIRECTION_NORMAL_LEFT;
            }

            @Override
            public void onSwipe(int[] positionList, SwipeDirection[] directionList) {
                for (int i = 0; i < positionList.length; i++) {
                    SwipeDirection direction = directionList[i];
                    int position = positionList[i];
                    String dir = "";

                    switch (direction) {
                        case DIRECTION_FAR_LEFT:
                            dir = "Far left";
                            break;
                        case DIRECTION_NORMAL_LEFT:
                            dir = "Left";
                            break;
                        case DIRECTION_FAR_RIGHT:
                            dir = "Far right";
                            break;
                        case DIRECTION_NORMAL_RIGHT:
                            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                            builder.setTitle("Test Dialog").setMessage("You swiped right").create().show();
                            dir = "Right";
                            break;
                    }

                    Toast.makeText(getApplicationContext(), dir + " swipe Action triggered on " + mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_bar, menu);

        SearchView searchView = (SearchView) menu.findItem(R.id.app_bar_search).getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                mylist = (ListView) findViewById(R.id.ListConfessioner);
                SimpleCursorAdapter filterAdapter = (SimpleCursorAdapter) mylist.getAdapter();
                filterAdapter.getFilter().filter(newText.toString());
                SwipeActionAdapter mAdapter = new SwipeActionAdapter(filterAdapter);
                mAdapter.setListView(mylist);
                return false;
            }
        });
        mycursoradapter.setFilterQueryProvider(new FilterQueryProvider() {
            @Override
            public Cursor runQuery(CharSequence constraint) {

                return myDB.searchname(constraint);
            }
        });
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//handle presses on the actionbar items
        switch (item.getItemId()) {
            case R.id.app_bar_search:

                return true;
            case R.id.btnadd_contact:

                bottomSheetDialog.show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void ShowToast(String Text) {
        Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
    }

    private void openDB() {
        myDB = new DataBaseAdapter(this);
        myDB.open();
    }

    public void populateListView() {
        Cursor cursor = myDB.searchname(null);
        String[] fromFieldNames = new String[]{myDB.KEY_ROWID, myDB.KEY_NAME, myDB.KEY_DATE};
        int[] toviewIDs = new int[]{R.id.textViewpnumber, R.id.textViewpname, R.id.textViewdate};
        mycursoradapter = new SimpleCursorAdapter(this, R.layout.item_layout, cursor, fromFieldNames, toviewIDs, 0);
        mylist = (ListView) findViewById(R.id.ListConfessioner);
        mAdapter = new SwipeActionAdapter(mycursoradapter);
        mAdapter.setListView(mylist);
        mylist.setAdapter(mAdapter);
        mAdapter.addBackground(SwipeDirection.DIRECTION_NORMAL_LEFT, R.layout.row_bg_left);
        mAdapter.addBackground(SwipeDirection.DIRECTION_NORMAL_RIGHT, R.layout.row_bg_right);
        mylist.setFastScrollEnabled(true);
        mylist.setTextFilterEnabled(true);
    }

}
``
this is the error
java.lang.ClassCastException: com.wdullaer.swipeactionadapter.SwipeActionAdapter cannot be cast to android.support.v4.widget.SimpleCursorAdapter

how can i solve it
i want to filter my listview and keep the swipe option after filtering 
wdullaer commented 6 years ago

You need to keep a reference to the Adapter you are wrapping with the SwipeActionAdapter and use that one to manipulate the data source.

SmoothCoffe commented 6 years ago

SwipeActionAdapter mAdapter = new SwipeActionAdapter(filterAdapter); mAdapter.setListView(mylist); isn't filterAdapter is the reference ?