【问题标题】:Android ListView select item and show information about list itemAndroid ListView 选择项目并显示有关列表项的信息
【发布时间】:2013-07-28 14:25:02
【问题描述】:

我对 Android 很陌生。

我需要创建一个列表视图,其中左侧有一个项目列表,单击一个项目会打开一个对话框,类似于显示项目信息的东西。此外,我希望能够从项目列表中选择这些(或检查?)项目,然后单击同一屏幕中的“完成”按钮返回使用三个选定项目的另一个活动。 我已经尝试阅读 ListView 文档,但到目前为止,Android 还没有那么直观,这就是我真正坚持下来的原因。

这是我当前的代码:

公共类 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 和 2。

谢谢!

【问题讨论】:

    标签: android listview


    【解决方案1】:

    有关信息,您可以将此代码用于对话框,并且可以通过 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      • 2010-11-14
      • 2017-12-21
      • 1970-01-01
      相关资源
      最近更新 更多