我想让短信应用程序我有列表视图,有人谁给我发短信,但姓名是重复的数字

gev0vcfq  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(322)

我想使短信应用程序我有列表视图,有人谁给我发短信,但姓名重复我的代码是

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循环中,我将得到所有发短信给我的人的号码,如果有人多次给我发短信,他的号码将重复多次这是一个屏幕截图
我应该怎么做才能让列表视图中的每个数字都有一个
希望大家能理解

ru9i0ody

ru9i0ody1#

将while循环更改为

while (cursor.moveToNext()) {
                String x = cursor.getString(cursor.getColumnIndex("address"));
                cursor.moveToNext();
            if (!check.contains(x)){
                contacts.add(x);
            }
        }

相关问题