简体   繁体   English

android sqlite onresume()oncreate()

[英]android sqlite onresume() oncreate()

I have several activities that write and read to database. 我有一些写和读数据库的活动。 As client has to switch between them. 由于客户端必须在它们之间切换。 If i leave code just in onCreate, different views will not be updated when activity is broght back to screen. 如果我仅将代码保留在onCreate中,则当活动重新回到屏幕上时,将不会更新不同的视图。 What is the best way to ensure that all data is apdated when activity gets focus? 什么是确保活动集中注意力时确保所有数据都正确的最佳方法是什么? Is it wise to move evrything from onCreate() to onResume() ? 将evrything从onCreate()移到onResume()是否明智?

is there wat to make it better? 有水可以使它变得更好吗?

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

    }





}

adapter 适配器

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



}

If you are not finishing your activity, than you no need to write all thing in onResume() 如果您尚未完成活动,则无需在onResume()中编写所有内容

you should write just this in onResume(). 您应该只在onResume()中编写此代码。

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

define your CandidateAdapter as a class variable. CandidateAdapter定义为类变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM