在扩展 a Fragment
,SQLite
查询语句的类中编写,这可能吗?
问题@
我面临的是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;
}
}