7
A
回答
1
rs1 = stmt.executeQuery("SELECT * from message");
StringBuilder sb = new StringBuilder();
while (rs1.next())
{
String script = rs1.getString(1);
String call = rs1.getString(2);
String price = rs1.getString(3);
String stoploss = rs1.getString(4);
String target = rs1.getString(5);
String ltp = rs1.getString(6);
String exit = rs1.getString(7);
sb.append(script).append(";").append(call).append(";").append(price).append(";").append(stoploss).append(";").append(target).append(";").append(ltp).append(";").append(exit).append("_");
}
out.print(sb.toString());
out.flush();
爲此,你有XML 這個你有一個XML像
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_marginTop="20dip">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab"
>
<TableRow>
</TableRow>
</TableLayout>
</LinearLayout>
,以顯示你寫了Android的數據。
String st = new String(str);
Log.e("Main",st);
String[] rows = st.split("_");
TableLayout tableLayout = (TableLayout)findViewById(R.id.tab);
tableLayout.removeAllViews();
for(int i=0;i<rows.length;i++){
String row = rows[i];
TableRow tableRow = new TableRow(getApplicationContext());
final String[] cols = row.split(";");
Handler handler = null;
for (int j = 0; j < cols.length; j++) {
final String col = cols[j];
final TextView columsView = new TextView(getApplicationContext());
columsView.setText(String.format("%7s", col));
tableRow.addView(columsView);
0
當您想要填充的表格中定義了不變的行/列數時,可以使用靜態。否則,我建議使用一個循環,併爲循環中的每一步在表視圖中添加一行。
4
這取決於。如果你確定只有幾行,那麼你可以將它們循環添加到TableLayout中。但請注意,您可以爲每一行創建視圖。
隨着大量數據的創建例如基於CursorAdapter的ListView和適配器:
public class MyCursorAdapter extends CursorAdapter {
private static final String TAG = "MyCursorAdapter";
private final int NAME_COLUMN;
private final int ADDRESS_COLUMN;
private final int STATE_COLUMN;
public MyCursorAdapter(Context context, Cursor c) {
super(context, c);
NAME_COLUMN = c.getColumnIndexOrThrow("name");
ADDRESS_COLUMN = c.getColumnIndexOrThrow("address");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.custom_row, null);
MyRowViewHolder rowData = new MyRowViewHolder();
rowData.name = (TextView) view.findViewById(R.id.name);
rowData.address = (TextView) view.findViewById(R.id.address);
rowData.name.setText(cursor.getString(NAME_COLUMN));
rowData.address.setText(cursor.getString(ADDRESS_COLUMN));
view.setTag(rowData);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
MyRowViewHolder rowData = (MyRowViewHolder) view.getTag();
rowData.name.setText(cursor.getString(NAME_COLUMN));
rowData.address.setText(cursor.getString(ADDRESS_COLUMN));
}
public static class MyRowViewHolder {
TextView name;
TextView address;
}
}
這種方法不創建視圖的每一行。我認爲這更好,但需要更多的努力。爲了讓表的佈局風格使用的LinearLayout的行與layout_weight列的
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_weight="0.25"
android:layout_width="0"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/address"
android:layout_weight="0.75"
android:layout_width="0"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
到ListView,如果你想添加頁眉和頁腳。
0
我們可以想象一個android自定義組件:TableView。
它的代碼會是這樣的:
public class TableView extends TableLayout {
public TableView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TableView(Context context) {
super(context);
}
public void setAdapter(TableAdapter<?> adapter) {
removeAllViews();
for (int row = 0; row < adapter.getRowCount(); row++) {
TableRow tableRow = new TableRow(getContext());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(params);
addView(tableRow);
for (int column = 0; column < adapter.getColumnCount(); column++) {
View cell = adapter.getView(row, column);
tableRow.addView(cell);
TableRow.LayoutParams cellParams = (android.widget.TableRow.LayoutParams) cell.getLayoutParams();
cellParams.weight = adapter.getColumnWeight(column);
cellParams.width = 0;
}
}
}
}
它會使用像這樣的適配器:
public interface TableAdapter<T> {
int getRowCount();
int getColumnWeight(int column);
int getColumnCount();
T getItem(int row, int column);
View getView(int row, int column);
}
8
這可能對您有用..
try{
JSONArray jArray = new JSONArray(result);
TableLayout tv=(TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag=1;
// when i=-1, loop will display heading of each column
// then usually data will be display from i=0 to jArray.length()
for(int i=-1;i<jArray.length();i++){
TableRow tr=new TableRow(Yourclassname.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// this will be executed once
if(flag==1){
TextView b3=new TextView(Yourclassname.this);
b3.setText("column heading 1");
b3.setTextColor(Color.BLUE);
b3.setTextSize(15);
tr.addView(b3);
TextView b4=new TextView(Yourclassname.this);
b4.setPadding(10, 0, 0, 0);
b4.setTextSize(15);
b4.setText("column heading 2");
b4.setTextColor(Color.BLUE);
tr.addView(b4);
TextView b5=new TextView(Yourclassname.this);
b5.setPadding(10, 0, 0, 0);
b5.setText("column heading 3");
b5.setTextColor(Color.BLUE);
b5.setTextSize(15);
tr.addView(b5);
tv.addView(tr);
final View vline = new View(Yourclassname.this);
vline.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline); // add line below heading
flag=0;
} else {
JSONObject json_data = jArray.getJSONObject(i);
TextView b=new TextView(Yourclassname.this);
String str=String.valueOf(json_data.getInt("column1"));
b.setText(str);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1=new TextView(Yourclassname.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
String str1=json_data.getString("column2");
b1.setText(str1);
b1.setTextColor(Color.WHITE);
tr.addView(b1);
TextView b2=new TextView(Yourclassname.this);
b2.setPadding(10, 0, 0, 0);
String str2=String.valueOf(json_data.getInt("column3"));
b2.setText(str2);
b2.setTextColor(Color.RED);
b2.setTextSize(15);
tr.addView(b2);
tv.addView(tr);
final View vline1 = new View(Yourclassname.this);
vline1.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1); // add line below each row
}
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data " + e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
1
我看到這篇文章是相當古老的,但如果別人也面臨着在Android中的表中顯示自定義數據的問題,我願意e提供我的TableView作爲可能的解決方案。
所以你不關心如何把數據填到表,你可以簡單地創建一個自定義的適配器你想顯示(就像我們已經在Android的意見,從類似的ListView知道)的數據。
我們的代碼看起來像這樣:
List<Flight> myData = new ArrayList<>();
myData.add(new Flight(...));
myData.add(new Flight(...));
myData.add(new Flight(...));
TableView<Flight> table = findViewById(R.id.table);
table.setHeaderAdapter(new SimpleHeaderAdapter("Time", "Airline", "Flight", "Destination"));
table.setDataAdapter(new FlightDataAdapter(myData));
結果看起來是這樣的:
相關問題
- 1. 在桌面視圖中顯示來自plist的數據數組
- 2. 數據僅在桌面視圖中單擊時顯示
- 3. 數據不顯示在桌面視圖Swift CoreData
- 4. 如何在單元格在桌面視圖中顯示視圖
- 5. 桌面視圖標籤上顯示的TableView數據
- 6. 隱藏或顯示桌面視圖
- 7. 來自服務器的數據沒有被顯示在桌面視圖中
- 8. 在Android桌面佈局中顯示SQLite數據
- 9. 桌面視圖中的webview沒有顯示在iOS 7中
- 10. 在桌面視圖頂部顯示下方顯示
- 11. 如何在桌面視圖中顯示圖像
- 12. 更新桌面視圖(編輯後)中的條目並顯示在桌面視圖中的問題
- 13. Swift解析 - 本地數據存儲和在桌面視圖中顯示對象
- 14. 在桌面視圖中顯示錄製的音頻
- 15. 在桌面視圖中顯示一年的日期
- 16. 如何在Android中的桌面上顯示圖像
- 17. 在Android桌面上使用SL4A Python顯示數據(example)
- 18. 在桌面視圖單元格內全屏顯示視頻
- 19. 在桌面視圖中顯示移動導航引導程序
- 20. 搜索欄結果不會顯示在桌面視圖中
- 21. 如何在桌面視圖中顯示模態查看器
- 22. 問題圖片顯示在桌面視圖
- 23. 核心數據與桌面視圖
- 24. 列表視圖不顯示數據庫中的數據Android
- 25. android數據庫顯示在列表視圖顯示一行
- 26. 顯示數據庫的內容在列表視圖中的Android
- 27. 查看頁面裏面的回收站視圖不顯示數據在Android
- 28. 如何從肥皂中加載數組並在桌面視圖中顯示?
- 29. 動態顯示桌面視圖單元格上的圖像
- 30. 數據綁定在視圖頁面不顯示數據
你是什麼意思的 「靜態」? – WarrenFaith 2011-02-11 11:23:41
TextView value3 =(TextView)findViewById(R.id。值3); \t \t \t \t \t \t value3.setText(k2);那種代碼..形式靜態是必要的? – Siten 2011-02-11 11:33:31
問清楚.. – Ads 2011-02-11 11:35:41