0

如何刷新GridView?下面是我的代碼。掃描完成後,應用程序將更新到sqlite數據庫中,並在最後kill Activity。應用程序返回到使用沒有更新的舊版GridView的片段。任何幫助?Sqlite更新後Android 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; 
    } 

} 

回答

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

}

投了否決票,如果這是你

+0

結果沒有問題,(插入到數據庫)但當我關閉活動。我的片段沒有改變,也許有一些方法onResume?但我不知道如何使用它 – waclaw

+0

是的我在說當你關閉那個活動時把同樣的東西放在你的某個變量或數據中,意圖告訴以前的活動新數據已經到達。看看這個鏈接的更多細節http://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android –