【问题标题】:Adding data to the rows in Table layout向表格布局中的行添加数据
【发布时间】:2018-01-17 23:32:39
【问题描述】:

我正在尝试创建一种 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);

【问题讨论】:

  • 所以数据没有正确添加到表格布局中??
  • 是的,数据不会添加到表格行中。而是添加到布局下方

标签: android android-tablelayout


【解决方案1】:

我对你的问题的回答。

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

【讨论】:

    【解决方案2】:

    尝试替换

    TableRow tableRow2=new TableRow(VideoDatabase1.this);

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

    另外,在行中设置断点:

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

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

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

    【讨论】:

    • 嗨@Panda 首先感谢您的回答。我试过你的代码。但它显示类演员异常。我是新手。那你能详细解释一下吗
    • 对不起 4 这个。是的,您可以将右侧投射到(TableRow)。我已经更新了我的答案。
    • 这些值被插入到表格布局的右侧,但不在表格布局中..
    【解决方案3】:

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

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

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

    【讨论】:

    • 感谢您的指导。
    • 为了什么??寻求帮助或记得发送请求??
    【解决方案4】:

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

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

    【讨论】:

    • 也许不要大喊大叫并修复格式。此外,一些描述这如何解决手头问题的词语,而不仅仅是一堵代码,将使这个答案更有用。
    猜你喜欢
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多