android中ListView介绍






package com.smart;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ActivityMain extends Activity {

 OnClickListener listener1 = null;
 OnClickListener listener2 = null;
 OnClickListener listener3 = null;
 OnClickListener listener4 = null;
 Button button1;

 Button button2;
 Button button3;
 Button button4;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
   perpareListeners();
   button1=(Button)findViewById(R.id.button1);
   button1.setOnClickListener(listener1);
   button2=(Button)findViewById(R.id.button2);
   button2.setOnClickListener(listener2);
   button3=(Button)findViewById(R.id.button3);
   button3.setOnClickListener(listener3);
   button4=(Button)findViewById(R.id.button4);
   button4.setOnClickListener(listener4);
   
//   button1.setText(resid);
//   button1.setBackgroundColor(color)
 }
 //全部进行实例化,并且根据点击进去的方法
 private void perpareListeners() {
  listener1 = new OnClickListener() {

   @Override
   public void onClick(View v) {
    Intent intent1 = new Intent(ActivityMain.this, ActivtityList1.class);
    startActivity(intent1);
   }
  };

  listener2 = new OnClickListener() {

   @Override
   public void onClick(View v) {
    Intent intent2 = new Intent(ActivityMain.this, ActivtityList2.class);
    startActivity(intent2);
   }
  };

  
  listener3 = new OnClickListener() {

   @Override
   public void onClick(View v) {
    Intent intent3 = new Intent(ActivityMain.this, ActivtityList3.class);
    startActivity(intent3);
   }
  };
  listener4 = new OnClickListener() {

   @Override
   public void onClick(View v) {
    Intent intent4 = new Intent(ActivityMain.this, ActivtityList4.class);
    startActivity(intent4);
   }
  };
 }

}



package com.smart;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ActivtityList1 extends Activity {
 ListView listView;
//数据列表
 private String[] data={"0号","1号","3号","3号","4号","5号"};

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  listView=new ListView(this);
  listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, data));
//  listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
  listView.setItemsCanFocus(true);
  listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//多选
  setContentView(listView);//显示
  
 }
 
 
 
}
package com.smart;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

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

public class ActivtityList2 extends Activity {

 private ArrayList<Map<String, Object>> data;
 private ListView listView = null;
 //数据列表
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  prepareData();
  listView = new ListView(this);
  
  SimpleAdapter adapter = new SimpleAdapter(this, data,
    android.R.layout.simple_list_item_1,
    new String[] { "姓名", "性别" }, new int[] { android.R.id.text1
      });
  
 // 下面为显示的效果
//  SimpleAdapter adapter = new SimpleAdapter(this, data,
//    android.R.layout.simple_list_item_2,
//    new String[] { "姓名", "性别" }, new int[] { android.R.id.text1,
//      android.R.id.text2 });

//   SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list1,
//   new String[] { "姓名", "性别" }, new int[] { R.id.mview1,
//   R.id.mview2 });
  listView.setAdapter(adapter);
  setContentView(listView);

  OnItemClickListener listener = new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View v,
     int position, long id) {
   // 标题的选择显示
    setTitle(parent.getItemAtPosition(position).toString());

   }

  };
  listView.setOnItemClickListener(listener);
 }
// 增加 数据
 public void prepareData() {
  data = new ArrayList<Map<String, Object>>();

  Map<String, Object> item;
  item = new HashMap<String, Object>();
  item.put("姓名", "SMART");
  item.put("性别", "男");
  data.add(item);

  item = new HashMap<String, Object>();
  item.put("姓名", "SMART2");
  item.put("性别", "男");
  data.add(item);

  item = new HashMap<String, Object>();
  item.put("姓名", "SMART3");
  item.put("性别", "女");
  data.add(item);
 }

}
package com.smart;

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

public class ActivtityList3 extends ListActivity {
 private String[] data={};
 
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.list3);
  setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data));
  
  
 }


 @Override
 protected void onListItemClick(ListView listView, View v, int position, long id) {
  super.onListItemClick(listView, v, position, id);
  
  setTitle(listView.getItemAtPosition(position).toString());
 }
 
 
}


package com.smart;

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.OnItemSelectedListener;

public class ActivtityList4 extends Activity {
 ListView listView;
 private String[] data={"0号","1号","3号","3号","4号","5号"};


 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  listView = new ListView(this);
  listView.setAdapter(new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, data));
  setContentView(listView);
  OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView<?> parent, View v,
     int position, long arg3) {
    setTitle("您先中的软件是:"+parent.getItemAtPosition(position).toString());
   }

   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
    setTitle("");
   }
  };
  listView.setOnItemSelectedListener(itemSelectedListener);
 }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
    <Button
    android:id="@+id/button1"
 android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="列表一"
    />
    
      
    <Button
    android:id="@+id/button2"
 android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="列表二"
    />
    
    
      
    <Button
    android:id="@+id/button3"
 android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="列表三"
    />
    
    
      
    <Button
    android:id="@+id/button4"
 android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="列表四"
    />
    
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  
  
  <TextView
  android:id="@+id/mview1"
  android:layout_width="100px"
  android:layout_height="wrap_content"
  />
    
  <TextView
  android:id="@+id/mview2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
  
  
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <ListView
  android:id="@id/android:list"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  
  />
    <TextView
  android:id="@id/android:empty"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="对不起,没有数据显示"
  />
  
  
</LinearLayout>




posted on 2011-03-24 19:07  llb988  阅读(1830)  评论(1编辑  收藏  举报

导航