listview显示数据,类似HTML那样的table显示。横屏可以拖拉

来源:互联网 发布:linux启动smb服务 编辑:程序博客网 时间:2024/05/21 15:41
报表显示
listview显示数据,类似HTML那样的table显示。显示不足的时候,横屏可以拖拉。Activity 包括数据

table.jpg
欢迎大家出来交流交流

package com.jdjw.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;
import com.jdjw.test.TableAdapter.TableCell;
import com.jdjw.test.TableAdapter.TableRow;
public class TestGridViewTableActivity extends Activity
{
        private ListView lvSaleNum;
        private List<TableRow> table;
        private Context mContext;
        @Override
        protected void onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                setContentView(R.layout.test_gridview_table);
                lvSaleNum = (ListView)findViewById(R.id.lv_salesNum);
                mContext = this;
                data();
        }
        private void data(){               
                salesNum s1 = new salesNum("星期一",50,60,R.drawable.a0,60,80,R.drawable.a1);
                salesNum s2 = new salesNum("星期二",50,60,R.drawable.a0,10,80,R.drawable.a0);
                salesNum s3 = new salesNum("星期三",120,30,R.drawable.a1,30,80,R.drawable.a0);
                salesNum s4 = new salesNum("星期四",50,50,R.drawable.a0,80,80,R.drawable.a1);
                salesNum s5 = new salesNum("星期五",10,20,R.drawable.a0,62,60,R.drawable.a0);
                salesNum s6 = new salesNum("星期六",80,10,R.drawable.a1,90,80,R.drawable.a0);
                salesNum s7 = new salesNum("星期日",50,90,R.drawable.a0,50,30,R.drawable.a0);
                List<salesNum> data = new ArrayList<salesNum>();
                data.add(s1);
                data.add(s2);
                data.add(s3);
                data.add(s4);
                data.add(s5);
                data.add(s6);
                data.add(s7);
               
               
                int width = getWindowManager().getDefaultDisplay().getWidth()/7;
                table = new ArrayList<TableRow>();
               
               
                int height=30;//LayoutParams.FILL_PARENT;
               
               
                TableCell[] titleCells = new TableCell[7];
                List<TableCell> titleList = new ArrayList<TableCell>();
               
                TableCell title1 = new TableCell("时间",width + 8 ,height, TableCell.TEXT);//第一格
                titleList.add(title1);
                TableCell title2 = new TableCell("上月条码数",width + 8 ,height, TableCell.TEXT);//第一格
                titleList.add(title2);
                TableCell title3 = new TableCell("本月条码数",width + 8 ,height, TableCell.TEXT);//第一格
                titleList.add(title3);
                TableCell title4 = new TableCell("条码趋势",width + 8 ,height, TableCell.TEXT);//第一格
                titleList.add(title4);
                TableCell title5 = new TableCell("上月销售金额",width + 8 ,height, TableCell.TEXT);//第一格
                titleList.add(title5);
                TableCell title6 = new TableCell("本月销售金额",width + 8 ,height, TableCell.TEXT);//第一格
                titleList.add(title6);
                TableCell title7 = new TableCell("销售金额趋势",width + 8 ,height, TableCell.TEXT);//第一格
                titleList.add(title7);
                table.add(new TableRow(titleList.toArray(titleCells)));
               
                for(int i=0;i<data.size();i++){
                        salesNum sales = data.get(i);
                        TableCell[] cells = new TableCell[7];
                        List<TableCell> cellsList = new ArrayList<TableCell>();
                        TableCell cell1 = new TableCell(sales.getWeek(),width + 8 ,height, TableCell.TEXT);//第1格
                        cellsList.add(cell1);
                        TableCell cell2 = new TableCell(sales.getBackMonthNum(),width + 8 ,height, TableCell.TEXT);//第2格
                        cellsList.add(cell2);
                        TableCell cell3 = new TableCell(sales.getNextMonthNum(),width + 8 ,height, TableCell.TEXT);//第3格
                        cellsList.add(cell3);
                        TableCell cell4 = new TableCell(sales.getNumImage(),width + 8 ,height, TableCell.IMAGE);//第4格
                        cellsList.add(cell4);
                        TableCell cell5 = new TableCell(sales.getBackMonthMomey(),width + 8,height, TableCell.TEXT);
                        cellsList.add(cell5);
                        TableCell cell6 = new TableCell(sales.getNextMonthMomey(),width + 8 ,height, TableCell.TEXT);
                        cellsList.add(cell6);
                        TableCell cell7 = new TableCell(sales.getMomeyImage(),width + 8 ,height, TableCell.IMAGE);
                        cellsList.add(cell7);
                        table.add(new TableRow(cellsList.toArray(cells)));
                }

                //设置适配器
                TableAdapter adapter = new TableAdapter(mContext,table);
                lvSaleNum.setAdapter(adapter);
        }
        
        
        class salesNum{
                private String week;
                private int backMonthNum;
                private int nextMonthNum;
                private int numImage;
                private int backMonthMomey;
                private int nextMonthMomey;
                private int momeyImage;
                public salesNum(){}
                public salesNum(String week,int backMonthNum,int nextMonthNum,int numImage,int backMonthMomey,int nextMonthMomey,int momeyImage){
                        this.week = week;
                        this.backMonthNum = backMonthNum;
                        this.nextMonthNum = nextMonthNum;
                        this.numImage = numImage;
                        this.backMonthMomey = backMonthMomey;
                        this.nextMonthMomey = nextMonthMomey;
                        this.momeyImage = momeyImage;
                        
                }
                public String getWeek()
                {
                        return week;
                }
                public void setWeek(String week)
                {
                        this.week = week;
                }
                public int getBackMonthNum()
                {
                        return backMonthNum;
                }
                public void setBackMonthNum(int backMonthNum)
                {
                        this.backMonthNum = backMonthNum;
                }
                public int getNextMonthNum()
                {
                        return nextMonthNum;
                }
                public void setNextMonthNum(int nextMonthNum)
                {
                        this.nextMonthNum = nextMonthNum;
                }
                public int getNumImage()
                {
                        return numImage;
                }
                public void setNumImage(int numImage)
                {
                        this.numImage = numImage;
                }
                public int getBackMonthMomey()
                {
                        return backMonthMomey;
                }
                public void setBackMonthMomey(int backMonthMomey)
                {
                        this.backMonthMomey = backMonthMomey;
                }
                public int getNextMonthMomey()
                {
                        return nextMonthMomey;
                }
                public void setNextMonthMomey(int nextMonthMomey)
                {
                        this.nextMonthMomey = nextMonthMomey;
                }
                public int getMomeyImage()
                {
                        return momeyImage;
                }
                public void setMomeyImage(int momeyImage)
                {
                        this.momeyImage = momeyImage;
                }
               
        }
}

适配器
package com.jdjw.test;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
* ListView自适应实现Table的类TableAdapter.java代码如下:
*
* PS:TableCell是格单元的类,TableRow是表格行的类,TableRowView是实现表格行的组件。 实现步骤:TableCell -->
* TableRow(TableRowView)-->ListView
*
*
*/
public class TableAdapter extends BaseAdapter
{
        private Context context;
        private List<TableRow> table; // 7行7列

        public TableAdapter(Context context, List<TableRow> table)
        {
                this.context = context;
                this.table = table;
        }

        @Override
        public int getCount()
        {
                return table.size();
        }

        @Override
        public long getItemId(int position)
        {
                return position;
        }

        public TableRow getItem(int position)
        {
                return table.get(position);
        }

        int i = 0;

        public View getView(int position, View convertView, ViewGroup parent)
        {
                TableRow tableRow = null;
                tableRow = table.get(position);// 得到行
                TableRowView view;
                if (convertView == null)
                {
                        view = new TableRowView(this.context, tableRow);
                        convertView = view;
                }
                return convertView;
        }

        /**
         * TableRowView 实现表格行的样式
         *
         * @author hellogv
         */
        class TableRowView extends LinearLayout
        {
                public TableRowView(Context context, TableRow tableRow)
                {
                        super(context);
                        this.setOrientation(LinearLayout.HORIZONTAL);
                        for (int i = 0; i < tableRow.getSize(); i++)
                        {// 逐个格单元添加到行
                                TableCell tableCell = tableRow.getCellValue(i);
                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                                                tableCell.width, tableCell.height);// 按照格单元指定的大小设置空间
                                layoutParams.setMargins(0, 0, 1, 1);// 预留空隙制造边框
                                if (tableCell.type == TableCell.TEXT)
                                {// 如果格单元是文本内容
                                        TextView textCell = new TextView(context);
                                        textCell.setLines(1);
                                        textCell.setGravity(Gravity.CENTER);
                                        // textCell.setBackgroundColor(Color.BLACK);//背景黑色
                                        textCell.setText(String.valueOf(tableCell.value));
                                        addView(textCell, layoutParams);
                                }
                                else if (tableCell.type == TableCell.IMAGE)
                                {// 如果格单元是图像内容
                                        ImageView imgCell = new ImageView(context);
                                        // imgCell.setBackgroundColor(Color.BLACK);//背景黑色
                                        imgCell.setImageResource((Integer) tableCell.value);
                                        addView(imgCell, layoutParams);
                                }
                        }
                        // this.setBackgroundColor(Color.WHITE);//背景白色,利用空隙来实现边框
                }
        }

        /**
         * TableRow 实现表格的行
         *
         * @author hellogv
         */
        static public class TableRow
        {
                private TableCell[] cell;

                public TableRow(TableCell[] cell)
                {
                        this.cell = cell;
                }

                public int getSize()
                {
                        return cell.length;
                }

                public TableCell getCellValue(int index)
                {
                        if (index >= cell.length)
                                return null;
                        return cell[index];
                }
        }

        /**
         * TableCell 实现表格的格单元
         *
         * @author hellogv
         */
        static public class TableCell
        {
                static public final int TEXT = 0;
                static public final int IMAGE = 1;
                public Object value;
                public int width;
                public int height;
                private int type;

                public TableCell(Object value, int width, int height, int type)
                {
                        this.value = value;
                        this.width = width;
                        this.height = height;
                        this.type = type;
                }
        }
}

xml 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<!-- 添加横向滑动 设置fillviewport为truey意思是否将HorizontalScrollView的内容宽度拉伸以适应视口-->
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"  
android:fillViewport="true"   
     >
<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/lv_salesNum"
   
     />
    </HorizontalScrollView>
</LinearLayout>
原创粉丝点击