我想制作短信应用程序我有列表视图,其中有给我发短信但名字重复的人数
i want to make sms app I have list view that have the numbers of people who text me but the names is repeated
我想制作短信应用我有列表视图,其中包含给我发短信但姓名重复的人数
我的代码是
public class MainActivity extends AppCompatActivity {
Cursor cursor;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.listview);
ArrayList<String> contacts =new ArrayList<>();
ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,contacts);
listView.setAdapter(arrayAdapter);
cursor = getContentResolver().query(Uri.parse("content://sms"), null, null,null,null);
cursor.moveToFirst();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS}, PackageManager.PERMISSION_GRANTED);
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndex("body"));
cursor.moveToFirst();
while(cursor.moveToNext()){
contacts.add("from: "+cursor.getString(cursor.getColumnIndex("address"))
+"\n"+cursor.getString(cursor.getColumnIndex("body")));
cursor.moveToNext();
}
}
}
如果我删除了 +"\n"+cursor.getString(cursor.getColumnIndex("body")));
从 while 循环中我将得到所有给我发短信的人的号码
而且如果有人多次发给我,他的号码会重复多次
this is a screen shot
我应该怎么做才能在列表视图中显示每个号码之一
望谅解
将 while 循环更改为
while (cursor.moveToNext()) {
String x = cursor.getString(cursor.getColumnIndex("address"));
cursor.moveToNext();
if (!check.contains(x)){
contacts.add(x);
}
}
我想制作短信应用我有列表视图,其中包含给我发短信但姓名重复的人数 我的代码是
public class MainActivity extends AppCompatActivity {
Cursor cursor;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.listview);
ArrayList<String> contacts =new ArrayList<>();
ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,contacts);
listView.setAdapter(arrayAdapter);
cursor = getContentResolver().query(Uri.parse("content://sms"), null, null,null,null);
cursor.moveToFirst();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS}, PackageManager.PERMISSION_GRANTED);
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndex("body"));
cursor.moveToFirst();
while(cursor.moveToNext()){
contacts.add("from: "+cursor.getString(cursor.getColumnIndex("address"))
+"\n"+cursor.getString(cursor.getColumnIndex("body")));
cursor.moveToNext();
}
}
}
如果我删除了 +"\n"+cursor.getString(cursor.getColumnIndex("body")));
从 while 循环中我将得到所有给我发短信的人的号码
而且如果有人多次发给我,他的号码会重复多次
this is a screen shot
我应该怎么做才能在列表视图中显示每个号码之一
望谅解
将 while 循环更改为
while (cursor.moveToNext()) {
String x = cursor.getString(cursor.getColumnIndex("address"));
cursor.moveToNext();
if (!check.contains(x)){
contacts.add(x);
}
}