2012-05-03 93 views
2

我正在学校开展一个项目,我需要帮助正确实施ListViews。我必须执行项目上的“设置”和“帮助”部分,截至目前,我可以显示列表中的内容,并在单击时显示选定项目的Toast消息。我的问题是,如何创建并显示特定项目内的内容?例如,我有一个选项"Edit Password",当我点击它时,它应该显示我的"Edit Password"屏幕。这适用于我的两个部分。ListView帮助

这是我到目前为止。它基本上是android ListView教程,但我在那里添加了我的选项。我的问题是,当我点击列表中的某个选项时,如何显示其具体的详细信息?就像我之前所说的,如果我点击"Edit Password",我想让它进入"Edit Password"屏幕。或者在帮助上,如果我点击让我们说“信用”,我希望它将我引导至信用页面。

public class Settings extends ListActivity { 
     /** Called when the activity is first created. */ 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setListAdapter(new ArrayAdapter<String>(this, R.layout.settings, SETTINGS)); 

      ListView lv = getListView(); 
      lv.setTextFilterEnabled(true); 
      lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, 
        long id) 
      { 
       // When clicked, show a toast with the TextView text 
       Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
         Toast.LENGTH_SHORT).show(); 
      } 
      }); 
     } 

    static final String[] SETTINGS = new String[] 
      {"Set Refresh Rate", "Edit Password", "Delete Account"}; 

}

+1

你使用什么语言/设备? – assylias

+0

我正在使用Eclipse进行项目。 – wasabiman

+0

好的,但这是Java? C#? Android设备开发?等等,如果你不显示你迄今为止尝试过的东西,那么你不可能得到任何答案。我建议你尝试编写一些代码,一旦遇到问题,请询问关于您的具体问题的具体问题。 – assylias

回答

1

当你扩展ListActivity你已经有了OnItemClickListener实施,你应该覆盖的方法onListItemClick。在这种方法中,你应该使用Intent得到一个新的Activity在那里你会显示你想要的东西:

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    Intent i = new Intent(this, SecondActivityName.class); 
    i.putExtra("pos", position); //pass the position of the clicked row so we know what to show. 
    startActivity(i); // let's go to the other activity 
} 

SeconActivityNameActivity,你应该创建并在那里你会显示你想要的其他屏幕(记住将活动添加到清单文件!):

public class SecondActivityName extends Activity { 

    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
       Intent i = getIntent(); 
       int rowPosition = i.getIntExtra("pos", -1); //we now have the row's position that was clicked in the other activity. 
       // based on that you show the screen you want for example after a switch 
       switch (rowPosition) { 
        case -1: 
         //this is the default value, something has gone terrible wrong 
         //finish the activity and hide: 
         finish(); 
         break; 
        case 0: 
         //row 0 was clicked so show the "Set Refresh Rate" screen 
         setContentView(R.layout.refresh_rate__screen); 
         break; 
       //do the same for the other rows; 
//... 

这将适用于各种设置的屏幕没有那么不同的情况。如果他们是你可能必须为每个人执行一个不同的活动,并根据位置参数在onListItemClick中开始适当的活动。

+0

谢谢!但是..我很抱歉地说,但如果我想扩展SherlockActivity而不是ListActivity,这是否仍然适用? – wasabiman

+0

我试过了,但不起作用。当我点击一个项目时,它会崩溃应用程序。 – wasabiman

+0

@wasabiman如果你想使用'ActionBarSherlock',你可以扩展'SherlockListActivity',让你在'ListActivity'中有'ActionBar'(如果你扩展了'SherlockActivity',你将不得不提供你自己的布局和一个'ListView' )。我猜不出为什么你的代码崩溃了,我必须看到你在'Logcat'中得到的异常。我也忘记了一些'''在我的代码中,我希望这不是你崩溃的原因。 – Luksprog