mercredi 6 avril 2016

ERROR. Database query getting added to multiple lists simultaneously

Database Name : "events". I want to add the data of all rows belonging to "name","status","numbers" attribute of table event to different lists tmp1,tmp2,tmp3 respectively. I have the following code

public class OneFragment extends Fragment {

FrameLayout frame;
RecyclerView recList;
TextView BlankDB;
private SQLiteDatabase datab;
public ArrayList<String> array = new ArrayList<String>();


public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_one, container, false);
    frame = (FrameLayout)v.findViewById(R.id.frame);

    if(checkDataBase()) {
        recList = new RecyclerView(getActivity());
        recList.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recList.setLayoutManager(llm);

        //Getting the elements from DB
        ArrayList<String> tmp1 = getListofevents("events", "name");
        ArrayList<String> tmp2 = getListofstatus("events","status");
        ArrayList<String> tmp3 = getListofmembers("events", "numbers");
        for(int i=0;i<tmp3.size();i++){
            Log.e("The length of tmp1 is: ", String.valueOf(tmp1.get(i)));
            Log.e("The length of tmp2 is: ", String.valueOf(tmp2.get(i)));
            Log.e("The length of tmp3 is: ", String.valueOf(tmp3.get(i)));


        }

      //  String[] array4 = tmp4.toArray(new String[tmp4.size()]);
        Log.e("The length of tmp1 is: ", String.valueOf(tmp1.size()));

        ContactAdapter ca = new ContactAdapter(tmp1);
        recList.setAdapter(ca);
        frame.addView(recList);
    }

    else{
        BlankDB = new TextView(getActivity());
        BlankDB.setText("There is no event to display");
        BlankDB.setTextSize(40);
        frame.addView(BlankDB);
    }
    return v;
}

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        checkDB = SQLiteDatabase.openDatabase(getActivity().getDatabasePath("events").toString(), null,
                SQLiteDatabase.OPEN_READONLY);
        checkDB.close();
    } catch (SQLiteException e) {
        // database doesn't exist yet.
    }
    return checkDB != null;
}


public ArrayList<String> getListofevents(String evName,String attribute) {

    datab = getActivity().openOrCreateDatabase(evName, Context.MODE_PRIVATE, null);

    Cursor crs = datab.rawQuery("SELECT * FROM event", null);

    while(crs.moveToNext()){
        String uname = crs.getString(crs.getColumnIndex(attribute));
        Log.e("The string is : ", uname);
        array.add(uname);
    }
    crs.close();
    datab.close();
    return array;
}

public ArrayList<String> getListofstatus(String evName,String attribute) {

    datab = getActivity().openOrCreateDatabase(evName, Context.MODE_PRIVATE, null);

    Cursor crs = datab.rawQuery("SELECT * FROM event", null);

    while(crs.moveToNext()){
        String uname = crs.getString(crs.getColumnIndex(attribute));
        Log.e("The string is : ", uname);
        array.add(uname);
    }
    crs.close();
    datab.close();
    return array;
}

public ArrayList<String> getListofmembers(String evName,String attribute) {

    datab = getActivity().openOrCreateDatabase(evName, Context.MODE_PRIVATE, null);

    Cursor crs = datab.rawQuery("SELECT * FROM event", null);

    while(crs.moveToNext()){
        String uname = crs.getString(crs.getColumnIndex(attribute));
        Log.e("The string is : ", uname);
        array.add(uname);
    }
    crs.close();
    datab.close();
    return array;
}

}

But the problem is that all the lists are getting all the data that is selected by all queries. The table event has the following data

"name" : dinner , abcd

"status" : Yes, Yes

"number" : 1 , 1

But the log generated as the result of compilation shows

04-06 23:33:59.023 2302-2302/? E/The string is :: dinner
04-06 23:33:59.023 2302-2302/? E/The string is :: abcd
04-06 23:33:59.024 2302-2302/? E/The string is :: Yes
04-06 23:33:59.024 2302-2302/? E/The string is :: Yes
04-06 23:33:59.024 2302-2302/? E/The string is :: 1
04-06 23:33:59.024 2302-2302/? E/The string is :: 1
04-06 23:33:59.029 2302-2302/? E/The length of tmp1 is:: dinner
04-06 23:33:59.029 2302-2302/? E/The length of tmp2 is:: dinner
04-06 23:33:59.029 2302-2302/? E/The length of tmp3 is:: dinner
04-06 23:33:59.029 2302-2302/? E/The length of tmp1 is:: abcd
04-06 23:33:59.029 2302-2302/? E/The length of tmp2 is:: abcd
04-06 23:33:59.029 2302-2302/? E/The length of tmp3 is:: abcd
04-06 23:33:59.029 2302-2302/? E/The length of tmp1 is:: Yes
04-06 23:33:59.029 2302-2302/? E/The length of tmp2 is:: Yes
04-06 23:33:59.029 2302-2302/? E/The length of tmp3 is:: Yes
04-06 23:33:59.029 2302-2302/? E/The length of tmp1 is:: Yes
04-06 23:33:59.029 2302-2302/? E/The length of tmp2 is:: Yes
04-06 23:33:59.029 2302-2302/? E/The length of tmp3 is:: Yes
04-06 23:33:59.029 2302-2302/? E/The length of tmp1 is:: 1
04-06 23:33:59.029 2302-2302/? E/The length of tmp2 is:: 1
04-06 23:33:59.029 2302-2302/? E/The length of tmp3 is:: 1
04-06 23:33:59.029 2302-2302/? E/The length of tmp1 is:: 1
04-06 23:33:59.029 2302-2302/? E/The length of tmp2 is:: 1
04-06 23:33:59.029 2302-2302/? E/The length of tmp3 is:: 1
04-06 23:33:59.029 2302-2302/? E/The length of tmp1 is:: 6

I am new to android. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire