在android中的表视图中显示数据

Sit*_*ten 10 android

我想从database我的android中获取数据table view.

我应该使用循环吗?这对静态有好处吗?

sel*_*chi 10

这可能对你有用..

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.MATCH_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.MATCH_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.MATCH_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();
}
Run Code Online (Sandbox Code Playgroud)

  • 不推荐使用`LayoutParams.FILL_PARENT`; 使用`LayoutParams.MATCH_PARENT`. (3认同)

Sit*_*ten 5

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();
Run Code Online (Sandbox Code Playgroud)

为此,您有 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> 
Run Code Online (Sandbox Code Playgroud)

在您编写的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);
Run Code Online (Sandbox Code Playgroud)


Ing*_*arz 5

我看到这篇文章已经很老了,但是如果其他人也面临在Android的Table中显示自定义数据的问题,我想提供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));
Run Code Online (Sandbox Code Playgroud)

结果可能如下所示:

例