Note that there are some explanatory texts on larger screens.

plurals
  1. POExtended Fragment & List Fragment classes with SQLite query statements
    text
    copied!<p>Writing within a class extending a <code>Fragment</code>, <code>SQLite</code> query statements, is it possible? </p> <p><em>Problem@</em> </p> <p>I am facing is that <code>SQLiteOpenHelper</code> requires a context as its parameter but its not possible to give context in <code>Fragment</code>.</p> <p>I am also not able to access <code>Fragment elements from the Activity</code> as it is in another layout, can View Pager be used?</p> <pre><code>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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload