I want to find Duplicate Images from External Storage.
In my current solution I successfully find Duplicates.
But when the data is large, the processing time long.
My current design first gets all images, then loops to find duplicates.
Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AllImages_1 = getImagesList(); // AllImages_1 is a array list
}
private ArrayList<File> getImagesList() {
Uri uri;
Cursor cursor;
int column_index_data;
ArrayList<File> listOfAllImages = new ArrayList<>();
String absolutePathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
cursor = MainActivity.this.getContentResolver().query(uri, projection, null,
null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(new File(absolutePathOfImage));
}
return listOfAllImages;
}
below code is find Dulpicate
public void CheckedDuplicateImage() {
int Group = 1;
arrayList.clear();
ArrayList<String> grpind = new ArrayList<>();
for (int i = 0; i < AllImages_1.size(); i++) {
ArrayList<VideoDataClass> group = new ArrayList<>();
duplicate = false;
boolean duplicate_I = false;
if (grpind.contains(String.valueOf(i))) {
continue;
}
for (int j = 0; j < AllImages_1.size(); j++) {
if (i != j && (AllImages_1.get(i).length() == AllImages_1.get(j).length() ||
AllImages_1.get(i).getName() == AllImages_1.get(j).getName())) {
if (!duplicate_I) {
group.add(OriginalFile(AllImages_1.get(i)));
duplicate_I = true;
}
duplicate = true;
group.add(DuplicateFile(AllImages_1.get(j)));
grpind.add(String.valueOf(j));
}
}
if (duplicate) {
Duplicate=true;
String Header = "Group" + Group;
modelClass = new ModelClass(group, Header);
arrayList.add(modelClass);
Group++;
}
}
}
My question: **is there an alternative solution that will achieve the same result in less time?**
Related
This question already has answers here:
The application may be doing too much work on its main thread
(21 answers)
Closed 1 year ago.
I have a block of code to load a table view. But for large data, my app is crashing. I want to fix that and also want to show a progress bar while loading the table view data. I tried with the AsyncTask but it is showing Skipped 98 frames! The application may be doing too much work on its main thread.
My onCreate code (Updated): -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sheet);
myDb = new DBHelper(this);
loadIntentInputs();
setToolbar();
new MyWorker(this).execute();
}
My AsyncTask code (Updated): -
public class MyWorker extends AsyncTask < String , Context , Void > {
private Context context ;
private ProgressDialog progressDialog;
public MyWorker (Context context) {
this.context = context ;
progressDialog = new ProgressDialog ( context ) ;
progressDialog.setCancelable ( false ) ;
progressDialog.setMessage ( "Retrieving data..." ) ;
progressDialog.setTitle ( "Please wait" ) ;
progressDialog.setIndeterminate ( true ) ;
}
# Override
protected void onPreExecute ( ) {
progressDialog.show () ;
}
# Override
protected Void doInBackground ( String ... params ) {
runOnUiThread(new Runnable() {
#Override
public void run() {
showAttendanceTable();
}
});
return null ;
}
# Override
protected void onPostExecute ( Void result ) {
if(progressDialog != null && progressDialog.isShowing()){
progressDialog.dismiss ( ) ;
}
}
}
showAttendanceTable method code (Updated): -
private void showAttendanceTable() {
TableLayout tableLayout = findViewById(R.id.table_layout);
int day_in_month = getDayInMonth(month_year);
int row_size = sid_array.length+1;
TableRow[] rows = new TableRow[row_size];
TextView[] rolls_tv = new TextView[row_size];
TextView[] names_tv = new TextView[row_size];
TextView[][] status_tv = new TextView[row_size][day_in_month+1];
for (int i = 0; i < row_size; i++) {
rolls_tv[i] = new TextView(this);
names_tv[i] = new TextView(this);
for (int j = 1; j <= day_in_month; j++) {
status_tv[i][j] = new TextView(this);
}
}
// for excel file
HSSFSheet hssfSheet = null;
try {
hssfSheet = hssfWorkbook.createSheet(month_year);
} catch (IllegalArgumentException e){
e.printStackTrace();
}
HSSFRow hssfRow;
// setting 1st row
rolls_tv[0].setText("Roll");
names_tv[0].setText("Name");
// setting excel file
hssfRow = hssfSheet.createRow(0);
hssfRow.createCell(0).setCellValue("Roll");
hssfRow.createCell(1).setCellValue("Name");
rolls_tv[0].setTextSize(1,22);
names_tv[0].setTextSize(1,22);
rolls_tv[0].setTypeface(rolls_tv[0].getTypeface(), Typeface.BOLD);
names_tv[0].setTypeface(names_tv[0].getTypeface(), Typeface.BOLD);
rolls_tv[0].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
names_tv[0].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
rolls_tv[0].setTextColor(Color.parseColor("#FF000000"));
names_tv[0].setTextColor(Color.parseColor("#FF000000"));
for (int i = 1; i <=day_in_month ; i++) {
String date = String.valueOf(i);
if(i<10) date = "0"+date;
status_tv[0][i].setText(date);
// setting excel file
hssfRow.createCell(i+1).setCellValue(date);
status_tv[0][i].setTextSize(1,22);
status_tv[0][i].setTypeface(status_tv[0][i].getTypeface(), Typeface.BOLD);
status_tv[0][i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
status_tv[0][i].setTextColor(Color.parseColor("#FF000000"));
}
// setting rows after 1st row
for (int i = 1; i < row_size ; i++) {
rolls_tv[i].setText(roll_array[i-1]);
names_tv[i].setText(name_array[i-1]);
// setting excel file
hssfRow = hssfSheet.createRow(i);
hssfRow.createCell(0).setCellValue(roll_array[i-1]);
hssfRow.createCell(1).setCellValue(name_array[i-1]);
rolls_tv[i].setTextSize(1,20);
names_tv[i].setTextSize(1,20);
rolls_tv[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
names_tv[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
rolls_tv[i].setTextColor(Color.parseColor("#FF000000"));
names_tv[i].setTextColor(Color.parseColor("#FF000000"));
int count=0;
for (int j = 1; j <=day_in_month ; j++) {
String day = String.valueOf(j);
String month = month_year.substring(0,3);
String year = month_year.substring(4,8);
String status = myDb.getStatus(sid_array[i-1],day+" "+month+" "+year);
if (status!=null && status.equals("A")) {
status_tv[i][j].setText(status);
status_tv[i][j].setBackgroundColor(Color.parseColor("#EF9A9A"));
// setting excel file
hssfRow.createCell(j+1).setCellValue(status);
} else if (status!=null && status.equals("P")) {
status_tv[i][j].setText(String.valueOf(++count));
status_tv[i][j].setBackgroundColor(Color.parseColor("#A5D6A7"));
// setting excel file
hssfRow.createCell(j+1).setCellValue(String.valueOf(count));
}
status_tv[i][j].setTextSize(1,20);
status_tv[i][j].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
status_tv[i][j].setTextColor(Color.parseColor("#FF000000"));
}
}
for (int i = 0; i < row_size; i++) {
rows[i] = new TableRow(this);
rolls_tv[i].setPadding(20,12,20,12);
names_tv[i].setPadding(20,12,20,12);
if (i==0) {
rows[i].setBackgroundColor(Color.parseColor("#FFD400"));
} else if(i%2==0) {
rows[i].setBackgroundColor(Color.parseColor("#EEEEEE"));
} else {
rows[i].setBackgroundColor(Color.parseColor("#E4E4E4"));
}
rows[i].addView(rolls_tv[i]);
rows[i].addView(names_tv[i]);
for (int j = 1; j <=day_in_month ; j++) {
status_tv[i][j].setPadding(20,12,20,12);
rows[i].addView(status_tv[i][j]);
}
tableLayout.addView(rows[i]);
}
tableLayout.setShowDividers(TableLayout.SHOW_DIVIDER_MIDDLE);
}
The problem is you are running new thread on your doInBackground method and you have placed your showAttendanceTable() code inside runOnUiThread() which is causing too many work to consume main thread.Just copy paste the below code, replace it with your doInBackground() method and everything will work fine.
protected Void doInBackground ( String ... params ) {
showAttendanceTable();
}
return null ;
}
Copy this much part of your code from showAttendanceTable() to onPreExecute() method:
TableLayout tableLayout = findViewById(R.id.table_layout);
int day_in_month = getDayInMonth(month_year);
int row_size = sid_array.length+1;
TableRow[] rows = new TableRow[row_size];
TextView[] rolls_tv = new TextView[row_size];
TextView[] names_tv = new TextView[row_size];
TextView[][] status_tv = new TextView[row_size][day_in_month+1];
for (int i = 0; i < row_size; i++) {
rolls_tv[i] = new TextView(this);
names_tv[i] = new TextView(this);
for (int j = 1; j <= day_in_month; j++) {
status_tv[i][j] = new TextView(this);
}
}
Crash is here......
android.database.sqlite.SQLiteException: near "GROUP": syntax error (Sqlite code 1 SQLITE_ERROR): , while compiling: SELECT _id, bucket_display_name, bucket_id, _id, orientation FROM images WHERE ((is_pending=0) AND (is_trashed=0) AND (volume_name IN ( 'external_primary' ))) AND ((1) GROUP BY 1,(2)) ORDER BY date_modified DESC, (OS error - 2:No such file or directory)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:184)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:423)
at android.content.ContentResolver.query(ContentResolver.java:955)
at android.content.ContentResolver.query(ContentResolver.java:891)
at android.content.ContentResolver.query(ContentResolver.java:840)
Below is method to show all folders or album of images.
private boolean logGalleryFolders() {
this.albumList = new ArrayList<>();
List<Integer> bucketIdList = new ArrayList<>();
Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, "1) GROUP BY 1,(2", null, "date_modified DESC");
List<GridViewItem> items;
int i;
if (cur == null || !cur.moveToFirst()) {
items = new ArrayList<>();
for (i = 0; i < this.albumList.size(); i++) {
items.add(new GridViewItem(this.activity, this.albumList.get(i).name, BuildConfig.FLAVOR + this.albumList.get(i).imageIdList.size(), true, this.albumList.get(i).imageIdForThumb, this.albumList.get(i).orientationList.get(0).intValue()));
}
this.albumList.add(new Album());
this.albumList.get(this.albumList.size() - 1).gridItems = items;
for (i = 0; i < this.albumList.size() - 1; i++) {
this.albumList.get(i).gridItems = createGridItemsOnClick(i);
}
if (cur != null) {
cur.close();
}
return true;
}
int bucketColumn = cur.getColumnIndex("bucket_display_name");
int bucketId = cur.getColumnIndex("bucket_id");
int imageId = cur.getColumnIndex("_id");
int orientationColumnIndex = cur.getColumnIndex("orientation");
do {
Album album = new Album();
int id = cur.getInt(bucketId);
album.ID = id;
if (bucketIdList.contains(id)) {
Album albumFromList = this.albumList.get(bucketIdList.indexOf(album.ID));
albumFromList.imageIdList.add(cur.getLong(imageId));
albumFromList.orientationList.add(cur.getInt(orientationColumnIndex));
} else {
String bucket = cur.getString(bucketColumn);
bucketIdList.add(id);
album.name = bucket;
album.imageIdForThumb = cur.getLong(imageId);
album.imageIdList.add(album.imageIdForThumb);
this.albumList.add(album);
album.orientationList.add(cur.getInt(orientationColumnIndex));
}
} while (cur.moveToNext());
items = new ArrayList<>();
for (i = 0; i < this.albumList.size(); i++) {
items.add(new GridViewItem(this.activity, this.albumList.get(i).name, BuildConfig.FLAVOR + this.albumList.get(i).imageIdList.size(), true, this.albumList.get(i).imageIdForThumb, this.albumList.get(i).orientationList.get(0).intValue()));
}
this.albumList.add(new Album());
this.albumList.get(this.albumList.size() - 1).gridItems = items;
for (i = 0; i < this.albumList.size() - 1; i++) {
this.albumList.get(i).gridItems = createGridItemsOnClick(i);
}
cur.close();
return true;
}
Below is crash line......
Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, "1) GROUP BY 1,(2", null, "date_modified DESC");
Just change the query like below:
From
Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, "1) GROUP BY 1,(2", null, "date_modified DESC");
To
Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, null, null, "date_modified DESC");
Your GROUP BY comes inside the And makes the issue. Please change your query as below:
SELECT
_id,
bucket_display_name,
bucket_id,
_id,
orientation
FROM
images
WHERE
(
(is_pending = 0)
AND (is_trashed = 0)
AND (volume_name IN ('external_primary'))
)
AND ((1))
GROUP BY
1,(2)
ORDER BY
date_modified DESC
I have an arraylist in which I have ESSID, BSSID, Strenght of access Point on first three indexes, and from Index 4 to 6 I have again ESSID, BSSID, Strength of another AccessPoint. I want to store this list in database like first three values save in one row of table. and next three values save in 2nd row of table.
String[] namesArr = new String[arrayList2.size()]; //conver arraylist to array
for (int j = 0; j < arrayList2.size(); j++){
namesArr[j] = arrayList2.get(j);
int length = namesArr[j].length();
for (int k = 0; k < length; k += 3) {
ssid = namesArr[k];
bssid = namesArr[k + 1];
rssid = namesArr[k + 2];
}
insertValues(this);
}
public void insertValues(View.OnClickListener view){
SendData send = new SendData(this);
send.execute(bssid,ssid,rssid);}
I have made a class to store this data in database that works fine.
public class SendData extends AsyncTask<String, Void, String> {
AlertDialog dialog;
Context context;
public SendData(Context context) {
this.context = context;
}
#Override
protected void onPreExecute() {
dialog = new AlertDialog.Builder(context).create();
dialog.setTitle("Message");
}
#Override
protected void onPostExecute(String s) {
dialog.setMessage(s);
dialog.show();
}
#Override
protected String doInBackground(String... voids) {
String data = "";
String result = "";
String MAC = voids[0];
String Name = voids[1];
String Strength = voids[2];
String con_Str = "http://10.5.48.129/Webapi/accesspoints_data/create.php";
try{
URL url = new URL(con_Str);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
OutputStream out_Stream = http.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out_Stream, "UTF-8"));
JSONObject obj = new JSONObject();
try {
obj.put("BSSID", MAC);
obj.put("ESSID", Name);
obj.put("RSSID", Strength);
} catch (JSONException e) {
e.printStackTrace();
}
data = obj.toString();
writer.write(data);
writer.flush();
writer.close();
out_Stream.close();
InputStream in_Stream = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in_Stream, "ISO-8859-1"));
String line = "";
while ((line = reader.readLine()) != null)
{
result += line;
}
reader.close();
in_Stream.close();
http.disconnect();
return result;
} catch (MalformedURLException e){
result = e.getMessage();
} catch (IOException e){
result = e.getMessage();
}
return result;
}
}
SendData class is perfectly working but problem is with for loop.
I think this is result that you are expecting :
List<String> arrayList2 = new ArrayList<>();
arrayList2.add("1");
arrayList2.add("2");
arrayList2.add("3");
arrayList2.add("4");
arrayList2.add("5");
arrayList2.add("6");
arrayList2.add("6");
arrayList2.add("7");
arrayList2.add("8");
arrayList2.add("9");
arrayList2.add("10");
List<String[]> sarrayList = new ArrayList<>();
String[] arr = new String[3];
int i = 0;
for (int j = 0; j < arrayList2.size(); j++)
{
arr[i] = arrayList2.get(j);
i++;
if((j+1)%3==0)
{
sarrayList.add(arr);
i = 0;
arr = new String[3];
}
}
for(String [] sa:sarrayList)
{
for(String s:sa)
{
System.out.println(s);
}
System.out.println("=========");
}
This might not be the most efficient way of doing it. But it splits the ArrayList in to String arrays of length=3 and stores them in a new ArrayList named sarrayList
I would advise to use a datastructure to hold the record. See the code below this is a small example how you could do it
ArrayList<Record> records;
for (int i = 2; i < inputArrayList.size(); i = i + 3){
string ssid = namesArr.get(i - 2);
string bssid = namesArr.get(i - 1);
string rssid = namesArr.get(i);
records.add(new Record(ssid, bssid, rssid));
}
class Record{
string ssid;
string bssid;
string rssid;
// Constructor...
// Getter and setter to be implemented...
}
ok from what i understand you want to divide the arraylist each 3 elements thats how you do it with streams and it will return an a collection of arraylists each one has 3 elements
final int chunkSize = 3;
final AtomicInteger counter = new AtomicInteger();
//arrayList here us your array list
final Collection<List<String>> result = arrayList.stream()
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / chunkSize))
.values();
and mentioning supermar10 answer you code make a class to map the strings to it like that
class Record{
string ssid;
string bssid;
string rssid;
Record(String ssid,String bssid,String rssid){
this.ssid=ssid;
this.bssid=bssid;
this.rssid=rssid;
}
}
now you have a class to map to now save the records in a list of Record
create a a list in the main class
static List<Record> lists=new ArrayList<>();
then map the data like that
result.stream().forEach(nowList -> saveRecord(nowList));
and thats the save method
static void saveRecord(List<String> list){
lists.add(new Record(list.get(0),list.get(1),list.get(2)));
}
I have simplified it to one loop and also modified insertValues so that it takes 3 more parameters. This
int size = arrayList2.size();
for (int j = 0; j < size; j += 3) {
if (size - j < 3 ) {
break;
}
String ssid = arrayList2.get(j);
String bssid = arrayList2.get(j + 1);
String rssid = arrayList2.get(j + 2);
insertValues(this, ssid, bssid, rssid);
}
if one the other hand ssid and so on are class variables the inside of the loop can be changed to
ssid = arrayList2.get(j);
bssid = arrayList2.get(j + 1);
rssid = arrayList2.get(j + 2);
insertValues();
I am working on a driving licence project on j2Me wich is including Tests like quizz , well and i am having a problem after parsing the questions and moving them into choiceGroups just like that :
if (questions.length > 0) {
for (int i = 0; i < questions.length; i++) {
ChoiceGroup reponses = new ChoiceGroup("Reponses" + i, Choice.EXCLUSIVE);
reponses.append(questions[i].getReponse1(), null);
reponses.append(questions[i].getReponse2(), null);
reponses.append(questions[i].getReponse3(), null);
pass.append(questions[i].getContenu());
pass.append(reponses);
}
}
} catch (Exception e) {
System.out.println("Exception:" + e.toString());
}
disp.setCurrent(pass);
and the next step is the command who's controlling the choiceGroups to test them if they are like the true answer or not .
so i am blocked here .
if (c == valider) {
int result = 0;
for (int i = 0; i < pass.size(); i++) {
String ch = pass.get(i).getLabel();
System.out.println(ch);
}
}
I don't know how to get the choice from the choicegroup
any help
Actually, I am not sure what totally you want for:
This code will help you get selected items from choicegroup that i did long time before:
//get a selected array in choicegroup
private String[] choiceGroupSelected(ChoiceGroup cg) {
String selectedArray[] = new String[cg.size()];
int k = 0;
for (int i = 0; i < cg.size(); i++) {
if (cg.isSelected(i)) {
selectedArray[k] = cg.getString(i);
k++;
}
}
return selectedArray;
}
That function will help me get all selected items for deleting action below:
private void deleteSpecificItem() {
try {
String temp = null;
int index;
//get ChoiceGroup size
int numbers = cgTrip.size();
String selectedItems[] = choiceGroupSelected(cgTrip);
//
rs = services.RecordStoreManager.openRecordStoreByName("TripRS");
re = rs.enumerateRecords(null, null, true);
String[] tripList = new String[2];
for (int i = 0; i < numbers; i++) {
temp = selectedItems[i];
if (temp != null) {
while (re.hasNextElement()) {
try {
index = re.nextRecordId();
System.out.println("RecordID: " + index);
byte[] byteBuff = rs.getRecord(index);
String source = new String(byteBuff);
tripList = services.StringManager.getItems(source, ";", 2);
String strProcess = tripList[0] + "-" + tripList[1];
//inspect all of items in choicegroup and if they are selecting then compare with record
//If comparison is true then delete this record
if (temp.equals(strProcess)) {
System.out.println("Delete RecordID: " + index);
rs.deleteRecord(index);
re.keepUpdated(true);
break;
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
}
}
try {
rs.closeRecordStore();
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
rs = null;
re.destroy();
this.LoadTripItem();
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
}
Actually I am using call logs as an input to database and then I am fetching it in a way that I can't get any duplicate values while displaying it and if i have any duplicate value in data base then it should be taken as integer value count. For example: john(6).
Here john must have entry 6 times in database. Don't get me wrong. I don't need a code.I need help. Here is code:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(android.provider.CallLog.Calls.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String rawContactId = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls._ID));
Cursor callLogCursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, new String[]{
android.provider.CallLog.Calls.CACHED_NAME,
android.provider.CallLog.Calls.CACHED_NUMBER_LABEL,
android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.DATE,
android.provider.CallLog.Calls.DURATION,
android.provider.CallLog.Calls.TYPE
},android.provider.CallLog.Calls._ID + "=?", new String[] { rawContactId }, null);;
if (callLogCursor != null) {
while (callLogCursor.moveToNext()) {
//String id = callLogCursor.getString(0);
String name = callLogCursor.getString(0);
String cacheNumber = callLogCursor.getString(1);
String number = callLogCursor.getString(2);
long dateTimeMillis = callLogCursor.getLong(3);
long durationMillis = callLogCursor.getLong(4);
int callType = callLogCursor.getInt(5);
String duration = getDuration(durationMillis * 1000);
String dateString = getDateTime(dateTimeMillis);
if (cacheNumber == null)
cacheNumber = number;
if (name == null)
name = "Unknown";
Uri image = null;
try {
String conId = fetchContactIdFromPhoneNumber(cacheNumber);
long contId = Long.parseLong(conId);
image = getPhotoUri(contId);
}catch(Exception e) {
Log.e("Exception", e.getMessage());
}
//CallLogModel callLogModel = new CallLogModel(image, name, cacheNumber,
// duration, dateString);
ContentValues values = new ContentValues();
values.put(NAME, name);
values.put(NUMBER, cacheNumber);
values.put(DATE, dateString);
values.put(DURATION,duration );
database.insert(CALL_LOG_TABLE, null, values);
Cursor cursor = database.query(CALL_LOG_TABLE, new String [] {LOG_ID, NAME, NUMBER, DATE, DURATION}, null, null, null, null, null);
int row =0;
if(!cursor.isAfterLast()) {
cursor.moveToFirst();
do{
int pId=cursor.getInt(0);
String pName = cursor.getString(1);
String pNumber = cursor.getString(2);
String pDate = cursor.getString(3);
String pDuration = cursor.getString(4);
int value = 0;
CallLogModel callLogModel = new CallLogModel(image, name, cacheNumber, duration, dateString);
if (callType == CallLog.Calls.OUTGOING_TYPE) {
for(int i=0;i<outgoingList.size();i++){
------------------------------Actually i want Logic here what should i do here--------------
}
}
outgoingList.add(callLogModel);
} else if (callType == CallLog.Calls.INCOMING_TYPE) {
incomingList.add(callLogModel);
} else if (callType == CallLog.Calls.MISSED_TYPE) {
missedcallList.add(callLogModel);
}
cursor.moveToNext();
} while (!cursor.isAfterLast());
}
}
callLogCursor.close();
}
}
You could model the outgoing calls in a hashmap, something like:
Map<String, Integer> outgoingCallsMap = new HashMap<String, Integer>();
for (int i = 0; i < outgoingList.size(); i++) {
String nameOfCallee = outgoingList.get(i);
if (!outgoingCallsMap.containsKey(nameOfCallee)) {
outgoingCallsMap.put(nameOfCallee, 1);
} else {
//Increment calls to this person
outgoingCallsMap.put(nameOfCallee, outgoingCallsMap.get(nameOfCallee) + 1);
}
}
Remove the duplicates in your outGoingList, by iterating it and putting the result to a map, with contact name as key and list of CallLogModel object as value.
You can refer this method.
private void convertToOutGoingMap(List<CallLogModel > outGoingList) {
HashMap<String,List<CallLogModel>> outGoingMap = new HashMap<String, List<CallLogModel>>();//map which has CallLogModel.name as key and List<CallLogModel> as value.
for(CallLogModel model : outGoingList){//Iterate thru the list.
if(outGoingMap.containsKey(model.name))
{
outGoingMap.get(model.name).add(model);//if map contains key, add model to the list.
} else {
List<CallLogModel> modelList = new ArrayList<CallLogModel>();//if it does not contains, initialize a list and add model to it.
modelList.add(model);
outGoingMap.put(model.name, modelList);
}
}
}
}
The key set of this map gives you the unique call log names and corresponding value list gives all occurrences and its size gives you number of occurrences.
Hope this help you.