Question

My layout includes an unpopulated horizontal linear layout with a multicolumn listview below. The listview is populated from an sqlite table via a custom cursor adapter in a separate java file. each time a particular view in a listview row is tapped, a button is dynamically created and added to the linear layout. For reasons I don't understand, when this happens, the listview content disappears. If I change the orientation of the device, it goes through the onCreate and the display builds correctly with the linear layout buttons showing correctly over the listview.

The main layout file:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/llRunnerList"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" >
            </LinearLayout>
        </HorizontalScrollView>

        <ListView
            android:id="@+id/list_data"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

and the view listener code in the custom cursor adapter:

    private OnClickListener strRunnerOnClickListener = new OnClickListener() {
        @Override
        //
        // When the Runner field is clicked, add the Runner name to the Runner row.
        //
        public void onClick(View v) {
            final DBAdapter databaseHelper = new DBAdapter(ctxThis);

            LayoutParams params =
                    new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT);

            TextView tvRunner = (TextView) v;
            String strRunner = (String) tvRunner.getText();
            if(RaceSheetActivity.blnRaceRunning) {
                // get the ListView and the position in it.
                ListView lv = (ListView) v.getParent().getParent();
                final int position = lv.getPositionForView((View) v.getParent());

                // get the cursor pointing to that row
                Cursor c = (Cursor) lv.getItemAtPosition(position);
                long lngCurrentId = c.getInt(0);
                long lngRaceId = c.getInt(1);
                c.close();

                // Add to database if doesn't exist
                Cursor cSNRec = databaseHelper.getSNRecByCompetitorId(lngCurrentId);
                if(cSNRec.moveToFirst()) {
                    Toast.makeText(v.getContext(), strRunner + " already in preempt list!", Toast.LENGTH_SHORT).show();
                }
                else {
                    databaseHelper.insertSN(strRunner, lngCurrentId);
                    // Add a button and initialise it
                    ToggleButton btnRunner = new ToggleButton(ctxThis);
                    btnRunner.setText(strRunner);
                    btnRunner.setTextOn(strRunner);
                    btnRunner.setTextOff(strRunner);
                    btnRunner.setTag(lngCurrentId);
                    btnRunner.setLayoutParams(params);
                    btnRunner.setOnClickListener(RaceSheetActivity.btnRunnerOnClickListener);
                    RaceSheetActivity.llRunnerList.addView(btnRunner); // to delete view removeViewAt(int index)
                    changeCursor(databaseHelper.getRaceSheetDataForRace(lngRaceId));
                    notifyDataSetChanged();
                 }
            }
        }
    };

Would really appreciate any ideas as to how I can keep the listview visible after tapping the runner field and the button appearing. I have tried notifyDatasetChanged without success, reread the database table into the cursor adapter.

Was it helpful?

Solution 3

Actually, solved the problem eventually. The listview was disappearing because I had used a cursor to get the adapter values for the row that was returned, and subsequently closing the cursor apparently cleared the listview.

Thanks to both those who offered solutions albeit they weren't the cause of the problem.

OTHER TIPS

Set your layout heights to wrap_content because your ItemList with the buttons will overlap your Listview if it's not empty.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/llItemList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>

        <ListView
            android:id="@+id/list_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</ScrollView>

Actually you are not initializing the HorizontalScrll view And just close the Horizontalscrollview due to that App return this type of behaviour

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top