Android 动态生成表格,使用的是TABLELAYOUT并排序

为了统计作图,做成表格显示,可以上下滑动,并支持排序

 

tablist.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"
    android:background="#FFFFFF" >

    <RelativeLayout
        android:id="@+id/tb_user_left_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:background="#FFffff"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/left_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="60dp"
            android:background="@drawable/bg_tb_left" />

        <TextView
            android:id="@+id/rigth_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/bg_tb_centra" />
    </RelativeLayout>

    <TableLayout
        android:id="@+id/tblColumnsContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tb_user_left_text"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="10dp"
        android:paddingLeft="10dip"
        android:paddingRight="40dip"
        android:paddingTop="20dp"
        android:orientation="vertical"
        android:stretchColumns="*" >
    </TableLayout>

    <ImageView
        android:id="@+id/noinfo_table"
        android:layout_below="@+id/tb_user_left_text"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_noinfo_table"
        android:layout_centerInParent="true"
        />
    <ImageView
        android:id="@+id/pinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tblColumnsContainer"
        android:layout_alignTop="@+id/tblColumnsContainer"
        android:paddingTop="10dp"
        android:src="@drawable/ic_pinner" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tblColumnsContainer" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/tblRowsContainer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="10dp"
                android:orientation="vertical"
                android:paddingLeft="10dip"
                android:paddingRight="40dip"
                android:stretchColumns="*" >
            </TableLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="10dp" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

 


代码 

 

package Demo.Table;

import java.text.NumberFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity{
	
	/** 数量排序 **/
	private Button mBtnQuantity;

	/** 金额排序 **/
	private Button mBtnAmount;

	/** 款号显示 **/
	private TextView mTvModel;

	private Button mBtnModel;
	/** 显示的行数 **/
	private int mRowNumber = 0;

	/**默认图片**/
	private ImageView mImgNoinfotable;
	
	/**回形针**/
	private ImageView mImgPinner;
	
	/** 列头容器 **/
	private TableLayout mTltColumnsContainer;

	/** 填充内容容器 **/
	private TableLayout mTltRowsContainer;

	/** 要显示的内容 **/
	private ArrayList<ArrayList<String>> lists;

	/** 默认为金额正序排列 **/
	private boolean isAupOrdown = true;

	/** 默认为数量正序排列 **/
	private boolean isQupOrdown = true;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tablist);
		mTltColumnsContainer = (TableLayout) this.findViewById(R.id.tblColumnsContainer);
		mTltRowsContainer = (TableLayout) this.findViewById(R.id.tblRowsContainer);
		mImgNoinfotable=(ImageView)this.findViewById(R.id.noinfo_table);
		mImgPinner=(ImageView)this.findViewById(R.id.pinner);
		getdatalist();//得到数据
		doInsertData();//显示数据
	}
	
	private void doInsertData(){
		if (mRowNumber==0) {
			mImgPinner.setVisibility(View.GONE);
			mImgNoinfotable.setVisibility(View.VISIBLE);
		} else {
			mImgPinner.setVisibility(View.VISIBLE);
			mImgNoinfotable.setVisibility(View.GONE);
			addWegits();
			addbutton();
			mBtnAmount.setOnClickListener(mBtnAmountClickListener);
			mBtnQuantity.setOnClickListener(mBtnQuantityClickListener);
		}
	}
	private void getdatalist() {
		
			lists = new ArrayList<ArrayList<String>>();
			ArrayList<String> list;
			NumberFormat numberFormat = NumberFormat.getInstance();
			numberFormat.setMaximumFractionDigits(4);
			double sumBtnAmount = 0;
			double sumBtnQuantity = 0;
			for (int i=1; i<50; i++) {
				sumBtnAmount += i*2;
				sumBtnQuantity += i*3;
			}
			
			for (int i=1; i<50; i++) {
				list = new ArrayList<String>();
				list.add("a"+String.valueOf(i));
				list.add(String.valueOf(i*2));

				list.add(getNumberLength(Double.valueOf(numberFormat.format((float)i*2/(float)sumBtnAmount*100)), 4));
				list.add(String.valueOf(i*3));
				list.add(getNumberLength(Double.valueOf(numberFormat.format((float)i*3/(float)sumBtnQuantity*100)), 4));
				lists.add(list);
			}
			mRowNumber = lists.size();
	}

	private String getNumberLength(double number, int length) {
		String strtn = String.valueOf(number);
		if(number==0.0){
			strtn="0.0";
		}
		if (strtn.indexOf(".") != -1) {
			int leng = length - (strtn.length() - strtn.indexOf(".") - 1);
			if (length > leng) {
				switch (leng) {
				case 0:
					strtn += "%";
					break;
				case 1:
					strtn += "0%";
					break;
				case 2:
					strtn += "00%";
					break;
				case 3:
					strtn += "000%";
					break;
				default:
					strtn = strtn.substring(0, strtn.indexOf(".") + 1 + length)
							+ "%";
					break;
				}
			}
		}
		return strtn;
	}

	private void dataSort(int number, boolean upOrdown) {
		ArrayList<String> couStatistics = new ArrayList<String>();
		for (int i = 1; i < mRowNumber; i++) {
			for (int j = 0; j < mRowNumber - i; j++) {
				if (upOrdown) {
					if (Double.valueOf(lists.get(j).get(number)) < Double
							.valueOf(lists.get(j + 1).get(number))) {
						couStatistics = lists.get(j);
						lists.remove(j);
						lists.add(j + 1, couStatistics);
					}
				} else {
					if (Double.valueOf(lists.get(j).get(number)) > Double
							.valueOf(lists.get(j + 1).get(number))) {
						couStatistics = lists.get(j);
						lists.remove(j);
						lists.add(j + 1, couStatistics);
					}
				}

			}
		}
		updateWegits();
	}

	private void addWegits() {

		mTltRowsContainer.setStretchAllColumns(true);
		TableLayout.LayoutParams tableLparams = new TableLayout.LayoutParams(
				TableLayout.LayoutParams.WRAP_CONTENT,
				TableLayout.LayoutParams.WRAP_CONTENT);
		tableLparams.setMargins(0, 0, 0, 0);
		int intbottom = mRowNumber - 1;
		int inttemp = 0;
		for (int i = 0; i < mRowNumber; i++) {
			TableRow tablerow = new TableRow(this);

			for (int j = 0; j < 5; j++) {
				TextView testview = new TextView(this);
				inttemp += 1;
				testview.setId(inttemp);
				if(j==3){
					testview.setText(lists.get(i).get(j));
				}else{
					testview.setText(lists.get(i).get(j));
				}
				testview.setGravity(Gravity.CENTER);
				testview.setTextSize(20);
				testview.setTextColor(Color.BLACK);
				//

				if (j == 0) {
					testview.setLayoutParams(new android.widget.TableRow.LayoutParams(
							LayoutParams.MATCH_PARENT,
							LayoutParams.WRAP_CONTENT));
				} else {
					testview.setLayoutParams(new android.widget.TableRow.LayoutParams(
							LayoutParams.MATCH_PARENT,
							LayoutParams.WRAP_CONTENT));
				}
				testview.setGravity(Gravity.CENTER);
				testview.setTextSize(20);
				testview.setTextColor(Color.BLACK);
				// testview.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);

				if (i == 0 && j == 0) {
					testview.setBackgroundResource(R.drawable.bg_tbl_row_top_left);
				}
				if (i == 0 && j == 1) {
					testview.setBackgroundResource(R.drawable.bg_tbl_row_top_middle);
				}
				if (i == 0 && j == 2) {
					testview.setBackgroundResource(R.drawable.bg_tbl_row_top_rigth);
				}
				if (i == 0 && j == 3) {
					testview.setBackgroundResource(R.drawable.bg_tbl_row_top_middle);
				}
				if (i == 0 && j == 4) {
					testview.setBackgroundResource(R.drawable.bg_tbl_row_top_rigth);
				}

				if (i > 0 && i < intbottom) {
					if (i % 2 != 0) {
						if (i > 0 && j == 0) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_middle_left);
						}
						if (i > 0 && (j == 1 || j == 3)) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_middle_middle);
						}
						if (i > 0 && (j == 2 || j == 4)) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_middle_rigth);
						}
					} else {
						if (i > 0 && j == 0) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_middle_left01);
						}
						if (i > 0 && (j == 1 || j == 3)) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_middle_middle01);
						}
						if (i > 0 && (j == 2 || j == 4)) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_middle_rigth01);
						}
					}

				}

				if (i == intbottom) {
					if (i % 2 == 0) {
						if (j == 0) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_down_left);
						}
						if (j == 1 || j == 3) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_down_middle);
						}
						if (j == 2 || j == 4) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_down_rigth);
						}
					} else {
						if (j == 0) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_down_left01);
						}
						if (j == 1 || j == 3) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_down_middle01);
						}
						if (j == 2 || j == 4) {
							testview.setBackgroundResource(R.drawable.bg_tbl_row_down_rigth01);
						}
					}

				}

				tablerow.addView(testview);
			}

			mTltRowsContainer.addView(tablerow, tableLparams);
		}
	}
	private void addbutton() {
		mTltColumnsContainer.setStretchAllColumns(true);
		TableLayout.LayoutParams tableLparams = new TableLayout.LayoutParams(
				TableLayout.LayoutParams.WRAP_CONTENT,
				TableLayout.LayoutParams.WRAP_CONTENT);
		tableLparams.setMargins(0, 0, 0, 0);
		TableRow tablerow = new TableRow(this);


		mBtnModel = new Button(this);
		mBtnModel.setId(10086);
		mBtnModel.setLayoutParams(new android.widget.TableRow.LayoutParams(141,
				LayoutParams.WRAP_CONTENT));
		mBtnModel.setText(String.valueOf("款号"));
		mBtnModel.setBackgroundResource(R.drawable.bg_tbl_top_left);
		mBtnModel.setGravity(Gravity.CENTER);
		mBtnModel.setTextSize(20);
		mBtnModel.setTextColor(Color.parseColor("#095583"));
		mBtnModel.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
		Log.v("msg", "tv = " + mBtnModel.getWidth());
		tablerow.addView(mBtnModel);

		mBtnAmount = new Button(this);
		mBtnAmount.setId(10000);
		mBtnAmount.setText(String.valueOf("数量"));
		mBtnAmount.setLayoutParams(new android.widget.TableRow.LayoutParams(
				390, LayoutParams.WRAP_CONTENT));
		mBtnAmount.setBackgroundResource(R.drawable.bg_tbl_top_rigth);
		mBtnAmount.setGravity(Gravity.CENTER);
		mBtnAmount.setTextSize(20);
		mBtnAmount.setTextColor(Color.parseColor("#095583"));
		mBtnAmount.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
		tablerow.addView(mBtnAmount);

		mBtnQuantity = new Button(this);
		mBtnQuantity.setId(10001);
		mBtnQuantity.setText(String.valueOf("金额"));
		mBtnQuantity.setLayoutParams(new android.widget.TableRow.LayoutParams(
				390, LayoutParams.WRAP_CONTENT));
		mBtnQuantity.setBackgroundResource(R.drawable.bg_tbl_top_rigth);
		mBtnQuantity.setGravity(Gravity.CENTER);
		mBtnQuantity.setTextSize(20);
		mBtnQuantity.setTextColor(Color.parseColor("#095583"));
		mBtnQuantity.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);

		tablerow.addView(mBtnQuantity);

		View v = new View(this);
		v.setLayoutParams(new android.widget.TableRow.LayoutParams(0, 0));
		v.setBackgroundColor(Color.RED);
		tablerow.addView(v);
		mTltColumnsContainer.addView(tablerow, tableLparams);
	}

	/**
	 * 排序后重新赋值
	 */
	private void updateWegits() {
		int inttemp = 0;
		for (int i = 0; i < mRowNumber; i++) {
			for (int j = 0; j < 5; j++) {
				inttemp += 1;
				TextView testview = (TextView) mTltRowsContainer
						.findViewById(inttemp);
				if(j==3){
					testview.setText(lists.get(i).get(j));
				}else{
					testview.setText(lists.get(i).get(j));
				}
				
				
			}
		}
	}

	private OnClickListener mBtnAmountClickListener = new OnClickListener() {

		public void onClick(View v) {
			dataSort(1, isAupOrdown);
			isAupOrdown = updateUpOrDown(isAupOrdown);
		}
	};

	private OnClickListener mBtnQuantityClickListener = new OnClickListener() {

		public void onClick(View v) {
			dataSort(3, isQupOrdown);
			isQupOrdown = updateUpOrDown(isQupOrdown);
		}
	};

	/**
	 * 取相反的值
	 * 
	 * @param upOrDown
	 * @return boolean
	 */
	private boolean updateUpOrDown(boolean upOrDown) {
		if (upOrDown) {
			return false;
		} else {
			return true;
		}
	}
}


 

 效果图

 

 如果想要代码那就下载吧,也可以把添加图片的代码给删除了

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值