一起學android之ArrayAdapter使用(12),androidadapter

來源:互聯網
上載者:User

一起學android之ArrayAdapter使用(12),androidadapter

Adapter常用的實現方式ArrayAdapter、simpleAdapter、SimpleCursorAdapter、BaseAdapter。

1、ArrayAdapter通常用於將數組或List集合的多個值封裝成多個清單項目。

arrayadapter布局檔案:

<span style="font-size:18px;"><?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" >    <ListView        android:id="@+id/lv_arrayadapter"        android:layout_width="fill_parent"        android:layout_height="wrap_content" >    </ListView></LinearLayout></span>

ArrayAdapterTest檔案:

<span style="font-size:18px;">public class ArrayAdapterTest extends Activity {private ListView lv_arrayadapter;private String[] str_name = new String[] { "jack", "debb", "robin", "kikt","dog", "cat", "elep" };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.arrayadapter);initView();setData();}private void initView() {lv_arrayadapter = (ListView) findViewById(R.id.lv_arrayadapter);//註冊監聽事件lv_arrayadapter.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Toast.makeText(ArrayAdapterTest.this, str_name[position], Toast.LENGTH_SHORT).show();}});}private void setData() {//建立ArrayAdapterArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ArrayAdapterTest.this, android.R.layout.simple_list_item_1,str_name);//綁定適配器lv_arrayadapter.setAdapter(arrayAdapter);}}</span>


建立ArrayAdapter時指定的三個參數說明如下:

Contex::整個應用的上下文。

textViewResourceId:資源ID,代表一個TextView,用作ArrayAdapter的列表組件。

objects:清單項目中的資料




轉載請註明出處:http://blog.csdn.net/hai_qing_xu_kong/article/details/42353249  情緒控_





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.