-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImportContacts.java
More file actions
145 lines (127 loc) · 5.55 KB
/
Copy pathImportContacts.java
File metadata and controls
145 lines (127 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.example.tarun.auxilium;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class ImportContacts extends AppCompatActivity
{
ArrayList<String> contact=new ArrayList<>();;
ListView myListView;
String id,name,phoneNo;
static final int RESULT_PICK_CONTACT=1;
private ArrayAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
setContentView(R.layout.activity_import_contacts);
final ContactsDatabaseHandler handler= new ContactsDatabaseHandler(this);
myAdapter = new ImportContactsCustomAdapter(this, contact);
myListView = (ListView)findViewById(R.id.listView);
myListView.setAdapter(myAdapter);
// getSharedPreferences();
Cursor cursor=handler.getListContacts();
if(cursor!=null && cursor.moveToFirst())
{ do
{
name=cursor.getString(cursor.getColumnIndex("name"));
phoneNo=cursor.getString(cursor.getColumnIndex("number"));
contact.add(name+"\n("+phoneNo+")");
myAdapter.notifyDataSetChanged();
cursor.moveToNext();
}while (!cursor.isAfterLast());
cursor.close();
}
myListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id)
{
TextView textView = (TextView)view.findViewById(R.id.myText);
final String mySourceString = textView.getText().toString();
int endIndex=mySourceString.indexOf("\n");
final String substr=mySourceString.substring(0,endIndex);
AlertDialog.Builder ab =new AlertDialog.Builder(ImportContacts.this);
ab.setMessage("Do you want to delete contact of "+substr+"?")
.setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
handler.deleteContacts(substr);
contact.remove(position);
myAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),mySourceString+"\n"+"Successfully Removed",Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert=ab.create();
alert.setTitle("DELETION CONFIRMATION");
alert.show();
return true;
}
});
ImageButton addContact=(ImageButton)findViewById(R.id.add_contacts);
addContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{ // check whether the result is ok
if (resultCode ==MainActivity.RESULT_OK)
{ // Check for the request code, we might be using multiple startActivityForReslut
switch (requestCode)
{ case RESULT_PICK_CONTACT:
Cursor cursor = null;
try
{ Uri uri = data.getData();
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idIndex=cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
id=cursor.getString(idIndex);
int nameIndex=cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
name=cursor.getString(nameIndex);
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNo = cursor.getString(phoneIndex);
storeView();
}catch (Exception e)
{ e.printStackTrace();
}
break;
}
}else
{ Log.e("MainActivity", "Failed to pick contact");
}
}
void storeView()
{ ContactsDatabaseHandler handler=new ContactsDatabaseHandler(this);
contact.add(name+"\n("+phoneNo+")");
myAdapter.notifyDataSetChanged();
handler.addContacts(id,name,phoneNo);
}
}