如何将onSaveInstanceState嵌入到onCreate方法中;

时间:2015-02-14 14:10:18

标签: java

我的应用会创建一个列表视图,其中的项目包括电话。当电话结束时,返回活动必须重新创建它。由于这个和耗时的连接应用程序 - 数据库我尝试实现onSaveInstanceState,如:

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("new_final_itinList", final_itinList);         

    }

并检索ArrayList" new_final_itinList"在OnCreate方法中。

OnCreate:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkitineraries);

        arithm =  new ArrayList<String>(); 
        itinList = new ArrayList<ArrayList<String>>();              
        itinList3 = new ArrayList<ArrayList<String>>(); 

            if (savedInstanceState != null) {
                new_final_itinList = (ArrayList<ArrayList< String>>) savedInstanceState.getSerializable("new_final_itinList");              

                 ListView list1 = (ListView) findViewById(R.id.list);       


                final class Adapter extends BaseAdapter {

                    ArrayList<ArrayList< String>> final_itinList5 =new ArrayList<ArrayList< String>>();


                    public Adapter(
                            ArrayList<ArrayList<String>> final_itinList2) {
                        super();
                        this.final_itinList5 = new_final_itinList;
                    }                   

                    @Override
                    public int getCount() {
                        // TODO Auto-generated method stub
                        return final_itinList5.size();
                    }

                    @Override
                    public Object getItem(int position) {

                        return final_itinList5.get(position);
                    }

                    @Override
                    public long getItemId(int position) {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public int getViewTypeCount() {
                        return 2;
                    }

                    @Override
                    public int getItemViewType(int position) {
                        if (final_itinList5.get(position).get(1)
                                .equals(username)) {
                            return 0;
                        } else {
                            return 1;
                        }
                    }

                    @Override
                    public View getView(final int position,
                            View convertView, ViewGroup parent) {
                        View v = convertView;
                        int type = getItemViewType(position);

                        Log.d("type", String.valueOf(type));
                        if (v == null) {
                            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            if (type == 0) {

                                v = inflater.inflate(
                                        R.layout.header_list_item, parent,
                                        false);
                            } else {
                                v = inflater.inflate(R.layout.list_item2,
                                        parent, false);
                            }

                        }

                        if (type == 0) {

                            v1 = (TextView) v.findViewById(R.id.start_pro);
                            v2 = (TextView) v.findViewById(R.id.final_pro);
                            v1.setText(final_itinList5.get(position).get(3));
                            v2.setText(final_itinList5.get(position).get(7));

                        } else {


                             final String phone_number = final_itinList5.get(position).get(10);


                            TextView number_n = (TextView) v.findViewById(R.id.number_n);
                            number_n.setText(final_itinList5.get(position).get(12));


                            TextView start_Poli = (TextView) v.findViewById(R.id.startpoli);
                            start_Poli.setText(final_itinList5.get(position).get(3));

                            TextView final_Poli = (TextView) v.findViewById(R.id.finalpoli);
                            final_Poli.setText(final_itinList5.get(position).get(7));


                            TextView num_km = (TextView) v.findViewById(R.id.numKm);
                            num_km.setText(final_itinList5.get(position).get(11));

                            Button btnphone = (Button) v
                                    .findViewById(R.id.btnphone);

                            btnphone.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    try {
                                        Intent intent = new Intent(
                                                Intent.ACTION_CALL);
                                        intent.setData(Uri.parse("tel:"
                                                + phone_number));
                                        startActivity(intent);
                                    } catch (Exception e) {
                                        Log.e("Demo application",
                                                "Failed to invoke call", e);
                                    }
                                }

                            });

                        }
                        return v;
                    }

                }

                Adapter adapter = new Adapter(new_final_itinList);

                list1.setAdapter(adapter);  


                }           
            }   
            else {              
        new GetData().execute();
            }
    }

根据第一个&#34; onSaveInstanceState&#34;返回第一个电话的活动不是问题。实施工作正常。但我必须实现&#34; onSaveInstanceState&#34;每次客户拨打电话。考虑到我不能将一种方法(&#34; onSaveInstanceState&#34;)嵌入另一种方法(&#34; onCreate&#34;),这怎么可能呢?提前致谢

0 个答案:

没有答案