使用以下代码,
排
将创建
根据数据,结果将与此类似
LinearLayout table = (LinearLayout) findViewById(R.id.table);
ArrayList<Tabledata> datalist = new ArrayList<>();
datalist = db.getyourSQLITEdata();
// this method for table title
createtitle(table);
for(int i=0;i<datalist.size();i++){
// this method is creating table rows and filling data
Createtable(table,i);
}
}
// your table title here (if you want title for your table you can use this)
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
void createtitle(LinearLayout main){
LinearLayout row = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 7);
row.setLayoutParams(params);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setWeightSum(100);
// your table title here
String[] title={"title1","title2","title3","title4","title5","title6","title7"};
for (int i = 0; i < 7; i++) {
LinearLayout.LayoutParams textparam;
if(i == 2){
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 20.285);
}else{
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 13.285);
}
TextView col1 = new TextView(this);
col1.setText(title[i]);
col1.setTextSize(13);
col1.setPadding(0,0,0,0);
col1.setLayoutParams(textparam);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
col1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
col1.setBackground(getResources().getDrawable(R.drawable.border,null));
}else{
col1.setBackground(getResources().getDrawable(R.drawable.border));
}
row.addView(col1);
}
main.addView(row);
}
// your data table is created here
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
void Createtable(LinearLayout main,int pos) {
Tabledata data = datalist.get(pos);
// this is where you get data from list and show it in table
String[] yourdata = {data.getdata1(),data.getdata2(),data.getdata3(),data.getdata4(),data.getdata5(),data.getdata6(),data.getdata7()};
LinearLayout row = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 7);
row.setLayoutParams(params);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setWeightSum(100);
for (int i = 0; i < 7; i++) {
LinearLayout.LayoutParams textparam;
if(i == 2){
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 20.285);
}else{
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 13.285);
}
TextView col1 = new TextView(this);
col1.setText(yourdata[i]);
col1.setTextSize(10);
col1.setLines(1);
col1.setPadding(0,0,0,0);
col1.setLayoutParams(textparam);
col1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
col1.setBackground(getResources().getDrawable(R.drawable.border,null));
}else{
col1.setBackground(getResources().getDrawable(R.drawable.border));
}
row.addView(col1);
}
main.addView(row);
}
Xml代码
<LinearLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
</LinearLayout>