2017-03-07 20 views
0

目前我正在使用一個項目來在垂直顯示Android的表中顯示數據庫值。我在水平方向得到了答案,現在我想要垂直方向。如何顯示我的SQL數據庫中的數據並垂直顯示在表中

my current output

I want it like this.

主要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> 

enter image description here

+0

你是什麼意思水平? –

+0

對不起,我需要垂直像第二張圖片。 – Jincy

+0

兩者有什麼區別?看起來相似。你爲什麼要水平?只是獲取數據,並使Listview項目行與Linearlayout方向水平 –

回答

-1

你必須使用1噸能夠?您可以拆分佈局,並使用兩個表格「標題」和一個值作爲表格。

你可以這樣拆分佈局:

<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> 

然後添加內佈局內你的表。

相關問題