从列表视图中选择一个项目以及android中的按钮

时间:2012-08-09 11:26:53

标签: android button onclick android-listview

我有一个按钮以及XML中的列表视图

按钮用于新联系人,列表用于联系人列表。 1.如何在此列表视图上使用ArrayAdapter / OnListItemClick?

setListAdapter(new ArrayAdapter<String>(Contacts.this, R.id.ListView));

这个声明是否正确?

2.这种方法是否正确?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/bAddContact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ADD CONTACT" />

    <ListView
        android:id="@+id/ListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试此操作以显示简单列表

    String [] names = 
    { "a" , "b", "c", "d", "e", "f", "g", "h", "i", "j",
            "a1" , "b1", "c1", "d1", "e1", "f1", "g1", "h1", "i1", "j1",
            "a2" , "b2", "c2", "d2", "e2", "f2", "g2", "h2", "i2", "j2",
            "a3" , "b3", "c3", "d3", "e3", "f3", "g3", "h3", "i3", "j3","j4"
    }; 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, names );
     yourList.SetListAdapter(adapter);

这是使用适配器的方式。获取listView的选定项目试试这个。

    listView.setOnItemClickListener(new OnItemClickListener() {

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     long arg3) {
             // TODO Auto-generated method stub
        String  getSelectedItemOfList = yourList.get(pos); // here you can get selected item.
     }
   });

我认为这就是你想要的。 :)