扩展片段&使用SQLite查询语句列出片段类

时间:2012-06-15 04:21:04

标签: android sqlite android-fragments

在扩展FragmentSQLite查询语句的类中编写,是否可能?

问题@

我面临的是SQLiteOpenHelper需要上下文作为参数,但不能在Fragment中提供上下文。

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

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;


        }

    }

1 个答案:

答案 0 :(得分: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();