0

How to refresh GridView? Below there is my code. After scanning, application make some updates into sqlite database, and at the end kill Activity. After application back to fragment with GridView, which is old without updates. Any help?

Scanning class:

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

Fragment class:

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

}
waclaw
  • 433
  • 1
  • 7
  • 18

2 Answers2

0

The GridView has an invalidateViews() method.

when you call this method: "all the views to be rebuilt and redrawn." http://developer.android.com/reference/android/widget/GridView.html

I hope this helps.

sanjeeb
  • 87
  • 1
  • 12
0

You can start your activity for result and can do respectively after getting that result Here is the link for this. To update the UI you can call invalidating function corresponding to your view if it is not updating but it will.

For an overview you can see the code here

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

Response will be entertained in onActivityResult function

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

}

Do vote if this is beneficial to you

Areeb Gillani
  • 440
  • 4
  • 25
  • There no problem with result, (insert into db) but when i close activity. My fragment is without changes, maybe some method onResume? But I don't know how to use it – waclaw May 19 '15 at 18:06
  • yes i am saying the same thing when you close that activity put some variable or data in ur intent to tell the previous activity that new data is arrived. Take a look at this link for more details http://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android – Areeb Gillani May 19 '15 at 19:29