1

在扩展 a FragmentSQLite查询语句的类中编写,这可能吗?

问题@

我面临的是SQLiteOpenHelper需要一个上下文作为它的参数,但它不可能在Fragment.

我也无法访问Fragment elements from the Activity,因为它在另一个布局中,可以使用 View Pager 吗?

public class ListFrag extends Fragment implements OnClickListener{
    private SQLiteDatabase database;
    private MySQLiteHelper dbHelper=new MySQLiteHelper(this.getActivity().getApplicationContext()); 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w("aaaaaaa","List on Create");
        database = dbHelper.getWritableDatabase();

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.w("b","List on Activity Created");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.home, container,false);

        TableRow leave =(TableRow)view.findViewById(R.id.leave);
        leave.setOnClickListener(this);
        Log.w("ccccccccc","List on Create View");
        TableRow time =(TableRow)view.findViewById(R.id.time);
        time.setOnClickListener(this);

        TableRow cart =(TableRow)view.findViewById(R.id.cart);
        cart.setOnClickListener(this);

        TableRow invoice =(TableRow)view.findViewById(R.id.invoice);
        invoice.setOnClickListener(this);

        TableRow purchase =(TableRow)view.findViewById(R.id.purchase);
        purchase.setOnClickListener(this);

        TableRow travel =(TableRow)view.findViewById(R.id.travel);
        travel.setOnClickListener(this);



        TextView leave_n =(TextView)view.findViewById(R.id.leave_n);
        Cursor LCount= database.rawQuery("select count(*) from LeaveRequest",null); 
        LCount.moveToFirst(); 
        int lcount= LCount.getInt(0);
        LCount.close();
        leave_n.setText(""+lcount);


        return view;
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub


          FragmentManager fragmentManager = getFragmentManager();
          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        switch (v.getId()) {
            case R.id.leave:
                ArrayListFragment list=new ArrayListFragment();
                fragmentTransaction.replace(R.id.frag_capt, list);
                fragmentTransaction.commit();
            break;

        case R.id.time:
            Toast.makeText(this.getActivity().getApplicationContext(), "Toast message",
                    Toast.LENGTH_LONG).show();
            break;

        case R.id.cart:
            Toast.makeText(this.getActivity().getApplicationContext(), "Toast message",
                    Toast.LENGTH_LONG).show();
        break;

        case R.id.invoice:
            Toast.makeText(this.getActivity().getApplicationContext(), "Toast message",
                    Toast.LENGTH_LONG).show();
        break;

        case R.id.purchase:
            Toast.makeText(this.getActivity().getApplicationContext(), "Toast message",
                    Toast.LENGTH_LONG).show();
            break;

        case R.id.travel:
            Toast.makeText(this.getActivity().getApplicationContext(), "Toast message",
                    Toast.LENGTH_LONG).show();
            break;


        }

    }
4

1 回答 1

1

我一直这样做......使用getActivity()

我的 ListFragments 之一的代码片段:

itemCursor = mDbHelper.fetchAllItems();
getActivity().startManagingCursor(itemCursor);
String[] from = new String[] { GroceryDB.ITEM_NAME,
        GroceryDB.ITEM_UNIT, GroceryDB.ITEM_COST };
int[] to = new int[] { R.id.ListItem1, R.id.ListItem2, R.id.ListItem4 };
lists = new CursorAdapter_Check(getActivity(),
        R.layout.rowlayout_itemlist, itemCursor, from, to);
setListAdapter(lists);

编辑

您收到错误是因为您从未打开数据库然后尝试从已关闭的数据库中获取游标。通常在你对数据库做任何事情之前应该有这样的事情:

mDbHelper = new yourDatabase(getActivity());
mDbHelper.open();
于 2012-06-15T06:02:50.833 回答