【发布时间】:2016-04-06 18:20:02
【问题描述】:
数据库名称:“事件”。 我想将属于表事件的“名称”、“状态”、“数字”属性的所有行的数据分别添加到不同的列表 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;
}
}
但问题是所有列表都获取了所有查询选择的所有数据。表格事件有以下数据
“名称”:晚餐,abcd
“状态”:是的,是的
"数字" : 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
我是安卓新手。任何帮助将不胜感激。
【问题讨论】:
标签: java android mysql list sqlite