首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

ProgressBar跟ListView的使用方法

2012-09-28 
ProgressBar和ListView的使用方法1.ListView(先继承ListActivity) ? ?它以列表的形式展示具体内容,并且能

ProgressBar和ListView的使用方法

1.ListView(先继承ListActivity) ? ?它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。在Android中是非常重要的数据列表显示控件,包括我们的电话薄、电话记录等都有使用到ListView控件。

b) ?ListView显示的四个步骤一般为:

      i. ? ?定义每个子项(Item)的样式。

      ii. ? 定义数据源,也就是我们要显示的文字、图片或者其他一些信息。

      iii. ?定义适配器,并为其指定数据(如何把数据映射到ListView中)。

      iv. ?为ListView控件绑定适配器。

?必须调用setListAdapter方法

?

SimpleAdapter 是一个系统已经实现好的类,主要用作把我们在XML文件中定义好的静态数据呈现在UI上。

new一个SimpleAdapter参数一次是:

  1. this——适配器的上下文参数
  2. ? ?? ?? ?*list——数据源
  3. ? ?? ?? ?*R.layout.*——ListView的子项模式
  4. ? ?? ?? ?*new String[]{*,*}——在数据源list中对应的绑定项,lie列的名字
  5. ? ?? ?? ?*new int[]{*,*}——ListView子项中对应的TextView的ID号,这个必须和前一个new String[]{*,*}中的参数对应,否则绑定将失败

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <LinearLayout         android:id="@+id/listlinearlayout"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >        <ListView             android:id="@id/android:list"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:drawSelectorOnTop="false"            android:scrollbars="vertical"              ></ListView>         </LinearLayout> </LinearLayout><!-- android:scrollbars="vertical"  滚动条垂直方向 -->

?user.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal"     android:paddingLeft="10dip"    android:paddingTop="1dip"    android:paddingRight="10dip"    android:paddingBottom="1dip"    >    <TextView         android:id="@+id/name"        android:layout_width="180dip"        android:layout_height="30dip"        android:textSize="10dip"        android:singleLine="true"        />    <TextView         android:id="@+id/ip"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:gravity="right"        android:textSize="10dip"         />   </LinearLayout>
?

java

package com.jianglin.ListView;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.app.ListActivity;import android.os.Bundle;import android.widget.SimpleAdapter;public class ListViewTestActivity extends ListActivity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        ArrayList<HashMap<String,String>> list = new  ArrayList<HashMap<String,String>>();        HashMap<String,String> map1=new HashMap<String,String>();        HashMap<String,String> map2=new HashMap<String,String>();        HashMap<String,String> map3=new HashMap<String,String>();        map1.put("name", "lihao");        map1.put("id", "1");        map2.put("name", "zhangsan");        map2.put("id", "2");        map3.put("name", "lisi");        map3.put("id", "3");        list.add(map1);        list.add(map2);        list.add(map3);                SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user,         new String[]{"name","id"},new int[]{R.id.name,R.id.ip});        setListAdapter(listAdapter);          }   //如果需要实现点击效果就需要 onListItemClick() 事件}
?

?

2.ProgressBar(进度条)

?

public class ProgressBarTestActivity extends Activity {private ProgressBar firstBar = null;private ProgressBar secondBar = null;private Button myButton = null;private int i = 0;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        firstBar = (ProgressBar)findViewById(R.id.firstBar);        secondBar = (ProgressBar)findViewById(R.id.secondBar);        myButton = (Button)findViewById(R.id.myButton);        myButton.setOnClickListener(new ButronListenter());                  }    class ButronListenter implements OnClickListener{@Overridepublic void onClick(View v) {if(i == 0){firstBar.setVisibility(View.VISIBLE);  //设置进度条可见状态secondBar.setVisibility(View.VISIBLE);}else if (i < firstBar.getMax()){firstBar.setProgress(i); //设置第一进度条位置多少firstBar.setSecondaryProgress(i+10);//设置第二进度条位置//secondBar.setProgress(i); 默认进度条不显示进度}else{firstBar.setVisibility(View.GONE); //设置进度条不可见secondBar.setVisibility(View.GONE);}i = i + 10;}    }}

?main.xml

?

    <ProgressBar         android:id="@+id/firstBar"        style="?android:attr/progressBarStyleHorizontal"           android:layout_width="200dp"        android:layout_height="wrap_content"        android:visibility="gone"          /><ProgressBar     android:id="@+id/secondBar"    style="?android:attr/progressBarStyle"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:visibility="gone"    /><Button     android:id="@+id/myButton"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="begin"    />

热点排行