在我的应用程序中,我只想要具有不同的调用日志,并按日期降序排序。我尝试了下面的代码而没有得到确切的结果
我的代码如下
public class MainActivity extends Activity {
// List<CLogItem> clogitems = new ArrayList<CLogItem>();
Set clogitems = new TreeSet();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v){
getCallDetails(this);
TextView tv1 = (TextView)findViewById(R.id.tv1);
String str = "";
Iterator sitr=clogitems.iterator();
while(sitr.hasNext())
{
CLogItem clitem=(CLogItem) sitr.next();
str = str + "P: " + clitem.No + "\n";
str = str + "N: " + clitem.Name + "\n";
str = str + "--------------------------------------------\n";
}
tv1.setText(str);
}
private void getCallDetails(Context context) {
StringBuffer stringBuffer = new StringBuffer();
Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DATE + " DESC");
int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
int date = cursor.getColumnIndex(CallLog.Calls.DATE);
int iname = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
while (cursor.moveToNext()) {
CLogItem clitem = new CLogItem();
clitem.No = cursor.getString(number);
clitem.Name = cursor.getString(iname);
clitem.lDate = Long.valueOf(cursor.getString(date));
clogitems.add(clitem);
}
cursor.close();
}
public static class CLogItem implements Comparable {
String No;
String Name;
long lDate;
public CLogItem(){
this.No = "";
this.Name = "";
this.lDate = 0;
}
@Override
public int compareTo(Object arg0) {
CLogItem l = (CLogItem)arg0;
int fnResult = this.No.compareTo(l.No);
if( fnResult == 0 ){
if (lDate > l.lDate) {
return -1;
}
else if (lDate < l.lDate) {
return 1;
}
else {
return 0;
}
}
return fnResult;
}
}
}
请帮我解决这个问题
提前致谢
答案 0 :(得分:1)
最后我得到了一个正确的解决方案。
private void getCallDetails(Context context) {
StringBuffer stringBuffer = new StringBuffer();
Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.NUMBER + "," + CallLog.Calls.DATE + " DESC");
int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
int date = cursor.getColumnIndex(CallLog.Calls.DATE);
int iname = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
String curNo = "";
while (cursor.moveToNext()) {
String No = cursor.getString(number);
if( !No.equalsIgnoreCase(curNo) ){
CLogItem clitem = new CLogItem();
clitem.No = cursor.getString(number);
clitem.Name = cursor.getString(iname);
clitem.lDate = Long.valueOf(cursor.getString(date));
clogitems.add(clitem);
curNo = No;
}
}
cursor.close();
Collections.sort(clogitems);
}
答案 1 :(得分:0)
我们不能在查询时使用GROUP BY来实现这一点。因为在这种情况下,我们应该获得所有值,然后检查可能性。如果我想获得前两个不同的项目,我仍然需要获得所有的价值。所以,我认为GROUP BY会帮助..