2

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

这就是我到目前为止所拥有的。它基本上是 androidListView教程,但我在那里添加了我的选项。我的问题是,当我单击列表中的一个选项时,如何显示其具体细节?就像我之前说的,如果我点击"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"};

}

4

1 回答 1

1

当您扩展ListActivity您已经OnItemClickListener实现时,您应该覆盖方法onListItemClick。在这种方法中,您应该使用 anIntent进入一个新的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
}

SeconActivityName是一个Activity你应该创建的,你会在哪里显示你想要的另一个屏幕(记住将活动添加到清单文件中!):

    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根据位置参数启动适当的活动。

于 2012-05-04T04:23:09.300 回答