当前位置:操作系统 > 安卓/Android >>

Android ListView复杂多功能详解

\

 

-------------------------------------------activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
  


>
    </ListView>


</RelativeLayout>list_item.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="wrap_content" >


    <TextView
        android:id="@+id/item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="16sp"
        android:layout_weight="1"
        android:textStyle="bold" />


    <ImageView
        android:id="@+id/item_right"
   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <ImageView
        android:id="@+id/item_right1"
     
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>

 

 

 


------------------------------------------.Main
package com.example.tezt;


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


import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;


public class MainActivity extends
ListActivity {


@Override
public void onCreate(
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


List<Map<String, Object>> datas = getDatas();
ImageAdapter adapter = new ImageAdapter(
this, datas);


this.getListView().setAdapter(
adapter);
}


private List<Map<String, Object>> getDatas() {
// TODO Auto-generated method stub
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
String[] items = this
.getResources()
.getStringArray(
R.array.menu);


for (int i = 0; i < items.length; i++) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("right1",
R.drawable.icon2);
item.put("text", items[i]);
item.put("right",
R.drawable.icon1);


results.add(item);
}


return results;


}


@Override
public boolean onCreateOptionsMenu(
Menu menu) {
getMenuInflater().inflate(
R.menu.activity_main,
menu);
return true;
}
}

 

 

 

---------------------------------------------.ImageAdapter


package com.example.tezt;


import java.util.List;
import java.util.Map;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ImageAdapter extends
BaseAdapter {
private List<Map<String, Object>> datas;


private Context context;


public ImageAdapter(
Context context,
List<Map<String, Object>> datas) {
this.context = context;
this.datas = datas;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return this.datas.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return this.datas.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 holder = null;


if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater
.from(context)
.inflate(
R.layout.list_item,
null);
holder.itemRightImage1 = (ImageView) convertView
.findViewById(R.id.item_right1);
holder.itemText = (TextView) convertView
.findViewById(R.id.item_text);
holder.itemRightImage = (ImageView) convertView
.findViewById(R.id.item_right);


convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView
.getTag();
}


Map<String, Object> item = this.datas
.get(position);


holder.itemRightImage1
.setImageResource(Integer
.parseInt(item
.get("right1")
.toString()));
holder.itemText
.setText(item.get(
"text")
.toString());
holder.itemRightImage
.setImageResource(Integer
.parseInt(item
.get("right")
.toString()));


return convertView;
}


static class ViewHolder {
ImageView itemRightImage1;
TextView itemText;
ImageView itemRightImage;
}


 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,