gpt4 book ai didi

android sqlite onresume() oncreate()

转载 作者:行者123 更新时间:2023-11-30 03:52:44 24 4
gpt4 key购买 nike

我有几个写入和读取数据库的 Activity 。因为客户端必须在它们之间切换。如果我将代码留在 onCreate 中,当 Activity 返回屏幕时,不同的 View 将不会更新。确保在 Activity 获得焦点时更新所有数据的最佳方法是什么?将所有内容从 onCreate() 移动到 onResume() 是否明智?

有什么办法可以让它变得更好?

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



}

最佳答案

如果你没有完成你的 Activity ,那么你不需要在 onResume() 中写所有东西

你应该在 onResume() 中只写这个。

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

将您的 CandidateAdapter 定义为类变量。

关于android sqlite onresume() oncreate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13908167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com