2016-04-06 17 views
0

データベース名: "events"。 テーブルイベントの "name"、 "status"、 "numbers"属性に属するすべての行のデータをそれぞれ別のリストtmp1、tmp2、tmp3に追加したいとします。 私は次のコードエラー。複数のリストに同時に追加されるデータベースクエリ

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; 
} 

}

を持っている。しかし、問題は、すべてのリストは、すべてのクエリによって選択されたすべてのデータを取得しているということです。生成された1、1

しかしログ:夕食、ABCD

"ステータス"::はい、はい

"数" の表のイベントには、次のデータを

"名前" があります編集結果は

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 

私はアンドロイドが初めてです。どんな助けもありがとう。

答えて

1

すべての選択を処理するために同じインスタンス '配列'を使用しています。 これは、同じ参照を使用するため、配列に含まれるすべてのデータを 'temp instances'(1,2,3)に保持させます。

例:

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

    ArrayList<String> arrAux = new ArrayList<String>(); 

    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); 
      arrAux.add(uname); 
    } 
    crs.close(); 
    datab.close(); 


    return arrAux; 
} 
関連する問題