在Sqlite更新后刷新Android GridView

时间:2015-05-19 17:42:59

标签: android android-fragments android-sqlite android-gridview

如何刷新GridView?下面是我的代码。扫描完成后,应用程序会对sqlite数据库进行一些更新,并最终杀死Activity。使用GridView应用程序返回到片段后,这是旧的没有更新。有什么帮助吗?

扫描课程:

  @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;
    }

}

2 个答案:

答案 0 :(得分:0)

GridView有一个invalidateViews()方法。

当你调用这个方法时:"所有要重建和重绘的视图。" http://developer.android.com/reference/android/widget/GridView.html

我希望这会有所帮助。

答案 1 :(得分:0)

您可以为结果开始活动,并且可以在获得结果后分别执行 这是link。要更新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)
    }
}

}

如果这对您有益,请投票