1. 程式人生 > >第七篇-列表式App:ListActivity及ListView

第七篇-列表式App:ListActivity及ListView

port pos pub per pda xtend emc osi listitem

一、新建一個empty activity的項目。

二、修改MainActivity.java:

extends AppCompactActivity改為extends ListActivity。註釋掉setContentView(R.layout.activity_main);

package com.example.aimee.listapp;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {
    //Step 1 - define data - string array
    String data[] = {
      "Apple",
      "Samsung",
      "Huawei",
      "Xiaomi",
      "HTC",
      "LG",
      "Dummy",
      "Dummy",
      "Dummy",
      "Dummy",
      "Dummy",
      "Dummy",
      "Dummy",
      "Dummy",
      "Dummy",
      "LAST item"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);

        //Step 2 - Define array adapter
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                android.R.id.text1,
                data
        );

        //Step 3 - update adapter
        setListAdapter(adapter);
    }

//    @Override
//    protected void onListItemClick(ListView l, View v, int position, long id) {
//        super.onListItemClick(l, v, position, id);
//        //Create Intent
//
//        //JUmp to anather activity
//    }
}

  

第七篇-列表式App:ListActivity及ListView