2015-05-19 141 views
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 –