2012-12-17 58 views
1

我有几个活动,写入和读取数据库。因为客户端必须在它们之间切换。如果我在onCreate中保留代码,当活动重新回到屏幕时,不会更新不同的视图。当活动得到关注时,确保所有数据都已过期的最佳方法是什么? 将evrything从onCreate()移动到onResume()是否明智?android sqlite onresume()oncreate()

有没有wat让它更好?

public class InboxActivity extends ListActivity { 

    List<Candidate> candidates; 
    private DatabaseHandler db; 
    private CandidateAdapter adapter; 
    private ListView lvMain; 
    public void onDestroy(){ 
     if (db != null) 
      db.close(); 

     super.onDestroy(); 
     //db.close(); 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.inbox_list); 
     db = new DatabaseHandler(this); 
     candidates = db.getAllCandidates(1); 
     db.close(); 
     adapter = new CandidateAdapter(this,(ArrayList) candidates); 
     lvMain = getListView(); 
     lvMain.setAdapter(adapter); 
     lvMain.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 

       String name = ((TextView) view.findViewById(R.id.from)).getText().toString(); 
       Log.d("Name ", name); 


       Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class); 
       in.putExtra("name", name); 
       Log.d("Starting activity ", "Yeah "); 
       startActivity(in); 

      } 
     }); 
    } 
    public void onResume(){ 
     super.onResume(); 
     db = new DatabaseHandler(this); 
     candidates = db.getAllCandidates(1); 
     db.close(); 
     adapter = new CandidateAdapter(this,(ArrayList) candidates); 
     lvMain = getListView(); 
     lvMain.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 

    } 





} 

适配器

public class CandidateAdapter extends BaseAdapter{ 
    Context ctx; 
    LayoutInflater lInflater; 
    ArrayList<Candidate> objects; 
    CandidateAdapter(Context context, ArrayList<Candidate> candidates) { 
      ctx = context; 
      objects = candidates; 
      lInflater = (LayoutInflater) ctx 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      } 
    public int getCount() { 
     return objects.size(); 
     } 
    public Object getItem(int position) { 
     return objects.get(position); 
     } 
    public long getItemId(int position) { 
     return position; 
     } 
    Candidate getCandidate(int position) { 
     return ((Candidate) getItem(position)); 
     } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // используем созданные, но не используемые view 
     View view = convertView; 
     if (view == null) { 
      view = lInflater.inflate(R.layout.inbox_list_item, parent, false); 
     } 
     Candidate p = getCandidate(position); 
     ((TextView) view.findViewById(R.id.from)).setText(p.get_name()); 
     ((TextView) view.findViewById(R.id.subject)).setText(p.get_office()); 
     ((RatingBar) view.findViewById(R.id.rateindicator)).setRating(p.get_ranking()); 
     int loader = R.drawable.loader; 


     ImageView image = (ImageView) view.findViewById(R.id.photo); 


     String image_url = p.get_photograph(); 


     ImageLoader imgLoader = new ImageLoader(ctx); 


     imgLoader.DisplayImage(image_url, loader, image); 
     return view; 
    } 



} 
+0

我不知道你在问什么。 – Squonk

回答

1

如果你没有完成你的活动,比你不需要写在所有的onResume东西()

你应该写这篇文章的onResume()。

public void onResume(){ 
    adapter = new CandidateAdapter(this,(ArrayList) candidates); 
    lvMain.setAdapter(adapter); 
    adapter.notifyDataSetChanged() 
} 

CandidateAdapter定义为类变量。

+0

我更新了代码,但它不能以所需的方式工作。 – Yarh

+0

它对我有用,请试试.... –

+0

仍然没有效果。如果删除super.onResume()应用程序刚刚完成错误“没有通过超级onResume调用” – Yarh