Android 从数据库中获取的listview中的重复值
我有一个来电历史记录应用程序。每次我打电话时,它都会将号码保存在数据库中,然后将其添加到列表中,然后列表又添加到listview中。但我有一个问题,那就是它从不检查旧值,它会向数据库中添加重复的数字,有没有办法避免这种情况?这是我的尝试Android 从数据库中获取的listview中的重复值,android,sqlite,listview,Android,Sqlite,Listview,我有一个来电历史记录应用程序。每次我打电话时,它都会将号码保存在数据库中,然后将其添加到列表中,然后列表又添加到listview中。但我有一个问题,那就是它从不检查旧值,它会向数据库中添加重复的数字,有没有办法避免这种情况?这是我的尝试 public class Incoming_call_history extends Activity { private ListView lv; private ArrayAdapter<String> adapter; private Arr
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();
}}
公共类传入呼叫\u历史扩展活动{
私有ListView lv;
专用阵列适配器;
私人审裁专家;
数据源调用DS;
布尔重复=假;
字符串temp=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u incoming\u call\u history);
DS=新数据源调用(此);
DS.open();
lv=(ListView)findViewById(R.id.callHistory);
registerForContextMenu(lv);
list=(ArrayList)DS.getAllCalls();
adapter=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1,list);
低压设置适配器(适配器);
@抑制警告(“弃用”)
游标mCursor=managedQuery(CallLog.Calls.CONTENT_URI,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(数字);
String dur=mCursor.getString(持续时间);
字符串datee=mCursor.getString(日期);
String calltype=mCursor.getString(类型);
日期d=新日期(长数值(日期E));
字符串callTypeStr=“”;
开关(Integer.parseInt(calltype)){
案例CallLog.Calls.MISSED_类型:
callTypeStr=“Missed”;
打破
案例CallLog.Calls.OUTGOING_类型:
callTypeStr=“传出”;
打破
案例CallLog.Calls.INCOMING_类型:
callTypeStr=“传入”;
打破
}
布尔值=false;
对于(int ndx=0;ndx
如果不想在表中插入已插入的手机号码,只需检查手机号码是否已插入即可
public int isUserAvailable(int number)
{
int numberCount = 0;
Cursor c = null;
try
{
c = db.rawQuery("select p_number from user_table where p_number = ?", new String[] {String.valueOf(number)});
if(c.getCount() != 0)
numberCount = c.getCount();
}
catch(Exception e) {
e.printStackTrace();
} finally {
if(c!=null) c.close();
}
return numberCount ;
}
如果不符合您的要求,请告诉我,我将尝试提供其他解决方案。您可以做的是,在将列表设置为适配器之前,您可以检查列表中的重复项并消除它们,然后将列表设置为适配器另一种方法是什么?