从Listview中删除由Cursor填充的重复项

时间:2017-12-20 07:03:55

标签: android sqlite

我有一个由sqlite查询填充的Listview。其中一列有重复的条目(信用),我想删除该列中的所有重复项。我尝试使用“distinct”方法在sqlite查询中执行此操作,但它不起作用。

这是代码

vehicles:[{
  key1:{ name: "Altima",type:"4-wheeler" },
  key2:{ name: "Harley",type:"2-wheeler" },
  key3:{ name: "Jeep",type:"4-wheeler" },
}]

也用于将查询更改为

String query = "SELECT * FROM items " +
            "WHERE type = 'month' OR type = 'income' OR type = 'regular' OR type = 'credit' AND" +
            " month = " + monthOfYear + "  ORDER BY day ASC, _id ASC";
    Cursor cursor = db.rawQuery(query, null);

    list = (ListView) findViewById(R.id.list);
ItemsCursorAdapter adapter = new ItemsCursorAdapter(
            this, R.layout.row_layout, cursor, 0);
    list.setAdapter(adapter);

2 个答案:

答案 0 :(得分:0)

正如我在上面的评论中提到的,您可以将查询更改为:

String query = "SELECT * FROM items " +
            "WHERE type = 'month' OR type = 'income' OR type = 'regular' OR type = 'credit' AND" +
            " month = " + monthOfYear + " GROUP by _id ORDER BY day ASC, _id ASC";

在上面的查询中,您可以看到我已使用_id列对条目进行分组,效果很好。

答案 1 :(得分:0)

试试这个

我有同样的问题,你现在面对的是,我已经按照以下方式解决了这个问题。

MainActivity.java文件的代码。

public class MainActivity extends AppCompatActivity {

    ListView listview;
    List<String> subject_list;
    ArrayAdapter<String> arrayadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView)findViewById(R.id.listView1);

        subject_list = new ArrayList<String>();

        subject_list.add("ONE");
        subject_list.add("TWO");
        subject_list.add("THREE");
        subject_list.add("FOUR");
        subject_list.add("FIVE");
        subject_list.add("SIX");
        subject_list.add("SEVEN");
        subject_list.add("EIGHT");
        subject_list.add("NINE");
        subject_list.add("TEN");
        subject_list.add("THREE");
        subject_list.add("FOUR");
        subject_list.add("FIVE");
        subject_list.add("THREE");
        subject_list.add("FOUR");
        subject_list.add("FIVE");
        subject_list.add("THREE");
        subject_list.add("FOUR");
        subject_list.add("FIVE");

        //Deleting Same entries

        HashSet<String> hashSet = new HashSet<String>();
        hashSet.addAll(subject_list);
        subject_list.clear();
        subject_list.addAll(hashSet);

        //Alphabetic sorting.
        Collections.sort(subject_list);

        arrayadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, subject_list);

        listview.setAdapter(arrayadapter);

    }
}