Android 入門到精通 (介面編程#3-ListView)

來源:互聯網
上載者:User
    在Android中ListView的使用較為複雜一點,也就是配置其Adapter,Adapter有幾種,有ArrayAdapter,SimpleAdapter等,首先要產生一個ListView(當然可以使用ListActivity,此Activity整合了ListView)然後用Adapter來設定ListView的顯示資料及布局方式,然後再來響應OnItemClick 事件,或者在ListActivity改寫onListItemClick 響應事件函數。看如下代碼示範了使用ListActivity:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Test extends ListActivity {
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  // TODO Auto-generated method stub
  super.onListItemClick(l, v, position, id);
  this.setTitle(this.mModelData.get(position).get("type").toString());
 }
 SimpleAdapter adapter = null;
 private ArrayList<Map<String, Object>> mModelData = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initModelData();
        adapter = new SimpleAdapter(this, mModelData, android.R.layout.two_line_list_item, new String[]{"name"}, new int[]{android.R.id.text1});
        this.setListAdapter(adapter);
        //setContentView(R.layout.main);
    }
    public void initModelData()
    {
     mModelData = new ArrayList<Map<String, Object>>();
     Map<String, Object> item = new HashMap<String,Object>();
     item.put("name", "Linux");item.put("type", "OS");
     mModelData.add(item);
     Map<String, Object> item2 = new HashMap<String,Object>();
     item2.put("name", "Android");item2.put("type", "Platform");
     mModelData.add(item2);
     Map<String, Object> item3 = new HashMap<String,Object>();
     item3.put("name", "Tomato");item3.put("type", "Fruit");
     mModelData.add(item3);
     
    }
}

下面代碼顯示了,使用ListView + Activity:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class TestStringList extends Activity implements OnItemClickListener {
    @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  this.setTitle("You Click Item:" + String.valueOf(arg2));  
 }
 private ListView mListView = null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  mListView = new ListView(this);
  mListView.setOnItemClickListener(this);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,
    new String[]{"ItemA", "ItemB", "ItemC"});
  mListView.setAdapter(adapter);
  this.setContentView(mListView);
 }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.