1

我正在尝试创建一种 EPG 类型的布局,我提到了这个(在 Android TableLayout 中打印出 ArrayList 内容)我的问题是当我尝试添加行时,它们被添加为新行,但我需要添加这些值现有的行。这是我的 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

4 回答 4

4

我对你的问题的回答。

    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);
    }
于 2018-01-03T16:41:38.637 回答
1

尝试更换

TableRow tableRow2=new TableRow(VideoDatabase1.this);

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

另外,在行中设置断点:

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

并在调试模式下观察你的“表”变量。它必须包含至少 1 个孩子。它必须有帮助。

关于您的问题:必须在下面添加 TableRow,因为您向 tableLayout 添加了一个新行。而且很正常。

于 2013-08-09T09:44:17.543 回答
0

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

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

这里id指的是id你在布局文件中声明的。简单地说,这会将值插入相应的列中。感谢一切。

于 2013-08-17T09:50:29.297 回答
-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);
    }
}
于 2018-01-17T15:32:39.320 回答