Android ListView选择项目并显示有关列表项的信息

时间:2013-07-28 14:25:02

标签: android listview

我是Android新手。

我需要创建一个列表视图,左边有一个项目列表,点击一个项目会打开一个对话框,就像显示项目信息一样。此外,我希望能够从项目列表中选择这些(或检查?)项目,然后单击同一屏幕中的“完成”按钮以返回到使用三个所选项目的另一个活动。 我已经尝试过阅读ListView文档了,但到目前为止Android并没有那么直观,这就是为什么我真的很坚持。

这是我目前的代码:

public class PlayerList扩展了ListActivity {

static final String[] PLAYERS = new String[] { "Messi", "Ronaldo", "Drogba", "Wesley"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // no more this
    // setContentView(R.layout.list_fruit);

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view2,PLAYERS));

    ListView listView = getListView();
    listView.setTextFilterEnabled(true);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text

            // edit this part to be able to show a new screen with info about the player

            Toast.makeText(getApplicationContext(),
            ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });

}

您能否告诉我如何1)点击后为每个项目创建详细屏幕,2)能够检查/选择项目,3)修改当前代码以包括1&amp; 2。

谢谢!

1 个答案:

答案 0 :(得分:0)

有关信息,您可以将此代码用于对话框,这可以通过showSettingsAlert()调用;您希望在何处显示此对话框。

   public void showSettingsAlert(){
         AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

         // Setting Dialog Title
         alertDialog.setTitle("Title here");

         // Setting Dialog Message
         alertDialog.setMessage("here the information can be display");


         alertDialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
             dialog.cancel();

             }
         });

         // Showing Alert Message
         alertDialog.show();
     }

对于另一件事看上下文动作模式。这些链接对您有所帮助

http://developer.android.com/guide/topics/ui/menus.html
http://www.vogella.com/articles/AndroidActionBar/

相关问题