1

Am trying to create an EPG kind of layout and i referred this (Print out ArrayList content in Android TableLayout) And my problem is when am trying to add rows, they are getting added as a new rows but i need those values to be added in existing rows. Here is my XML file:

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

and my java file is

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);
2
  • so data is not added properly in table layout?? Commented Aug 9, 2013 at 9:54
  • yes data is not added in table rows.. instead its getting added below the layout Commented Aug 9, 2013 at 12:31

4 Answers 4

4

My answer for your question.

    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);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Try to replace

TableRow tableRow2=new TableRow(VideoDatabase1.this);

to

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

Also, set a breakpoint in line:

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

And watch your "table" variable in debug mode. It must contain at least 1 child. It must help.

Regarding your question: TableRow must be added below because you add a new row to tableLayout. And it's normally.

3 Comments

Hi @Panda first thanks for your answer. I tried your code. but its showing class cast exception.Am a newbie. so can you explain me in detail about this
Sorry 4 this. Yes you can cast right side to (TableRow). I've update my answer.
The values are getting inserted in right side of table layout but not on table layout..
0

I finally found out the solution. To add the views to the particular table just do

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

Here id refers to the id you declared in your layout file. Simply this will insert the value in the corresponding column. Thanks for everything.

1 Comment

for what ?? for help or remember to send request??
-1

/*THIS IS A FULL EXAMPLE ON HOW TO FILL A TABLE LAYOUT FROM A DATABASE USING A FRAMGENT AND RECEIVING PARAMETERS USING A BUNDLE. I HOPE THIS CODE COULD HELP YOU */

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();

return view; }

    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);
    }
}

1 Comment

Maybe DON'T YELL and fixup the formatting. Also, some words describing how this addresses the question at hand instead of just a wall of code would make this answer more useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.