2011-11-28 60 views
1

我做了一个列表,使得每次点击某个项目时都会打开另一个页面。问题是,如果您使用后退按钮,它将关闭应用程序。我宁愿回到前面的菜单。我怎样才能解决这个问题?当我从列表中选择一个项目时回来

public class Listview extends Activity { 

    static ListView listView; 

    static public class BackgroundWorker extends AsyncTask<Void, Person, Void> { 
     @SuppressWarnings("unchecked") 
     @Override 
     protected void onPreExecute() { 
      // Prima di iniziare a inserire gli elementi svuotiamo l'adapter 
      ((ArrayAdapter<Person>) listView.getAdapter()).clear(); 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground (Void... params) { 
      Person[] people = { new Person("Privacy",R.drawable.icon1) }; 
      Person[] people1 = {new Person("Visualizzazione", R.drawable.icon1)}; 
      Person[] people2= { new Person("Notifiche", R.drawable.icon1)}; 

      // riempimento casuale della lista delle persone 
      Random r = new Random(); 

      for (int i = 0; i < 1; i++) { 
       // Pubblichiamo il progresso 
       publishProgress(people); 
       publishProgress(people1); 
       publishProgress(people2); 
      } 

      return null; 
     } 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void onProgressUpdate (Person... values) { 
      // Aggiungiamo il progresso pubblicato all'adapter 
      ((ArrayAdapter<Person>) listView.getAdapter()).add(values[0]); 
      super.onProgressUpdate(values); 
     } 
    } 

    @Override 
    public void onCreate (final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     listView = (ListView) findViewById(R.id.personListView); 
     listView.setAdapter(new PersonAdapter(this, R.layout.row_item, new ArrayList<Person>())); 
     listView.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Person p = (Person) parent.getItemAtPosition(position); 

       switch(position) { 
        case 0: 
         onCreate1(savedInstanceState); 
         // setContentView(R.layout.main1); 
         break; 
        case 1: 
         setContentView(R.layout.main2); 
         break; 
        case 2: 
         setContentView(R.layout.main3); 
       } 
      } 
     }); 
     new BackgroundWorker().execute(); 
    } 
} 

回答

3

你的问题是在这里:

switch(position) { 
    case 0: 
     onCreate1(savedInstanceState); 
     // setContentView(R.layout.main1); 
     break; 
    case 1: 
     setContentView(R.layout.main2); 
     break; 
    case 2: 
     setContentView(R.layout.main3); 
} 

你不应该使用超过一次setContentView更多的Activity。您应该改为使用startActivity()启动新的Activity。通过这种方式,您可以查看按回退后将返回的活动历史记录。

+0

我该怎么办? :( – user1023571

+1

我解决了......谢谢:) – user1023571

2

你重写后退按钮和执行自己的代码。就像这样:

@Override 
    public void onBackPressed() { 
     //Your code 
    return; 
    } 
相关问题