简体   繁体   中英

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

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

在此处输入图片说明

This is actually most likely because you are putting the values in a LinkedHashMap which implements the map interface which states

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.

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:

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

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")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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