Note that there are some explanatory texts on larger screens.

plurals
  1. POTablelayout in Scrollview not working
    text
    copied!<p>My fragment loads its data into a tablelayout inside a scrollview. At first, it will load saying "Click on image to view more data".<br> I have a onclick listener from the gallery that should delete all the rows from the tablelayout with <code>tableLayout.removeAllViews();</code>. Then the fragment will add row/rows to a tablelayout inside a scrollview. </p> <p>The tablelayout should change on every click from the gallery.</p> <p>However I am getting: **java.lang.IllegalStateException: ScrollView can host only one direct child. This line shows up with the above error in logcat: <code>getFragmentManager().executePendingTransactions();</code> in my addToTracker function.</p> <p><strong>gallery activity</strong></p> <pre><code>public class GalleryActivity extends Activity { private Gallery[] galleryView; private ArrayList&lt;ArrayList&lt;Dish&gt;&gt; listOfList; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); galleryView = new Gallery[3]; //for each of the 3 sites galleryView[0] = (Gallery)findViewById(R.id.galleryid0); galleryView[1] = (Gallery)findViewById(R.id.galleryid1); galleryView[2] = (Gallery)findViewById(R.id.galleryid2); final FragmentManager fragmentManager = getFragmentManager(); final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); galleryView[0].setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { addToTracker(0, position, fragmentTransaction); } }); galleryView[1].setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { addToTracker(1, position, fragmentTransaction); } }); galleryView[2].setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { addToTracker(2, position, fragmentTransaction); } }); } private void addToTracker(int id, int position, FragmentTransaction fragmentTransaction) { TrackerFragment tf = (TrackerFragment) getFragmentManager().findFragmentById(R.id.tracker1); fragmentTransaction.remove(tf); tf = TrackerFragment.newInstance(listOfList.get(id).get(position)); fragmentTransaction.replace(R.id.tracker1, tf); fragmentTransaction.commitAllowingStateLoss(); getFragmentManager().executePendingTransactions(); } </code></pre> <p><strong>tracker fragment</strong></p> <pre><code>public class TrackerFragment extends Fragment { private Dish dish; public static TrackerFragment newInstance(Serializable dish) { TrackerFragment tf = new TrackerFragment(); Bundle args = new Bundle(); args.putSerializable("dish", dish); tf.setArguments(args); return tf; } public static TrackerFragment newInstance(Bundle bundle) { Dish dish = (Dish) bundle.getSerializable("dish"); return newInstance(dish); } @Override public void onCreate(Bundle myBundle) { super.onCreate(myBundle); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.tracker, container, false); TableLayout tableLayout = (TableLayout) v.findViewById(R.id.tracker_layout); tableLayout.removeAllViews(); if (dish != null) { //display others //display subsystem stuff for (int i = 0; i &lt; dish.getSubsystems().size(); i++) { TableRow tableRow = new TableRow(v.getContext()); tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); TextView tv = new TextView(v.getContext()); tv.setText(dish.getSubsystems().get(i).getConnFuncAddr()); tableRow.addView(tv); tableLayout.addView(tableRow); } } else { TableRow tableRow = new TableRow(v.getContext()); tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); TextView tv = new TextView(v.getContext()); tv.setText("Click on image to view more data"); tableRow.addView(tv); tableLayout.addView(tableRow); } return v; } } </code></pre> <p><strong>main.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;!-- Trackers --&gt; &lt;LinearLayout android:layout_height="fill_parent" android:layout_width="300dp" android:layout_weight="1" android:orientation="vertical"&gt; &lt;fragment android:name="android.gallery.TrackerFragment" android:id="@+id/tracker1" android:layout_height="500dp" android:layout_width="300dp" android:layout_weight="1"&gt; &lt;/fragment&gt; &lt;/LinearLayout&gt; &lt;!-- Gallery --&gt; &lt;LinearLayout android:layout_height="fill_parent" android:layout_width="600dp" android:layout_weight="1" android:orientation="vertical"&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="245dp"&gt; &lt;Gallery android:id="@+id/galleryid0" android:layout_width="fill_parent" android:layout_height="match_parent"/&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="245dp"&gt; &lt;Gallery android:id="@+id/galleryid1" android:layout_width="fill_parent" android:layout_height="match_parent"/&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="245dp"&gt; &lt;Gallery android:id="@+id/galleryid2" android:layout_width="fill_parent" android:layout_height="match_parent"/&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>tracker.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TextView android:layout_height="wrap_content" android:layout_width="250dp" android:text="Tracker 1" android:id="@+id/t1"/&gt; &lt;ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/delete2" android:onClick="remove1" android:layout_toRightOf="@id/t1" /&gt; &lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tracker_layout" android:layout_below="@id/t1" android:layout_marginTop="40dp"&gt; &lt;/TableLayout&gt; &lt;/RelativeLayout&gt; &lt;/ScrollView&gt; </code></pre> <p>Does anyone know whats wrong? I am guessing I am adding a table row to the scrollview and not the tablelayout, however, I dont see where I am doing that wrong in my code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload