我在片段中使用數據庫。然而,當我嘗試初始化我的數據庫使用行:我無法在Android中使用片段數據庫
database=openOrCreateDatabase("firstdatabase",MODE_PRIVATE,null);
它給了我一個錯誤說:
Error:(47, 55) error: cannot find symbol variable MODE_PRIVATE
這是我的代碼:
public class Expenses extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.expenses,container,false);
final SQLiteDatabase database;
Cursor c;
ArrayList<String> al=new ArrayList<>();
ImageButton img=(ImageButton)v.findViewById(R.id.img);
final EditText commodity=(EditText)v.findViewById(R.id.commodity);
final EditText amount=(EditText)v.findViewById(R.id.amount);
ListView list=(ListView)v.findViewById(R.id.list);
Calendar calendar=Calendar.getInstance();
final int day=calendar.get(Calendar.DAY_OF_MONTH);
final int month=calendar.get(Calendar.MONTH);
final int year=calendar.get(Calendar.YEAR);
registerForContextMenu(list);
database=openOrCreateDatabase("firstdatabase",MODE_PRIVATE,null);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Entered Successfully", Toast.LENGTH_SHORT).show();
String c=commodity.getText().toString();
int a=Integer.parseInt(amount.getText().toString());
String q2="insert into expenses(day,month,year,commodity,amount)values('"+day+ "','" +month+ "','" +year+"','"+c+"','"+a+"');";
database.execSQL(q2);
}
});
String q1 = "select * from expenses";
c = database.rawQuery(q1, null);
c.moveToFirst();
do {
String comm = c.getString(c.getColumnIndex("commodity"));
String amo = c.getString(c.getColumnIndex("amount"));
al.add(comm + "\n" + amo);
ArrayAdapter ad = new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,al);
list.setAdapter(ad);
}
while (c.moveToNext());
return v;
}
}
使用類.MODE_PRIVATE。 – Adithya
@Adithya請告訴我它的完整代碼行 –