0

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

按钮用于新联系人,列表用于联系人列表。1.如何在这个List View上使用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>
4

1 回答 1

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.
     }
   });

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

于 2012-08-09T11:38:13.763 回答