Sqlite更新后的Android GridView刷新
如何刷新GridView?下面是我的代码。扫描之后,应用程序对sqlite数据库进行一些更新,并在最后终止活动。应用程序运行后返回到带有GridView的片段,该片段很旧,没有更新。有什么帮助吗 扫描类别:Sqlite更新后的Android GridView刷新,android,android-fragments,android-sqlite,android-gridview,Android,Android Fragments,Android Sqlite,Android Gridview,如何刷新GridView?下面是我的代码。扫描之后,应用程序对sqlite数据库进行一些更新,并在最后终止活动。应用程序运行后返回到带有GridView的片段,该片段很旧,没有更新。有什么帮助吗 扫描类别: @Override public void handleResult(Result rawResult) { // Do something with the result here Employee employee_One = new Emp
@Override
public void handleResult(Result rawResult) {
// Do something with the result here
Employee employee_One = new Employee(BitmapFactory.decodeResource(
getResources(), R.drawable.test1), "abc", 1);
Log.v("TAG", rawResult.getText()); // Prints scan results
Log.v("TAG", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
String message = rawResult.toString();
if(rawResult.toString().equals("8005603001555")) {
DbHelper = new DBhelper(this);
DbHelper.open();
DbHelper.insertEmpDetails(employee_One);
DbHelper.close();
finish();
} else {
mScannerView.startCamera();
Toast.makeText(getBaseContext(), "Fail" , Toast.LENGTH_LONG).show();
}
片段类:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_community, container, false);
button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(getActivity(), Scanning.class);
startActivity(i);
}
});
DbHelper = new DBhelper(getActivity());
DbHelper.open();
employeeList = DbHelper.retriveallEmpDetails();
DbHelper.close();
final GridView gridView = (GridView) rootView.findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(getActivity(), employeeList));
return rootView;
}
}
GridView有一个invalidateViews方法 调用此方法时:所有要重建和重绘的视图
我希望这会有所帮助。您可以开始您的活动以获得结果,并可以在获得结果后分别进行 这是我的建议。要更新UI,可以调用视图对应的失效函数(如果视图未更新,但会更新) 有关概述,请参见此处的代码
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
响应将在onActivityResult函数中进行
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
如果这对您有好处,请进行投票在哪里应该正确执行InvalidateView?您是否可以尝试在操作结束的扫描类中使用。结果没有问题,请插入数据库,但在我关闭活动时插入。我的片段没有变化,也许是恢复的方法?但是我不知道如何使用它。是的,当你结束活动时,我也在说同样的话。在你的意图中放入一些变量或数据,告诉之前的活动新数据已经到达。查看此链接了解更多详细信息