1

在这个活动中,我试图动态存储数据,但是当我在模拟器中按下后退按钮时,我的列表项被删除了.....

这是我的主要活动...

      public class Dynamic extends Activity {

    Button bt,bt2;
    EditText et,et2;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt = (Button) findViewById(R.id.nameok);
        et = (EditText) findViewById(R.id.name);

        final String input = et.getText().toString();

        bt.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                if (et.getText().length() == 0) {
                    Context context = getApplicationContext();
                    CharSequence text = "Enter Your Name";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                } else {
                    if (et.getText().length() == 0) {
                        Context context = getApplicationContext();
                        CharSequence text = "Enter Your Number";
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    } else {
                    et2.setVisibility(View.VISIBLE);
                    bt2.setVisibility(View.VISIBLE);
                      bt2.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            String input = et.getText().toString();
                            String input1 = et2.getText().toString();
                            Intent in = new Intent(Dynamic.this, GetList.class);
                            in.putExtra("input", input);
                            in.putExtra("input1", input1);
                            startActivity(in);


                        }
                    });

                }
                }
            }
        });
    }
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        et2 = (EditText) findViewById(R.id.number);
        bt2 = (Button) findViewById(R.id.numberok);

    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

这是我给列表的列表:

  public class GetList extends Activity {
public void onCreate(Bundle savedInstanceState) {
    ListView lv;
    Button bt3;

    ArrayAdapter<String> a_adapter;
    ArrayList<String> x_listItems;
    ArrayList<String> y_listItems;
    super.onCreate(savedInstanceState);

    setContentView(R.layout.list);

    lv = (ListView) findViewById(R.id.listView1);
    bt3 = (Button) findViewById(R.id.nameok1);
    bt3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent in3=new Intent(GetList.this,Dynamic.class);
            startActivity(in3);
        }
    });

    x_listItems = new ArrayList<String>();
    y_listItems = new ArrayList<String>();

    a_adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, x_listItems);

    lv.setAdapter(a_adapter);

    Intent in = getIntent();

    String inp = in.getStringExtra("input");
    String inp1 = in.getStringExtra("input1");

    if (null != inp && inp.length() > 0) {
        x_listItems.add(inp);

        a_adapter.notifyDataSetChanged();
    }
    if (null != inp1 && inp1.length() > 0) {
        y_listItems.add(inp1);
        Log.v("num", ""+y_listItems);


    }
}

     }
4

3 回答 3

1

如果您在 GetList Activity 上按下后退按钮,则 ArrayList 的值将被删除,因为如果您想保留 ArrayList 的值,则 Activity 已销毁

ArrayList<String> x_listItems;
ArrayList<String> y_listItems;

对此

public static ArrayList<String> x_listItems;
public static ArrayList<String> y_listItems;
于 2013-02-08T08:16:40.343 回答
1

尝试从您的 oncreate 方法中声明所有变量..

    public class GetList extends Activity {
     ListView lv;
        Button bt3;

        ArrayAdapter<String> a_adapter;
        ArrayList<String> x_listItems;
        ArrayList<String> y_listItems;

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

        lv = (ListView) findViewById(R.id.listView1);
        bt3 = (Button) findViewById(R.id.nameok1);
        bt3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent in3=new Intent(GetList.this,Dynamic.class);
                startActivity(in3);
            }
        });

        x_listItems = new ArrayList<String>();
        y_listItems = new ArrayList<String>();

        Intent in = getIntent();

        String inp = in.getStringExtra("input");
        String inp1 = in.getStringExtra("input1");

        if (null != inp && inp.length() > 0) {
            x_listItems.add(inp);

            a_adapter.notifyDataSetChanged();
        }
        if (null != inp1 && inp1.length() > 0) {
            y_listItems.add(inp1);
            Log.v("num", ""+y_listItems);


        }

         a_adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, x_listItems);

         lv.setAdapter(a_adapter);
    }


}
于 2013-02-08T07:25:03.797 回答
0

我不确定:

只需从 OnStart 中删除这两行并粘贴到 OnCreate。我只是根据android生命周期这么说。删除 OnStart 会更好

et2 = (EditText) findViewById(R.id.number);
bt2 = (Button) findViewById(R.id.numberok);

我在您的代码中发现了一些错误:

          if (et.getText().length() == 0) {
            Context context = getApplicationContext();
            CharSequence text = "Enter Your Name";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        } else {
            if (et2.getText().length() == 0) { // you noticed this as et
                Context context = getApplicationContext();
                CharSequence text = "Enter Your Number";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
于 2013-02-08T06:41:22.243 回答