2013-02-08 71 views
1

在該活動中我試圖動態存儲數據,但是當IM在我的模擬器按後退按鈕我的列表中的項目越來越刪除.....當IM按後退按鈕我的列表中的項目越來越刪除

這是我的主要活動......

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


    } 
} 

    } 
+0

你傳遞另一個值,並把它放在你的ArrayList的,因爲當你點擊從你的第二個活動即將上的EditText值將被保留,因爲你沒有完成後退按鈕您的主要活動 – DevfaR

+0

yaaa IM只通過另一個變量 – Ramz

回答

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(); 
      } 
+0

不存在這是沒有錯我的團隊負責人說,我們必須節省意圖列表中,但即時通訊沒有得到正確的程序 – Ramz

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


} 
+0

thnaq但它不工作...... :( – Ramz

+0

我明白了,你說我們必須給所有的變量oncreate權利,我們必須創建對象還在創造thnaqqqqq – Ramz

+0

你有沒有解決你的問題? – Maulik

1

如果按後退按鈕,當你對你的GetList活動ArrayList中的值將刪除,因爲如果你想保留的活性破壞ArrayList中的價值使他們

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

這個

public static ArrayList<String> x_listItems; 
public static ArrayList<String> y_listItems; 
+0

thanx來自我試過的答案,但實際上前面的值正在刪除,並與輸入的值 – Ramz

+0

任何人都可以幫我這個 – Ramz

+0

以及我嘗試編碼你正在做的一個,它的工作對我來說使靜態變量將保留它的數據;嘗試記錄這個。 Log.v( 「NUM」, 「」 + x_listItems.toString()); Log.v(「num」,「」+ y_listItems.toString()); – DevfaR