ListActivity

来源:互联网 发布:c语言中删除文件 编辑:程序博客网 时间:2024/05/22 05:24

ListActivity简单的说就是ListView和Activity的结合,跟ListView和Activity组合实现的没有什么很大的差别,主要是比较方便。但在实现时,有几点要注意。

            1、ListActivity可以不用setContentView(R.layout.main),它默认是LIstView占满屏。

不用的时候 效果如下:




            2、如果想在屏幕中显示其他控件,如文本框和按钮之类,可以采用如下方法:

                  a、代码中添加:setContentView(R.layout.main)

                  b、在 main.xml 文件中,添加一个LIstView控件,和一个 TextView 控件,注意它们 id 必须为"@id/android:list"、          "@id/android:empty";前一个表示匹配的ListView,后一个表示若LIstView没有内容则显示的提示:代码如下:

用的时候有内容是这样子的:


用的时候没有内容:使用默认
  


代码如下:
xml:
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" 
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" 
  6. android:paddingLeft="8dp"
  7. android:paddingRight="8dp">

  8. <!-- 除了ListView和id为@id/android:empty的view之外,我们还可以任意添加view -->

  9. <TextView 
  10. android:id="@+id/android:title" 
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content" 
  13. android:text="The following is a list:" />

  14. <!-- id为@id/android:list的ListView为客户化的list布局,如果没有,则系统会调用默认的布局 -->

  15. <ListView 
  16. android:id="@id/android:list" 
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent" 
  19. android:background="#00FF00"
  20. android:layout_weight="1" 
  21. android:drawSelectorOnTop="false" />

  22. <!-- 当ListView中没有数据时,id为@id/android:empty的view就会显示出来 -->

  23. <TextView 
  24. android:id="@id/android:empty" 
  25. android:layout_width="match_parent"
  26. android:layout_height="match_parent" 
  27. android:textColor="#FF0000"
  28. android:text="No data" 
  29. android:gravity="center_vertical|center_horizontal" />

  30. </LinearLayout>
java
  1. public class ListActivityDemo extends ListActivity {

  2. /** Called when the activity is first created. */

  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {

  5. super.onCreate(savedInstanceState);

  6. setContentView(R.layout.main);

  7. List<String> items = fillList();

  8. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items);

  9. setListAdapter(adapter);
  10. }


  11. private List<String> fillList() {
  12. List<String> items = new ArrayList<String>();


  13. items.add("星期一");
  14. items.add("星期二");
  15. items.add("星期三");
  16. items.add("星期四");
  17. items.add("星期五");
  18. items.add("星期六");
  19. items.add("星期日");

  20. //items.clear();

  21. return items;

  22. }

  23. }




以下是另一个例子:
 1)main.xml布局文件
<?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">
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <EditText android:id="@+id/et_item" android:layout_width="212px"
            android:layout_height="wrap_content">
        </EditText>
        <Button android:id="@+id/bt_add" android:layout_width="83px"
            android:layout_height="51px" android:text="添加">
        </Button>
    </LinearLayout>
    <ListView android:id="@id/android:list" android:layout_width="fill_parent"
        android:layout_height="0dip" android:layout_weight="1"
        android:drawSelectorOnTop="false" />
    <TextView android:id="@id/android:empty" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Can not find the file!" />

</LinearLayout>

下面是程序截图和代码:



  LIstView Item的布局文件list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content">
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/ItemImage" />
    <TextView android:layout_height="wrap_content"
        android:textSize="20dip" android:layout_width="fill_parent"
        android:id="@+id/ItemTitle" />

</LinearLayout>  


2)代码:

package com.myandroid.test;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListActivityImpl extends ListActivity {
    private Button bt_add;    
    private EditText et_item;
    private ArrayList<HashMap<String, Object>>   listItems;    //存放文字、图片信息
    private SimpleAdapter listItemAdapter;           //适配器    
    @Override
    public void onCreate(Bundle icicle)   {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        bt_add = (Button)findViewById(R.id.bt_add);
        et_item = (EditText)findViewById(R.id.et_item);
        initListView();
        this.setListAdapter(listItemAdapter);  
        bt_add.setOnClickListener(new ClickEvent());    
    }
    /**
     * 设置适配器内容
     */
    private void initListView()   {   
        listItems = new ArrayList<HashMap<String, Object>>();
        for(int i=0;i<10;i++)    {   
            HashMap<String, Object> map = new HashMap<String, Object>();   
            map.put("ItemTitle", "Music: "+i);    //文字
            map.put("ItemImage", R.drawable.music);   //图片   
            listItems.add(map);   
        }   
        //生成适配器的Item和动态数组对应的元素   
        listItemAdapter = new SimpleAdapter(this,listItems,   // listItems数据源    
                R.layout.list_item,  //ListItem的XML布局实现   
                new String[] {"ItemTitle", "ItemImage"},     //动态数组与ImageItem对应的子项          
                new int[ ] {R.id.ItemTitle, R.id.ItemImage}      //list_item.xml布局文件里面的一个ImageView的ID,一个TextView 的ID   
        );   
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)  {
        // TODO Auto-generated method stub
        Log.e("position", "" + position);
        setTitle("你点击第"+position+"行"); 
    }   
    class ClickEvent implements OnClickListener {
        @Override
        public void onClick (View v)  {
            // 向ListView里添加一项
            HashMap<String, Object> map = new HashMap<String, Object>();   
            map.put("ItemTitle", "Music: "+ et_item.getText().toString());   
            map.put("ItemImage", R.drawable.music);     //每次都放入同样的图片资源ID
            listItems.add(map);
            //重新设置适配器
            ListActivityImpl.this.setListAdapter(listItemAdapter);
        }
    }
}

0 0
原创粉丝点击