简体   繁体   English

数据库忽略列中具有相同值的行-Android

[英]Database ignoring rows with same value in column - Android

I am populating a RecyclerView with a table that has two columns it's basically a dictionary and when ever I call the method to start populating something unusual happens, the code ignores the rows with the same values in the first column "col1" and just get the last one and moves to the next and so on 我用一个包含两列的表填充一个RecyclerView,这基本上是一个字典,每当我调用该方法开始填充不正常的事情时,代码都会忽略第一列“ col1”中具有相同值的行,并得到最后一个,移至下一个,依此类推

JAVA 爪哇

public void fetchData()
{
    db =new DataBaseHelper(getContext());
    try {

        db.createDataBase();
        db.openDataBase();

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    namelist=new LinkedHashMap<>();
    int ii;
    SQLiteDatabase sd = db.getReadableDatabase();
    Cursor cursor = sd.query("dictionary_lib" ,null, null, null, null, null, null);
    ii=cursor.getColumnIndex("English_lib");
    eng_list=new ArrayList<String>();
    nor_list= new ArrayList<String>();
    while (cursor.moveToNext()){
        namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("German_lib")));
    }
    Iterator entries = namelist.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry thisEntry = (Map.Entry) entries.next();
        eng_list.add(String.valueOf(thisEntry.getKey()));
        german_list.add(String.valueOf(thisEntry.getValue()));
    }

    for (int i = 0; i < eng_list.size(); i++) {
        data.add(new DictObjectModel(eng_list.get(i), german_list.get(i)));
    }
    adapter = new CustomAdapter(data);
    rv.setAdapter(adapter);
}

EXAMPLE OF THE DATABASE 数据库示例

here the recyclerview will ignore the first two rows of alpha and prints the third one and move to the next 在这里,recyclerview将忽略前两行alpha,并打印第三行并移至下一行

在此处输入图片说明

This is actually most likely because you are putting the values in a LinkedHashMap which implements the map interface which states 实际上,这很可能是因为您将值放入LinkedHashMap ,该实现实现了映射接口,

An object that maps keys to values. 将键映射到值的对象。 A map cannot contain duplicate keys; 映射不能包含重复的键; each key can map to at most one value. 每个键最多可以映射到一个值。

You are retrieving duplicate keys here: 您正在此处检索重复的密钥:

while (cursor.moveToNext()){
    namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("German_lib")));
}

So the first two example values are replaced in the map as the key "alpha" is duplicated. 因此,当键"alpha"被复制时,前两个示例值将在映射中替换。

To verify this you could just print out or step over the values within the loop above. 为了验证这一点,您可以仅打印出或跳过上面循环中的值。

One way to resolve this would be to create your own java object: 解决此问题的一种方法是创建自己的java对象:

public class Example
{
    public String col1; // but appropriate names
    public String col2; 

    // Add constructors or getters and setting if you want private members
}

then use an Arraylist and in your loop 然后使用Arraylist并在您的循环中

List<Example> rows = new ArrayList<>;
while (cursor.moveToNext()){
    Example e = new Example();
    e.col1 = cursor.getString(ii);
    e.col2 = cursor.getString(cursor.getColumnIndex("German_lib"))
    rows.add(e)
}

EDIT 编辑

You are creating multiple loops just to get to the same result. 您正在创建多个循环只是为了获得相同的结果。 So you could just within your cursor loop just put 所以你可以在你的光标循环内

data.add(new DictObjectModel(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("German_lib")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM