0

I am am under development of an application which allows to make the contacts in a databse, and to show them in a listview.

At present I have it:

Database :

public class Contact {

    private int CTC_ID;
    private byte[]  CTC_IMAGE;
    public String CTC_NOM;
    public String CTC_NUMERO;

    public Contact(){
    }

    public Contact(int CTC_ID, byte[] CTC_IMAGE, String CTC_NOM, String CTC_NUMERO){
        this.CTC_ID = CTC_ID;
        this.CTC_IMAGE = CTC_IMAGE;
        this.CTC_NUMERO = CTC_NOM;
        this.CTC_NUMERO = CTC_NUMERO;
    }



    public int getCTC_ID(){
        return CTC_ID;
    }
    public void setCTC_ID(int CTC_ID) {

        this.CTC_ID = CTC_ID;
    }

    public byte[] getCTC_IMAGE(){
        return CTC_IMAGE;
    }
    public void setCTC_IMAGE(byte[] CTC_IMAGE) {
        this.CTC_IMAGE = CTC_IMAGE;
    }

    public String getCTC_NOM() {

        return CTC_NOM;
    }
    public void setCTC_NOM(String CTC_NOM) {
        this.CTC_NOM = CTC_NOM;
    }

    public String getCTC_NUMERO() {

        return CTC_NUMERO;
    }
    public void setCTC_NUMERO(String CTC_NUMERO) {

        this.CTC_NUMERO = CTC_NUMERO;
    }


    public String toString(){
        return "ID : "+CTC_ID+"\nimage : "+CTC_IMAGE+"\nnom : "+CTC_NOM+"\nnumero : "+CTC_NUMERO;
    }
}

databaseHelper :

public class DatabaseHelper extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "kids_phone";

    // Table Names
    private static final String TABLE_CONTACT = "Contact";
    private static final String TABLE_MESSAGE = "Message";
    private static final String TABLE_ASSOCIER = "Associer";

    // Common column names
    private static final String KEY_CTC_ID = "CTC_ID";
    private static final String KEY_MES_ID = "MES_ID";
    private static final String KEY_ASS_ID = "ASS_ID";

    // Contact Table - column names
    public static final String KEY_CTC_IMAGE = "CTC_IMAGE";
    public static final String KEY_CTC_NOM = "CTC_NOM";
    public static final String KEY_CTC_NUMERO = "CTC_NUMERO";

    // Message Table - column names
    private static final String KEY_MES_TITRE = "MES_TITRE";
    private static final String KEY_MES_CONTENU = "MES_CONTENU";

    // Associer Table - column names
    private static final String KEY_ASS_IMG = "ASS_IMG";
    private static final String KEY_ASS_MES_ID = "ASS_MES_ID";
    private static final String KEY_ASS_CTC_ID = "ASS_CTC_ID";

    // Contact table Create Statements
    private static final String CREATE_TABLE_CONTACT = "CREATE TABLE "
            + TABLE_CONTACT + " (" + KEY_CTC_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_CTC_IMAGE
            + " BLOB," + KEY_CTC_NOM + " TEXT," + KEY_CTC_NUMERO + " TEXT NOT NULL);";

    // Message table create statement
    private static final String CREATE_TABLE_MESSAGE = "CREATE TABLE " + TABLE_MESSAGE
            + " (" + KEY_MES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_MES_TITRE + " TEXT,"
            + KEY_MES_CONTENU + " TEXT NOT NULL);";

    // Associer table create statement
    private static final String CREATE_TABLE_ASSOCIER = "CREATE TABLE "
            + TABLE_ASSOCIER + " (" + KEY_ASS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ASS_IMG + " TEXT,"
            + KEY_ASS_MES_ID + " INTEGER," + KEY_ASS_CTC_ID + " INTEGER);";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        // creating required tables
        db.execSQL(CREATE_TABLE_CONTACT);
        db.execSQL(CREATE_TABLE_MESSAGE);
        db.execSQL(CREATE_TABLE_ASSOCIER);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // on upgrade drop older tables
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGE);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ASSOCIER);

        // create new tables
        onCreate(db);
    }

    private static SQLiteDatabase bdd;
    static ArrayList<Contact> contactListe = new ArrayList<Contact>();

    public void open() {
        //on ouvre la BDD en écriture
        SQLiteDatabase bdd = this.getWritableDatabase();
    }

    public void close() {
        //on ferme l'accès à la BDD
        bdd.close();
    }


    public long insertContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();
        //Création d'un ContentValues (fonctionne comme une HashMap)
        ContentValues values = new ContentValues();
        //on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
        values.put(KEY_CTC_IMAGE, contact.getCTC_IMAGE());
        values.put(KEY_CTC_NOM, contact.getCTC_NOM());
        values.put(KEY_CTC_NUMERO, contact.getCTC_NUMERO());
        //on insère l'objet dans la BDD via le ContentValues
        long CTC_ID = db.insert(TABLE_CONTACT, null, values);

        return CTC_ID;
    }

    public Cursor fetchAllContact() throws SQLException {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor mCursor = db.query(TABLE_CONTACT, new String[]{"CTC_ID _id", KEY_CTC_IMAGE, KEY_CTC_NUMERO}, null, null, null, null, null);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public ArrayList<Contact> getListContact(){
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACT;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        ArrayList<Contact> contactList = new ArrayList<Contact>();
        if (cursor.moveToFirst()) {
            do {
                Contact list = new Contact();
                list.setCTC_ID(Integer.parseInt(cursor.getString(0)));
                list.setCTC_IMAGE(cursor.getBlob(1));
                list.setCTC_NOM(cursor.getString(2));
                list.setCTC_NUMERO(cursor.getString(3));
                contactList.add(list);
            } while (cursor.moveToNext());
        }
        return contactList;
    }
}

The ContactAdapter :

public class ContactAdapter extends BaseAdapter{

    private Context context;
    private ArrayList<Contact> contactList = new ArrayList<Contact>();

    // Constructor
    public ContactAdapter(Context context, ArrayList<Contact> contactList) {
        this.context = context;
        this.contactList.clear();
        this.contactList.addAll(contactList);
    }
    @Override
    public int getCount() {
        return contactList.size();
    }

    @Override
    public Object getItem(int position) {
        return contactList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder{
        ImageView img;
        TextView name;
    }


    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        View row = view;
        ViewHolder holder = new ViewHolder();

        if(row == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.contact_item, null);
            row.setTag(holder);
        }

        final TextView name = (TextView) row.findViewById(R.id.name);
        name.setText(contactList.get(position).getCTC_NUMERO());
        final ImageView img = (ImageView) row.findViewById(R.id.img);
        byte[] contactImage = contactList.get(position).getCTC_IMAGE();
        Bitmap bitmap = BitmapFactory.decodeByteArray(contactImage, 0, contactImage.length);
        img.setImageBitmap(bitmap);
        return row;
    }
}

And my view :

public class MainActivity extends AppCompatActivity {
    private ListView lstViewContact;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                lstViewContact = (ListView) findViewById(R.id.lvContact);

                displayContact();

}

    public void displayContact() {

        //On ouvre la base de données pour écrire dedans
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        final ContactAdapter contactAdapter = new ContactAdapter(this, dbHelper.fetchAllContactDetail());
        lstViewContact.setAdapter(contactAdapter);
    }
}

At present that shows well the contacts,

I shall thus wish that when I swipe of the left to the right that call(appeal) the number.

Thank you of the help that you go to bring me.

EDIT :

I have try this :

public class MainActivity extends Activity  implements SwipeActionAdapter.SwipeActionListener {

    public static int OVERLAY_PERMISSION_REQ_CODE = 1234;
    protected SwipeActionAdapter mAdapter;
    private ListView lstViewContact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                lstViewContact = (ListView) findViewById(R.id.lvContact);

                displayContact();
}
    public void displayContact() {

        //On ouvre la base de données pour écrire dedans
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        final ContactAdapter contactAdapter = new ContactAdapter(this, dbHelper.getListContact());
        mAdapter = new SwipeActionAdapter(contactAdapter);
        lstViewContact.setAdapter(mAdapter);
    }

    @Override
    public boolean hasActions(int position, SwipeDirection direction) {
        if (direction.isLeft()) return true;
        if (direction.isRight()) return true;
        return false;
    }

    @Override
    public boolean shouldDismiss(int position, SwipeDirection direction) {
        return direction == 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(this);
                    builder.setTitle("Test Dialog").setMessage("You swiped right").create().show();
                    dir = "Right";
                    break;
            }
            Toast.makeText(
                    this,
                    dir + " swipe Action triggered on " + mAdapter.getItem(position),
                    Toast.LENGTH_SHORT
            ).show();
            mAdapter.notifyDataSetChanged();
        }
    }
}

But i Have this error in logcat :

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: info.picse.phone_kids, PID: 24155
                                                                       java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.View$OnTouchListener.onTouch(android.view.View, android.view.MotionEvent)' on a null object reference
                                                                           at com.wdullaer.swipeactionadapter.SwipeViewGroup.onInterceptTouchEvent(SwipeViewGroup.java:174)
                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1961)
                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
                                                                           at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2394)
                                                                           at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
                                                                           at android.app.Activity.dispatchTouchEvent(Activity.java:2809)
                                                                           at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2355)
                                                                           at android.view.View.dispatchPointerEvent(View.java:8667)
                                                                           at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4255)
                                                                           at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4121)
                                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3574)
                                                                           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3627)
                                                                           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3593)
                                                                           at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3710)
                                                                           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3601)
                                                                           at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3767)
                                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3574)
                                                                           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3627)
                                                                           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3593)
                                                                           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3601)
                                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3574)
                                                                           at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5939)
                                                                           at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5913)
                                                                           at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5884)
                                                                           at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6029)
                                                                           at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                                           at android.os.MessageQueue.nativePollOnce(Native Method)
                                                                           at android.os.MessageQueue.next(MessageQueue.java:143)
                                                                           at android.os.Looper.loop(Looper.java:122)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5270)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:915)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:710)

1 Answers1

0
    Jennyfer, I think you have to first do swipe logic in your listview.

    Bellow library to make the items in a ListView dismissable with the possibility to undo it, using your own view to providing this functionality like the Gmail app for Android does.    
for swipe to call you can refer bellow link.

 https://github.com/hudomju/android-swipe-to-dismiss-undo


    after that you can add calling functionality.like that

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:0377778888"));
    startActivity(callIntent); 
Chetan Patel
  • 180
  • 8