2017-07-08 68 views
0

我在片段中使用數據庫。然而,當我嘗試初始化我的數據庫使用行:我無法在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; 
} 
} 
+0

使用類.MODE_PRIVATE。 – Adithya

+0

@Adithya請告訴我它的完整代碼行 –

回答

0

使用Context.MODE_PRIVATE

這應該工作。

database=getActivity().openOrCreateDatabase("firstdatabase",android.content.Context.MODE_PRIVATE ,null);

+0

無法正常工作...給出了另一個錯誤。錯誤:(51,56)錯誤:不兼容的類型:int無法轉換爲CursorFactory –

+0

更新了答案。一探究竟。 – Adithya

+0

仍然無法正常工作。錯誤說:錯誤:(51,61)錯誤:不兼容的類型:int不能轉換爲DatabaseErrorHandler –

相關問題