gpt4 book ai didi

android - 使用 SQLite 查询语句扩展 fragment 和列表 fragment 类

转载 作者:行者123 更新时间:2023-11-30 04:09:38 25 4
gpt4 key购买 nike

在扩展Fragment的类中写SQLite查询语句,可以吗?

问题@

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

我也无法从 Activity 访问 Fragment 元素,因为它在另一个布局中,可以使用 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;


}

}

最佳答案

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

我的一个 ListFragments 中的代码 fragment :

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();

关于android - 使用 SQLite 查询语句扩展 fragment 和列表 fragment 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11044553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com