数据库名称:"事件"。 我想添加属于" name"," status"," number"的所有行的数据。表事件的属性分别为不同的列表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;
}
}
但问题是所有列表都获取了所有查询选择的所有数据。表事件具有以下数据
&#34;名称&#34; :晚餐,abcd
&#34;状态&#34; :是的,是的
&#34;数&#34; :1,1
但是编译结果生成的日志显示
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
我是android新手。任何帮助将不胜感激。
答案 0 :(得分:1)
您正在使用相同的实例'array'来处理所有选择。 这会导致“临时实例”(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;
}