Autore Topic: TableRow create dinamicamente con xml prefissato  (Letto 499 volte)

Offline dom4

  • Utente normale
  • ***
  • Post: 158
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    Vodafone Ideos, Nexus 5
TableRow create dinamicamente con xml prefissato
« il: 05 Aprile 2013, 17:15:07 CEST »
0
Salve ragazzi,
sto creando una TableLayout con aggiunta di TableRow dinamica.
Invece di creare i figli della tableRow dinamicamente ho pensato che fosse possibile creare uno specifico layout per la tableRow:
Codice (XML): [Seleziona]
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/tableRow1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_marginLeft="5dp"
   android:layout_marginRight="5dp"
    >

    <CheckBox
       android:id="@+id/checkBoxTable"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

    <ImageButton
       android:id="@+id/imageButtonStop"
       android:layout_width="30dp"
       android:layout_height="30dp"
       android:src="@drawable/ic_stop"
       android:visibility="gone"
       android:background="@android:color/transparent"
        />

    <TextView
       android:id="@+id/textViewTaskName"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="name"
       android:textStyle="@style/BoldBlue"
       android:layout_weight="1"
       />

    <ImageView
       android:id="@+id/imageViewArrow"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/ic_arrow" />

    <Button
       android:id="@+id/buttonHide"
       style="?android:attr/buttonStyleSmall"
       android:layout_width="40dp"
       android:layout_height="30dp"
       android:text="Hide"
       android:textSize="15sp"
       android:textStyle="bold"
       android:textColor="@color/white"
       android:background="@color/RedButton"
       android:visibility="gone"
        />

</TableRow>

Provandolo nel codice va in errore sull'inflating:

Codice (Java): [Seleziona]
TableLayout tableAllTasks = (TableLayout) getActivity().findViewById(R.id.rlAllTasks);
LayoutInflater inflater = getActivity().getLayoutInflater();
                                 for(int i = 0; i < cursorOverdue.getColumnCount();i++)  //cursorOverdue e' un Cursor
                                 {
                                         cursorOverdue.moveToNext();
                                         //TableRow row = new TableRow(getActivity());
                                         
                                         TableRow row = (TableRow)inflater.inflate(R.id.tableRow1,tableAllTasks);
                                         
                                         row.setTag("task");
                                         idRow = cursorOverdue.getInt(cursorOverdue.getColumnIndex("id"));
                                         Log.i("","ID TASK --> RIGA "+idRow);
                                         row.setId(idRow);
                                         
                                         CheckBox chBox = (CheckBox) row.findViewById(R.id.checkBoxTasks);
                                       
                                         TextView tvName = (TextView) row.findViewById(R.id.textViewNameTask);

                                         tvName.setTextAppearance(getActivity(), R.style.BoldBlue);
                                         String name = cursorOverdue.getString(cursorOverdue.getColumnIndex("name"));
                                         
                                        if(name.length()>30)
                                         {
                                                 String newName = name.substring(0, 28).concat("..");
                                                 tvName.setText(newName);
                                         }
                                         else{
                                         tvName.setText(cursorOverdue.getString(cursorOverdue.getColumnIndex("name")));
                                         }
                                         tvName.setTextSize(16 * getResources().getDisplayMetrics().density);
                                         
                                        ImageView imageArrow = (ImageView) row.findViewById(R.id.imageViewArrow);
                                       
                                         View line = new View(getActivity());
                                         line.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
                                         line.setBackgroundColor(Color.LTGRAY);
                                         
                                         ImageButton imageStop = (ImageButton) row.findViewById(R.id.imageButtonStop);
                                         tableAllTasks.addView(row);
                                         tableAllTasks.addView(line);
                                 }

Errore :
Codice: [Seleziona]
04-05 14:57:02.824: E/AndroidRuntime(10053): FATAL EXCEPTION: main
04-05 14:57:02.824: E/AndroidRuntime(10053): android.content.res.Resources$NotFoundException: Resource ID #0x7f0400c2 type #0x12 is not valid
04-05 14:57:02.824: E/AndroidRuntime(10053):         at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at android.content.res.Resources.getLayout(Resources.java:853)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at com.twentyci.MoveMe.progress.com.MyTasksFragment.createTasksList(MyTasksFragment.java:234)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at com.twentyci.MoveMe.progress.com.MyTasksFragment$2.handleMessage(MyTasksFragment.java:169)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at android.os.Looper.loop(Looper.java:137)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at android.app.ActivityThread.main(ActivityThread.java:5041)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at java.lang.reflect.Method.invokeNative(Native Method)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at java.lang.reflect.Method.invoke(Method.java:511)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-05 14:57:02.824: E/AndroidRuntime(10053):         at dalvik.system.NativeStart.main(Native Method)
04-05 14:57:05.884: E/Trace(10374): error opening trace file: No such file or directory (2)

Offline Mondo

  • Nuovo arrivato
  • *
  • Post: 2
  • Respect: 0
    • Github
    • Google+
    • ------
    • ------
    • ------
    • Mostra profilo
  • Dispositivo Android:
    ------
  • Play Store ID:
    ------
  • Sistema operativo:
    Windows 7
Re:TableRow create dinamicamente con xml prefissato
« Risposta #1 il: 09 Aprile 2013, 14:01:10 CEST »
0
Ei dom4, prova così:
Codice (Java): [Seleziona]
TableLayout tableAllTasks = (TableLayout) getActivity().findViewById(R.id.rlAllTasks);
int id = 0;

While(cursorOverdue.moveToNext()){

        View v = LayoutInflater.from(getActivity()).inflate("Qui metti il layout del TableRow es."R.layout.tableRowExample, tableAllTasks, false);

        //Per ogni views della TableRow setta id differenti

        TableRow tr = (TableRow) v.findViewById(R.id.tableRow1);
        CheckBox chBox = (CheckBox) v.findViewById(R.id.checkBoxTable);
        ImageButton imageStop = (ImageButton) v.findViewById(R.id.imageButtonStop);
        TextView tvName = (TextView) v.findViewById(R.id.textViewTaskName);
        ImageView imageArrow = (ImageView) v.findViewById(R.id.imageViewArrow);
        Button b = (Button) v.findViewById(R.id.buttonHide);

        idRow = cursorOverdue.getInt(cursorOverdue.getColumnIndex("id"));

        tr.setId(idRow);
        tr.setTag("task");
        chBox.setId(id); id++;
        imageStop.setId(id); id++;
        tvName.setId(id); id++;
        imageArrow.setId(id); id++;
        b.setId(id); id++;

        tvName.setTextAppearance(getActivity(), R.style.BoldBlue);
        String name = cursorOverdue.getString(cursorOverdue.getColumnIndex("name"));
        if(name.length()>30){
                String newName = name.substring(0, 28).concat("..");
                tvName.setText(newName);
        }
        else{
                tvName.setText(name);
        }
        tvName.setTextSize(16 * getResources().getDisplayMetrics().density);

        View line = new View(getActivity());
        line.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.LTGRAY);

        tableAllTasks.addView(v);
        tableAllTasks.addView(line);
}
Spero di esserti stato d'aiuto  :D

Offline dom4

  • Utente normale
  • ***
  • Post: 158
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    Vodafone Ideos, Nexus 5
Re:TableRow create dinamicamente con xml prefissato
« Risposta #2 il: 10 Aprile 2013, 16:15:20 CEST »
0
Grazie per la risposta Mondo,
alla fine ho gestito la situazione diversamente creando tutte le view dinamicamente e giocando con il setLayoutParameters sono riuscito ad ottenere cio' che volevo.
Provero' comunque la tua soluzione e ti faro' sapere,personalmente credo che sia piu' pulito usare un xml prefissato

Offline dom4

  • Utente normale
  • ***
  • Post: 158
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    Vodafone Ideos, Nexus 5
Re:TableRow create dinamicamente con xml prefissato
« Risposta #3 il: 17 Aprile 2013, 16:55:12 CEST »
0
Ciao Mondo;
ho provato il tuo metodo,ma non funziona,mi va in errore sull'inflating
Codice: [Seleziona]
04-17 14:50:49.070: W/Resources(13965): Converting to int: TypedValue{t=0x1/d=0x7f0a005b a=-1 r=0x7f0a005b}
04-17 14:50:49.080: D/AndroidRuntime(13965): Shutting down VM
04-17 14:50:49.080: W/dalvikvm(13965): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-17 14:50:49.130: E/AndroidRuntime(13965): FATAL EXCEPTION: main
04-17 14:50:49.130: E/AndroidRuntime(13965): android.view.InflateException: Binary XML file line #24: Error inflating class <unknown>
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.view.LayoutInflater.createView(LayoutInflater.java:613)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at com.twentyci.MoveMe.progress.com.MyTasksFragment.addTableRow(MyTasksFragment.java:769)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at com.twentyci.MoveMe.progress.com.MyTasksFragment.createTasksList(MyTasksFragment.java:545)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at com.twentyci.MoveMe.progress.com.MyTasksFragment$2.handleMessage(MyTasksFragment.java:179)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.os.Looper.loop(Looper.java:137)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.app.ActivityThread.main(ActivityThread.java:5041)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at java.lang.reflect.Method.invokeNative(Native Method)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at java.lang.reflect.Method.invoke(Method.java:511)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at dalvik.system.NativeStart.main(Native Method)
04-17 14:50:49.130: E/AndroidRuntime(13965): Caused by: java.lang.reflect.InvocationTargetException
04-17 14:50:49.130: E/AndroidRuntime(13965):         at java.lang.reflect.Constructor.constructNative(Native Method)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.view.LayoutInflater.createView(LayoutInflater.java:587)
04-17 14:50:49.130: E/AndroidRuntime(13965):         ... 17 more
04-17 14:50:49.130: E/AndroidRuntime(13965): Caused by: java.lang.NumberFormatException: Invalid int: "@2131361883"
04-17 14:50:49.130: E/AndroidRuntime(13965):         at java.lang.Integer.invalidInt(Integer.java:138)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at java.lang.Integer.parse(Integer.java:375)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at java.lang.Integer.parseInt(Integer.java:366)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:123)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.content.res.TypedArray.getInt(TypedArray.java:254)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.widget.TextView.<init>(TextView.java:924)
04-17 14:50:49.130: E/AndroidRuntime(13965):         at android.widget.TextView.<init>(TextView.java:578)
04-17 14:50:49.130: E/AndroidRuntime(13965):         ... 20 more