学习(二)ListView的几种实现方式

来源:互联网 发布:淘宝秋季休闲鞋女鞋 编辑:程序博客网 时间:2024/06/06 03:52

ListView是Android软件开发中非常重要组件之一,说道ListView就不得不说Adapter适配器,因为只有通过Adapter才可以把列表中的数据映射到ListView中。
在android的开发中最Adapter 一共可以分为
ArrayAdapter<T>,
BaseAdapter, 
CursorAdapter,
HeaderViewListAdapter, 
ResourceCursorAdapter,
SimpleAdapter,
SimpleCursorAdapter,
WrapperListAdapter

软件开发中最常用的有ArrayAdapter<T>, BaseAdapter, SimpleAdapter,接下下来讲介绍如何使用ListView控件。

下面是ListView的几种实现

public class StudyActivityextends BaseActivity implements OnClickListener{


private TextViewtitle;

private ImageViewmoreImageView;

private ListViewlistView;

private ArrayList<String>arrayList;

private ArrayList<Map<String, Object>>arrayListSub;

private MyListAdaptermyListAdapter = null;

@SuppressLint("DefaultLocale")

@Override

protectedvoid onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_study);

title = (TextView) findViewById(R.id.title);

title.setText("学习");

moreImageView = (ImageView)findViewById(R.id.more);

moreImageView.setOnClickListener(this);

arrayList =new ArrayList<String>();

for(int i = 0; i < 100; i++ )

{

String tempStr = String.format("第%1$d章", i+1);

arrayList.add(i, tempStr);

}

arrayListSub =new ArrayList<Map<String,Object>>();

for(int j = 0; j < 100; j++ )

{

Map<String, Object> item = new HashMap<String, Object>();

String tempStr1 = String.format("第%1$d章", j+1);

String tempStr2 = String.format("子标题说明%1$d", j+1);

item.put("image", R.drawable.default_pic);

item.put("title", tempStr1);

item.put("text", tempStr2);

arrayListSub.add(item);

}

listView = (ListView)findViewById(R.id.listView);

//第一种:简单的ListView

// ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, 

// android.R.layout.simple_expandable_list_item_1, arrayList);

//第二种:带标题的ListView

// SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayListSub, 

// android.R.layout.simple_expandable_list_item_2, 

// new String[]{"title","text"}, 

// new int[]{android.R.id.text1,android.R.id.text2});

//第三种:带图片的ListView

// SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayListSub, 

// R.layout.mystudylistview, 

// new String[]{"image","title","text"}, 

// new int[]{R.id.image_study,R.id.title_study,R.id.text_study});

//第四种:自定义布局BaseAdapter

myListAdapter =new MyListAdapter(arrayListSub,this);

listView.setAdapter(myListAdapter);

}

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

switch(arg0.getId())

{

case R.id.more:

// showToast("点击了右侧按钮");

Intent intent_subject = new Intent(this, SubjectAndTextBookActivity.class);

startActivity(intent_subject);

break;

default:

break;

}

}


@Override

public void refresh(Object... param) {

// TODO Auto-generated method stub 

super.refresh(param);

}


@Override

protectedvoid onResume() {

// TODO Auto-generated method stub

super.onResume();

SharedPreferences sp = getSharedPreferences("GRADE_SUB_PUB",MODE_PRIVATE);

        //读取数据

String grade = sp.getString("GRADE_KEY",null);

String subject = sp.getString("SUBJECT_KEY", null);

String publicatation = sp.getString("PUBLICATION_KEY",null);

showToast(grade);


}

//自定义Adapter

class MyListAdapterextends BaseAdapter{

private LayoutInflaterinflater;

private ArrayList<Map<String, Object>>theList;


public MyListAdapter(ArrayList<Map<String, Object>> list, Context context)

{

super();

inflater = LayoutInflater.from(context);

theList = list;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return (theList ==null) ? 0 : theList.size();

}


@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return theList.get(position);

}


@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}


@Override

public View getView(int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

ViewHolder viewHolder;  

       

        if (convertView ==null) {  

        convertView =inflater.inflate(R.layout.mystudylistview,null); 

       

        viewHolder =new ViewHolder();

viewHolder.titleView = (TextView)convertView.findViewById(R.id.title_study);

viewHolder.text = (TextView)convertView.findViewById(R.id.text_study);

viewHolder.image = (ImageView)convertView.findViewById(R.id.image_study);

convertView.setTag(viewHolder);

        } 

        else{

        viewHolder = (ViewHolder)convertView.getTag();

        }

       

        Map<String, Object> map = (Map<String, Object>)theList.get(position);

       

        String a = map.get("title") ==null ? "0" : map.get("title").toString();

        Log.i("heloo", a);

        viewHolder.titleView.setText(a);

       

        String b = map.get("text") ==null ? "0" : map.get("text").toString();

        viewHolder.text.setText(b);

       

        viewHolder.image.setImageResource(R.drawable.default_pic);  

         

       

return convertView;

}

}

class ViewHolder

{

public TextViewtitleView;

public TextViewtext;

public ImageViewimage;

}

}


下面是ListView布局文件

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#666666"

    android:orientation="vertical">

    

    

  <include

        layout="@layout/title"/>

 

  <ListView 

      android:id="@+id/listView"

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:background="#666666"

      android:scrollbars="none"

      android:listSelector="@color/blue"

      />

 

</LinearLayout>



<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content">


    <ImageView

        android:id="@+id/image_study"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:padding="2dip"

        android:scaleType="fitCenter"

        android:src="@drawable/default_pic"/>


    <TextView

        android:id="@+id/title_study"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_toRightOf="@+id/image_study"

        android:gravity="center_vertical"

        android:text="asdfadsf"

        android:textColor="#ffffff"

        android:textSize="20dip"/>


    <TextView

        android:id="@+id/text_study"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/title_study"

        android:layout_toRightOf="@+id/image_study"

        android:gravity="center_vertical"

        android:singleLine="true"

        android:text="adsfkldjskf"

        android:textSize="15dip"/>


</RelativeLayout>


最后贴一张运行截图


0 0