将数据添加到表布局中的行

时间:2013-08-09 09:31:15

标签: android android-tablelayout

我试图创建一种EPG类型的布局,我提到了这一点 (Print out ArrayList content in Android TableLayout) 我的问题是当我尝试添加行时,它们会被添加为新行但我需要将这些值添加到现有行中。 这是我的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <TableLayout
        android:id="@+id/myTableLayout"
        android:layout_width="match_parent"
        android:layout_height="354dp"
        android:background="#6495ED"
        android:stretchColumns="*" >


     <TableRow
            android:id="@+id/tablerow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

             <TextView
                 android:id="@+id/textView1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"

                 android:text="Some Text " 
                />

             <TextView
             android:id="@+id/textView2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"

             android:text="Some Text"
              />

             <TextView
             android:id="@+id/textView3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"

             android:text="Some Text" />



        </TableRow>
</TableLayout>

我的java文件是

TableLayout table = (TableLayout) findViewById(R.id.myTableLayout);
            for(int i=0;i<channel_arraylist.size();i++)
            {

                TableRow tableRow2=new TableRow(VideoDatabase1.this);
                localchannel1= channel_arraylist.get(i);

                  TextView tvid=new TextView(VideoDatabase1.this);
                  tvid.setText(""+Integer.toString(localchannel1.getID()));
             tableRow2.addView(tvid);

            table.addView(tableRow2);

4 个答案:

答案 0 :(得分:2)

我对你问题的回答。

    private ArrayList<String> data = new ArrayList<String>();
    private ArrayList<String> data1 = new ArrayList<String>();
    private TableLayout table;    

    data.add("Samsung");
    data.add("Apple");
    data.add("Pixle");
    data.add("Vivo");
    data1.add(" $109");
    data1.add(" $200");
    data1.add(" $399");
    data1.add(" $290");        


    table = (TableLayout)findViewById(R.id.mytable);

    for(int i=0;i<data.size();i++)
    {
        TableRow row=new TableRow(this);
        String phone = data.get(i);
        String amount = data1.get(i);
        TextView tv1=new TextView(this);
        tv1.setText(phone);
        TextView tv2=new TextView(this);
        tv2.setText(amount);
        row.addView(tv1);
        row.addView(tv2);
        table.addView(row);
    }

答案 1 :(得分:1)

尝试替换

TableRow tableRow2 = new TableRow(VideoDatabase1.this);

TableRow tableRow2 =((TableRow)table.getChildAt(0));

另外,在行中设置断点:

for(int i=0;i<channel_arraylist.size();i++)

调试模式中观看“table”变量。它必须包含至少1个孩子。它必须有所帮助。

关于你的问题:必须在下面添加TableRow,因为你向tableLayout添加了一个新行。它通常都是。

答案 2 :(得分:0)

我终于找到了解决方案。要将视图添加到特定表,只需执行

TextView tvid=(TextView)findViewById(R.id.textView1); 
          tvid.setText(localchannel1.getName()); 

此处id是指您在布局文件中声明的id。只需这样就可以在相应的列中插入值。谢谢你的一切。

答案 3 :(得分:-1)

/ *这是一个完整的例子,说明如何使用一个框架填充数据库中的表格布局并使用一个捆绑来接收参数。 我希望这个代码可以帮到你 * /

package co.m3medios.nagaapp.Fragments;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import co.m3medios.nagaapp.R;
import co.m3medios.nagaapp.SQLite.ConexionSQLiteHelper;
import co.m3medios.nagaapp.Utils.Utilidades;

public class ListasPreciosFragment extends Fragment {

    public TableLayout listaPreciosGeneral;
    public String[] titulo= {"Referencia", "Colección","Silueta", "Clas Producto", "Composición", "M", "Vende", "Color", "Tallas", "Agotados", "Observaciones", "Entrega", "Tiquete"};
    private OnFragmentInteractionListener mListener;

    public ListasPreciosFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static ListasPreciosFragment newInstance(String param1, String param2) {
        ListasPreciosFragment fragment = new ListasPreciosFragment();


        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_listas_precios, container, false);
        listaPreciosGeneral = (TableLayout) view.findViewById(R.id.tabla_listaPreciosGral);

        llenarTablaGeneral();

返回视图;         }

    private void llenarTablaGeneral() {
        TableRow tbrow0 = new TableRow(getContext());
        tbrow0.setBackgroundColor(Color.parseColor("#2d2d5a"));
        tbrow0.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

        //LLENAR EL HEADER DE LA TABLA

        for(String c:titulo){
            TextView tv0 = new TextView(getContext());
            tv0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            tv0.setGravity(Gravity.CENTER);
            tv0.setTextSize(18);
            tv0.setTextColor(Color.parseColor("#ffffff"));
            tv0.setPadding(5, 5, 5, 5);
            tv0.setText(c);
            tbrow0.addView(tv0);
        }
        //INSERTAR TITULOS
        listaPreciosGeneral.addView(tbrow0);


        //ABRIR LA CONEXION A LA BASE DE DATOS PARA ESCRITURA
        ConexionSQLiteHelper conn = new ConexionSQLiteHelper(getContext(), "nagamobile", null, 1);
        SQLiteDatabase db = conn.getWritableDatabase();//abro la BD para escritura

        db.beginTransaction();

        int color = 0;
        try
        {
            String selectQuery = "SELECT * FROM "+ Utilidades.TABLA_LISTAPRECIOS;
            Cursor cursor = db.rawQuery(selectQuery,null);
            if(cursor.getCount() >0)
            {
                while (cursor.moveToNext()) {
                    // LEER LOS DATOS DE LAS COLUMNAS POR FILA

                    int id_lista_p = cursor.getInt(cursor.getColumnIndex("id"));
                    String referencia = cursor.getString(cursor.getColumnIndex("referencia"));
                    String coleccion = cursor.getString(cursor.getColumnIndex("coleccion"));
                    String silueta = cursor.getString(cursor.getColumnIndex("silueta"));
                    String clasProducto = cursor.getString(cursor.getColumnIndex("clasProducto"));
                    String composicion = cursor.getString(cursor.getColumnIndex("composicion"));
                    String m = cursor.getString(cursor.getColumnIndex("m"));
                    String vende = cursor.getString(cursor.getColumnIndex("vende"));
                    String colorListaP = cursor.getString(cursor.getColumnIndex("colorListaP"));
                    String tallas = cursor.getString(cursor.getColumnIndex("tallas"));
                    String agotada = cursor.getString(cursor.getColumnIndex("agotada"));
                    String observaciones = cursor.getString(cursor.getColumnIndex("observaciones"));
                    String entrega = cursor.getString(cursor.getColumnIndex("entrega"));
                    String tiquete = cursor.getString(cursor.getColumnIndex("tiquete"));
                    tiquete = "$ "+tiquete;

                    // CREAR LAS FILAS DE DATOS POR CADA ITERACION
                    TableRow row = new TableRow(getContext());
                    row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

                    //CAMBIAR EL COLOR DE LAS FILAS
                        if ( color % 2 == 0) {
                            row.setBackgroundColor(Color.parseColor("#FFFFFF"));//LINEA BLANCA
                        }
                        else {
                            row.setBackgroundColor(Color.parseColor("#abb1b3"));//LINEA GRIS
                        }

                   //RENDERIZAR LINEA A LINEA
                    String[] lpRender={referencia,coleccion,silueta,clasProducto,composicion,m,vende,colorListaP,
                                     tallas,agotada,observaciones,entrega,tiquete};

                    for(String text:lpRender) {
                        TextView tv = new TextView(getContext());//CADA BUCLE INSTANCIA UN NUEVO OBJETO PARA CREAR LA FILA
                        tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
                        tv.setGravity(Gravity.CENTER);
                        tv.setTextColor(Color.parseColor("#2d2d5a"));
                        tv.setTextSize(14);
                        tv.setPadding(5, 5, 5, 5);
                        tv.setText(text);
                        row.addView(tv);
                    }
                    listaPreciosGeneral.addView(row);
                    color ++;//AUMENTA POR CADA ITERACION PARA CAMBIAR EL COLOR DE LA LINEA
                }
            }
            db.setTransactionSuccessful();
        }
        catch (SQLiteException e)
        {
            e.printStackTrace();
        }
        finally
        {
            db.endTransaction();
            db.close();
        }
    }

    public interface OnFragmentInteractionListener {
           void onFragmentInteraction(Uri uri);
    }
}