目前我正在使用一个项目来垂直显示Android中的表格中的数据库值。我得到了横向的答案,现在我想要它是垂直的。
主要Activity.java
public class MainActivity extends Activity {
String data = "";
TableLayout tl;
TableRow tr;
TextView label;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.maintable);
final GetDataFromDB getdb = new GetDataFromDB();
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<Users> users = parseJSON(data);
addData(users);
}
});
}
}).start();
}
public ArrayList<Users> parseJSON(String result) {
ArrayList<Users> users = new ArrayList<Users>();
try {
JSONObject jsono = new JSONObject(result);
//JSONArray jArray = new JSONArray("users");
JSONArray jArray = jsono.getJSONArray("agriculture1");
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users user = new Users();
user.setId(json_data.getInt("id"));
user.setCrop(json_data.getString("crop"));
user.setCategory(json_data.getString("category"));
user.setSoil_texture(json_data.getString("soil_texture"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText("Crop");
label.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
label.setPadding(30, 30, 30, 30);
label.setBackgroundColor(Color.LTGRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView place = new TextView(this);
place.setText("Category");
place.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
place.setPadding(30, 30, 30, 30);
place.setBackgroundColor(Color.LTGRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(place,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView soil = new TextView(this);
soil.setText("Soil_Texture");
soil.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
soil.setPadding(30, 30, 30, 30);
soil.setBackgroundColor(Color.LTGRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(soil,params);
tr.addView((View)Ll); // Adding textview to tablerow
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
@SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<Users> users) {
addHeader();
for (Iterator i = users.iterator(); i.hasNext();) {
Users p = (Users) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getCrop());
label.setId(p.getId());
label.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
label.setPadding(10,10, 10, 10);
label.setBackgroundColor(Color.CYAN);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView place = new TextView(this);
place.setText(p.getCategory());
place.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
place.setPadding(10, 10, 10, 10);
place.setBackgroundColor(Color.CYAN);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(place,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView soil = new TextView(this);
soil.setText(p.getCategory());
soil.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
soil.setPadding(10, 10, 10, 10);
soil.setBackgroundColor(Color.CYAN);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(soil,params);
tr.addView((View)Ll);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_below="@+id/textView1">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:stretchColumns="*"
android:id="@+id/maintable" >
</TableLayout>
</ScrollView>
答案 0 :(得分:-1)
你必须使用1张桌子吗?您可以拆分布局并使用两个表格,用于&#34;标题&#34;和价值观。
您可以像这样拆分布局:
<LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent">
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="0dp"/>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="0dp"/>
</LinearLayout>
然后在内部布局中添加表格。