Android 以XML或编程方式创建表列表
我目前正在Android页面上建立一个数据库驱动的结果列表。不过,我已经读到,您应该用XML构建内容视图,我已经这样做了。我的问题是,我真的应该构建10个空行并动态填充它们,还是应该通过在Java中构建视图来添加这些行?稍后我可能会添加更多行 我显示的数据如下所示:Android 以XML或编程方式创建表列表,android,xml,Android,Xml,我目前正在Android页面上建立一个数据库驱动的结果列表。不过,我已经读到,您应该用XML构建内容视图,我已经这样做了。我的问题是,我真的应该构建10个空行并动态填充它们,还是应该通过在Java中构建视图来添加这些行?稍后我可能会添加更多行 我显示的数据如下所示: 右侧的控件增加和减少分配给技能的点数,因此列表/行必须启用某种类型的用户控件。尝试使用ListView 我认为只能通过编程方式添加行 检查这里 ` ` 我认为这段代码会对您有所帮助。您希望使用游标适配器和列表视图来显示数据。下面
右侧的控件增加和减少分配给技能的点数,因此列表/行必须启用某种类型的用户控件。尝试使用ListView 我认为只能通过编程方式添加行 检查这里 ` `
我认为这段代码会对您有所帮助。您希望使用游标适配器和列表视图来显示数据。下面是Android文档中的一个示例。
这将使您显示的列表与数据库保持同步(如果添加或删除了内容),如果需要复杂的布局,您可以为XML中的行设计项目模板。以下是整个程序
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.*;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.*;
import android.widget.TableLayout.LayoutParams;
import android.os.Bundle;
import org.garret.perst.*;
public class RetailerTable extends Activity implements OnClickListener, OnFocusChangeListener {
public static LinearLayout mainLayout;
public static ScrollView scrollview;
public static HorizontalScrollView Hscrollview;
public static TableLayout tableLayout;
public TableRow tableRow;
public CheckBox checkbox;
public static int oldView = 0;
public int rowid = 0, colid = 0;
public int oldColor = 0;
public static int retailerId = 0;
public final int COLNO = 7;
public final String DATABASE_NAME="AIRTEL.db";
public final String TABLE_NAME="Retailer";
// database object
public DataHelper datahelper;
final static int pagePoolSize = 2 * 1024 * 1024;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scrollview = new ScrollView(this);
Hscrollview = new HorizontalScrollView(this);
tableLayout = new TableLayout(this);
//database code
try{
datahelper=new DataHelper(this,DATABASE_NAME,TABLE_NAME );
Retailer[] retailer=new Retailer[500];
retailer=datahelper.selectAll();
for(int i=0;i<retailer.length;i++)
{
CreateRetailerTable(retailer[i], i);
}
}catch(Exception e){
Toast.makeText(this,"Error"+ e.getMessage(), Toast.LENGTH_LONG).show();
}
// insertRetailer();
// fetchRetailer();
Hscrollview.addView(tableLayout);
scrollview.addView(Hscrollview);
setContentView(scrollview);
//TextView Textview = new TextView(this);
// Textview = (TextView) scrollview.findViewById(10);
// Toast.makeText(this, Textview.getText(), Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Import");
menu.add(0, 1, 0, "ADD");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
showMsg("Import");
break;
case 1:
//showMsg("Add");
startActivity(new Intent(RetailerTable.this,RetailerForm.class));
break;
}
return super.onOptionsItemSelected(item);
}
private void showMsg(String msg) {
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2);
toast.show();
}
public void CreateRetailerTable(Retailer ret, int i) {
try {
LinearLayout.LayoutParams params = new LayoutParams(
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(1, 1, 1, 1);
tableRow = new TableRow(this);
tableRow.setOnClickListener(this);
tableRow.setId(rowid);
// Column-1
rowid++;
mainLayout = new LinearLayout(this);
TextView txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(String.valueOf(ret.ID));
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// Column-2
rowid++;
mainLayout = new LinearLayout(this);
txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(ret.Name);
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// Column-3
rowid++;
mainLayout = new LinearLayout(this);
txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(ret.phone);
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// Column-4
rowid++;
mainLayout = new LinearLayout(this);
txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(ret.FosNo);
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// Column-5
rowid++;
mainLayout = new LinearLayout(this);
txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(ret.FosName);
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// Column-6
rowid++;
mainLayout = new LinearLayout(this);
txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(ret.Remarks);
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// column -7
rowid++;
mainLayout = new LinearLayout(this);
CheckBox cb = new CheckBox(this);
if (i % 2 == 0) {
cb.setBackgroundResource(R.color.white);
} else {
cb.setBackgroundResource(R.color.LightPink);
}
cb.setTextColor(Color.BLACK);
cb.setHeight(25);
cb.setId(rowid);
// cb.setText("Cell=" + (i + "" + 2));
cb.setOnClickListener(this);
cb.setOnFocusChangeListener(this);
if (ret.enable.equalsIgnoreCase("TRUE")) {
cb.setChecked(true);
} else {
cb.setChecked(false);
}
mainLayout.addView(cb, params);
tableRow.addView(mainLayout);
tableLayout.addView(tableRow);
} catch (Exception e) {
Toast.makeText(this,"error in createtable"+ e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View view) {
int selRow = 0;
int prevselRow = 0;
if (view.getId() % COLNO != 0) {
selRow = (view.getId() / COLNO) + 1;
} else {
selRow = (view.getId() / COLNO);
}
int firstCol = ((selRow - 1) * COLNO) + 1;
if (oldView != 0) {
if (oldView % 7 != 0) {
prevselRow = (oldView / COLNO) + 1;
} else {
prevselRow = (oldView / COLNO);
}
int prevfirstCol = ((prevselRow - 1) * COLNO) + 1;
TextView tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 1);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 2);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 3);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 4);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 5);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 6);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
}
// new Row
int color = Color.GRAY;
oldView = view.getId();
String result = new String();
TextView tv = new TextView(this);
tv = (TextView) findViewById(firstCol);
result = tv.getText().toString();
oldColor = tv.getDrawingCacheBackgroundColor();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 1);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 2);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 3);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 4);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 5);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 6);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
}
@Override
public void onFocusChange(View view, boolean hasFocus) {
int selRow = 0;
int prevselRow = 0;
if (hasFocus == true) {
if (view.getId() % COLNO != 0) {
selRow = (view.getId() / COLNO) + 1;
} else {
selRow = (view.getId() / COLNO);
}
int firstCol = ((selRow - 1) * COLNO) + 1;
if (oldView != 0) {
if (oldView % 7 != 0) {
prevselRow = (oldView / COLNO) + 1;
} else {
prevselRow = (oldView / COLNO);
}
int prevfirstCol = ((prevselRow - 1) * COLNO) + 1;
TextView tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 1);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 2);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 3);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 4);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 5);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 6);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
}
// new Row
int color = Color.GRAY;
oldView = view.getId();
String result = new String();
TextView tv = new TextView(this);
tv = (TextView) findViewById(firstCol);
result = tv.getText().toString();
oldColor = tv.getDrawingCacheBackgroundColor();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 1);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 2);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 3);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 4);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 5);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 6);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
// Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
}
public void insertRetailer() {
}
public void fetchRetailer() {
}
}
导入android.app.Activity;
导入android.content.Intent;
导入android.graphics.Color;
导入android.view.*;
导入android.view.MenuItem.OnMenuItemClickListener;
导入android.view.view.OnClickListener;
导入android.view.view.OnFocusChangeListener;
导入android.widget.*;
导入android.widget.TableLayout.LayoutParams;
导入android.os.Bundle;
导入org.garret.perst.*;
公共类RetailerTable extends活动实现OnClickListener、OnFocusChangeListener{
公共静态线性布局主布局;
公共静态滚动视图滚动视图;
公共静态水平滚动视图Hscrollview;
公共静态表格布局;
公共桌行;
公共复选框;
公共静态int oldView=0;
public int rowid=0,colid=0;
公共int oldColor=0;
公共静态int retailerId=0;
公共最终int COLNO=7;
公共最终字符串数据库\u NAME=“AIRTEL.db”;
公共最终字符串表\u NAME=“零售商”;
//数据库对象
公共数据助手数据助手;
最终静态int pagePoolSize=2*1024*1024;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
scrollview=新的scrollview(此);
Hscrollview=新的水平滚动视图(此);
tableLayout=新的tableLayout(本);
//数据库代码
试一试{
datahelper=新的datahelper(这个,数据库名称,表名称);
零售商[]零售商=新零售商[500];
零售商=datahelper.selectAll();
对于(int i=0;i
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:scrollbars="vertical" android:background="#ffffff"
android:scrollbarAlwaysDrawHorizontalTrack="true">
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarAlwaysDrawHorizontalTrack="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ffffff">
<TableRow android:background="#000000"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_margin="1dip">
<TextView android:textColor="#000000" android:text="Name"
android:background="#00CCFF" android:layout_margin="1dip" android:textStyle="bold"/>
<TextView android:textColor="#000000" android:text="Phone No."
android:background="#00CCFF" android:layout_margin="1dip" android:textStyle="bold"/>
<TextView android:textColor="#000000" android:text="City"
android:background="#00CCFF" android:layout_margin="1dip" android:textStyle="bold"/>
<TextView android:textColor="#000000" android:text="Area-1"
android:background="#00CCFF" android:layout_margin="1dip" android:textStyle="bold"/>
<TextView android:textColor="#000000" android:text="Area-2"
android:background="#00CCFF" android:layout_margin="1dip" android:textStyle="bold"/>
</TableRow>
<!-- DATA -->
<TableRow android:background="#000000"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_margin="1dip">
<TextView android:textColor="#000000" android:text="Rameshbhai"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="983874838"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="Rajkot"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="RaiyaRoad"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="Bhaktinagar Circle"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
</TableRow>
<TableRow android:background="#000000"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_margin="1dip">
<TextView android:textColor="#000000" android:text="Hirenbhai"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip" />
<TextView android:textColor="#000000" android:text="903839388"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="Ahmedabad"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="Satellite"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="Maninagar"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
</TableRow>
<TableRow android:background="#000000"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_margin="1dip">
<TextView android:textColor="#000000" android:text="Rameshbhai"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="983874838"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="Rajkot"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="RaiyaRoad"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
<TextView android:textColor="#000000" android:text="Bhaktinagar Circle"
android:background="#ffffff" android:layout_margin="1dip" android:padding="3dip"/>
</TableRow>
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.*;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.*;
import android.widget.TableLayout.LayoutParams;
import android.os.Bundle;
import org.garret.perst.*;
public class RetailerTable extends Activity implements OnClickListener, OnFocusChangeListener {
public static LinearLayout mainLayout;
public static ScrollView scrollview;
public static HorizontalScrollView Hscrollview;
public static TableLayout tableLayout;
public TableRow tableRow;
public CheckBox checkbox;
public static int oldView = 0;
public int rowid = 0, colid = 0;
public int oldColor = 0;
public static int retailerId = 0;
public final int COLNO = 7;
public final String DATABASE_NAME="AIRTEL.db";
public final String TABLE_NAME="Retailer";
// database object
public DataHelper datahelper;
final static int pagePoolSize = 2 * 1024 * 1024;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scrollview = new ScrollView(this);
Hscrollview = new HorizontalScrollView(this);
tableLayout = new TableLayout(this);
//database code
try{
datahelper=new DataHelper(this,DATABASE_NAME,TABLE_NAME );
Retailer[] retailer=new Retailer[500];
retailer=datahelper.selectAll();
for(int i=0;i<retailer.length;i++)
{
CreateRetailerTable(retailer[i], i);
}
}catch(Exception e){
Toast.makeText(this,"Error"+ e.getMessage(), Toast.LENGTH_LONG).show();
}
// insertRetailer();
// fetchRetailer();
Hscrollview.addView(tableLayout);
scrollview.addView(Hscrollview);
setContentView(scrollview);
//TextView Textview = new TextView(this);
// Textview = (TextView) scrollview.findViewById(10);
// Toast.makeText(this, Textview.getText(), Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Import");
menu.add(0, 1, 0, "ADD");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
showMsg("Import");
break;
case 1:
//showMsg("Add");
startActivity(new Intent(RetailerTable.this,RetailerForm.class));
break;
}
return super.onOptionsItemSelected(item);
}
private void showMsg(String msg) {
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2);
toast.show();
}
public void CreateRetailerTable(Retailer ret, int i) {
try {
LinearLayout.LayoutParams params = new LayoutParams(
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(1, 1, 1, 1);
tableRow = new TableRow(this);
tableRow.setOnClickListener(this);
tableRow.setId(rowid);
// Column-1
rowid++;
mainLayout = new LinearLayout(this);
TextView txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(String.valueOf(ret.ID));
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// Column-2
rowid++;
mainLayout = new LinearLayout(this);
txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(ret.Name);
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// Column-3
rowid++;
mainLayout = new LinearLayout(this);
txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(ret.phone);
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// Column-4
rowid++;
mainLayout = new LinearLayout(this);
txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(ret.FosNo);
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// Column-5
rowid++;
mainLayout = new LinearLayout(this);
txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(ret.FosName);
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// Column-6
rowid++;
mainLayout = new LinearLayout(this);
txtBox = new TextView(this);
if (i % 2 == 0) {
txtBox.setBackgroundResource(R.color.white);
} else {
txtBox.setBackgroundResource(R.color.LightPink);
}
txtBox.setTextColor(Color.BLACK);
txtBox.setHeight(25);
txtBox.setPadding(3, 3, 3, 3);
txtBox.setId((rowid));
txtBox.setText(ret.Remarks);
txtBox.setOnClickListener(this);
txtBox.setOnFocusChangeListener(this);
mainLayout.addView(txtBox, params);
tableRow.addView(mainLayout);
// column -7
rowid++;
mainLayout = new LinearLayout(this);
CheckBox cb = new CheckBox(this);
if (i % 2 == 0) {
cb.setBackgroundResource(R.color.white);
} else {
cb.setBackgroundResource(R.color.LightPink);
}
cb.setTextColor(Color.BLACK);
cb.setHeight(25);
cb.setId(rowid);
// cb.setText("Cell=" + (i + "" + 2));
cb.setOnClickListener(this);
cb.setOnFocusChangeListener(this);
if (ret.enable.equalsIgnoreCase("TRUE")) {
cb.setChecked(true);
} else {
cb.setChecked(false);
}
mainLayout.addView(cb, params);
tableRow.addView(mainLayout);
tableLayout.addView(tableRow);
} catch (Exception e) {
Toast.makeText(this,"error in createtable"+ e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View view) {
int selRow = 0;
int prevselRow = 0;
if (view.getId() % COLNO != 0) {
selRow = (view.getId() / COLNO) + 1;
} else {
selRow = (view.getId() / COLNO);
}
int firstCol = ((selRow - 1) * COLNO) + 1;
if (oldView != 0) {
if (oldView % 7 != 0) {
prevselRow = (oldView / COLNO) + 1;
} else {
prevselRow = (oldView / COLNO);
}
int prevfirstCol = ((prevselRow - 1) * COLNO) + 1;
TextView tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 1);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 2);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 3);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 4);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 5);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 6);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
}
// new Row
int color = Color.GRAY;
oldView = view.getId();
String result = new String();
TextView tv = new TextView(this);
tv = (TextView) findViewById(firstCol);
result = tv.getText().toString();
oldColor = tv.getDrawingCacheBackgroundColor();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 1);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 2);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 3);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 4);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 5);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 6);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
}
@Override
public void onFocusChange(View view, boolean hasFocus) {
int selRow = 0;
int prevselRow = 0;
if (hasFocus == true) {
if (view.getId() % COLNO != 0) {
selRow = (view.getId() / COLNO) + 1;
} else {
selRow = (view.getId() / COLNO);
}
int firstCol = ((selRow - 1) * COLNO) + 1;
if (oldView != 0) {
if (oldView % 7 != 0) {
prevselRow = (oldView / COLNO) + 1;
} else {
prevselRow = (oldView / COLNO);
}
int prevfirstCol = ((prevselRow - 1) * COLNO) + 1;
TextView tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 1);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 2);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 3);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 4);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 5);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
tv = new TextView(this);
tv = (TextView) findViewById(prevfirstCol + 6);
if (prevselRow % 2 == 0) {
tv.setBackgroundResource(R.color.white);
} else {
tv.setBackgroundResource(R.color.LightPink);
}
}
// new Row
int color = Color.GRAY;
oldView = view.getId();
String result = new String();
TextView tv = new TextView(this);
tv = (TextView) findViewById(firstCol);
result = tv.getText().toString();
oldColor = tv.getDrawingCacheBackgroundColor();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 1);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 2);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 3);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 4);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 5);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
tv = new TextView(this);
tv = (TextView) findViewById(firstCol + 6);
result += "," + tv.getText().toString();
tv.setBackgroundColor(color);
// Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
}
public void insertRetailer() {
}
public void fetchRetailer() {
}
}