I have an incoming call history app. Every time I call it takes the number and save it in the database, after that it adds it to the list which in turn added to the listview. But I have one problem which is it never checks for old values and it adds duplicated numbers to the database, is there any way to avoid this? Here is my try.
public class Incoming_call_history extends Activity {
private ListView lv;
private ArrayAdapter<String> adapter;
private ArrayList<String>list;
DataSourceCall DS;
boolean duplicated=false;
String temp="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_incoming_call_history);
DS= new DataSourceCall(this);
DS.open();
lv=(ListView)findViewById(R.id.callHistory);
registerForContextMenu(lv);
list=(ArrayList<String>) DS.getAllCalls();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
@SuppressWarnings("deprecation")
Cursor mCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null,
null, null);
int number = mCursor.getColumnIndex(CallLog.Calls.NUMBER);
int date = mCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = mCursor.getColumnIndex(CallLog.Calls.DURATION);
int type = mCursor.getColumnIndex(CallLog.Calls.TYPE);
while (mCursor.moveToNext()) {
String num = mCursor.getString(number);
String dur = mCursor.getString(duration);
String datee = mCursor.getString(date);
String calltype = mCursor.getString(type);
Date d = new Date(Long.valueOf(datee));
String callTypeStr = "";
switch (Integer.parseInt(calltype)) {
case CallLog.Calls.MISSED_TYPE:
callTypeStr = "Missed";
break;
case CallLog.Calls.OUTGOING_TYPE:
callTypeStr = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
callTypeStr = "Incoming";
break;
}
boolean found = false;
for (int ndx = 0; ndx < list.size(); ndx++)
{
if (num.equals(list.contains(num)))
{
found = true;
DS.deleteCall(num);
adapter.notifyDataSetChanged();
break;
}
}
if (!found)
{
DS.createCall(num);
temp=num;
Log.d("added succesfuly ", num);
}
// list.add("Phone number: "+num+"\n"+"Duration: " + dur+"\n"+"Type: " + callTypeStr+"\n"+"Call date: " + d+"\n");
}
mCursor.close();
adapter.notifyDataSetChanged();
}}
Aucun commentaire:
Enregistrer un commentaire