I currently am building a list of database driven results on an Android page. I've read that you should however, build your content views in XML, which I have done. My question is, should I really build 10 empty rows and populate them on the fly, or should I add the rows through building the views in Java? I may add more rows later on.
The data I am displaying looks something like this:
The controls on the right increase and decrease the number of points allotted to a skill开发者_运维百科, so the list/row will have to enable some type of user control.
<?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>
I think this code will help you.
here is whole Program...
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() {
}
}
You want to use a CursorAdapter and a ListView to display the data. Here is an example from the Android docs. Binding Data
This will keep your displayed list in sync with the database (if things are added or removed) and you can design your item templates for the rows in XML if you need complex layout.
Try to use ListView. http://developer.android.com/resources/tutorials/views/hello-listview.html
I think you must add rows programatically only.
Check Here...
`
private LinearLayout nameContainer;
private LinearLayout addressContainer;
private LinearLayout parentContainer;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
createNameContainer();
createAddressContainer();
createParentContainer();
setContentView(parentContainer);
}
private void createNameContainer()
{
nameContainer = new LinearLayout(this);
nameContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
nameContainer.setOrientation(LinearLayout.HORIZONTAL);
TextView nameLbl = new TextView(this);
nameLbl.setText("Name: ");
nameContainer.addView(nameLbl);
TextView nameValueLbl = new TextView(this);
nameValueLbl.setText("John Doe");
nameContainer.addView(nameValueLbl);
}
private void createAddressContainer()
{
addressContainer = new LinearLayout(this);
addressContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
addressContainer.setOrientation(LinearLayout.VERTICAL);
TextView addrLbl = new TextView(this);
addrLbl.setText("Address:");
TextView addrValueLbl = new TextView(this);
addrValueLbl.setText("911 Hollywood Blvd");
addressContainer.addView(addrLbl);
addressContainer.addView(addrValueLbl);
}
private void createParentContainer()
{
parentContainer = new LinearLayout(this);
parentContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
parentContainer.setOrientation(LinearLayout.VERTICAL);
parentContainer.addView(nameContainer);
parentContainer.addView(addressContainer);
}
}
`
精彩评论