我目前正在Android页面上构建数据库驱动结果列表。我已经读过你应该用XML构建你的内容视图,我已经完成了。我的问题是,我是否应该真正构建10个空行并在运行中填充它们,还是应该通过在Java中构建视图来添加行?我稍后可能会添加更多行。
我正在显示的数据如下所示:
右侧的控件增加和减少分配给技能的点数,因此列表/行必须启用某种类型的用户控件。
答案 0 :(得分:4)
<?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>
我认为此代码可以帮助您。
答案 1 :(得分:4)
这是整个程序......
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() {
}
}
答案 2 :(得分:1)
您想使用CursorAdapter和ListView来显示数据。以下是Android文档中的示例。 Binding Data
这将使您显示的列表与数据库保持同步(如果添加或删除了某些内容),并且如果您需要复杂的布局,则可以为XML中的行设计项目模板。
答案 3 :(得分:0)
答案 4 :(得分:0)
我认为你必须以编程方式添加行。
点击这里......
`
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);
}
}
`