我在片段中使用数据库。但是,当我尝试初始化我的数据库时使用以下行:
database=openOrCreateDatabase("firstdatabase",MODE_PRIVATE,null);
它给我一个错误说:
错误:(47,55)错误:找不到符号变量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;
}
}
答案 0 :(得分:0)
使用Context.MODE_PRIVATE
。
这应该有用。
database=getActivity().openOrCreateDatabase("firstdatabase",android.content.Context.MODE_PRIVATE ,null);