Dynamically add ListView in the code to ScrollView

心不动则不痛 提交于 2019-12-02 09:41:38

Do not add a ListView to a ScrollView if you don't know exaclty what you are doing.

Do not add a ListView to a ScrollView also if you are the best android programmer in the World.

ListViews are not supposed to get in ScrollView. Even if you hack, and i mean big hack, both views by extending them and got them to scroll , also be itemclickable etc etc , something will break eventually. Also do not add a ListView to a ScrollView.

Watch this

You should first get the ListView in you layout using findViewById(android.R.id.list) then you cant add the items into ListView and then add this to scrollview.

You cannot get reference to the ListView this way. You should execute findViewById(/Id of the listview element goes here/).

Edit:

Suppose you have:

<ListView android:layout_height="wrap_content" android:id="@+id/listView1" android:layout_weight="1" android:layout_width="fill_parent"></ListView>

You can get access to the listview by:

ListView l = findViewById(R.id.listView1);

Hope this helps!

Got the solution....here is the complete the code...

package net.pocketmagic.android.HeaderContentFooterGUI;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class HeaderContentFooterGUI extends Activity {

    final static int        idTopLayout = Menu.FIRST + 100,
                            idBack      = Menu.FIRST + 101,
                            idBotLayout = Menu.FIRST + 102;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Hide titlebar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Create our top content holder
        RelativeLayout global_panel = new RelativeLayout (this);
        global_panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        global_panel.setGravity(Gravity.FILL);

        // +++++++++++++ TOP COMPONENT: the header
        RelativeLayout ibMenu = new RelativeLayout(this);
        ibMenu.setId(idTopLayout);
        ibMenu.setBackgroundDrawable(getResources().getDrawable(R.drawable.line));
        int ibMenuPadding = (int) 6;
        ibMenu.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        topParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        global_panel.addView(ibMenu,topParams);
        // textview in ibMenu : card holder
        TextView cTV = new TextView(this);
        cTV.setText("Abirami Mega Mall");
        cTV.setTextColor(Color.rgb(255,255,255));
        int nTextH =  18;
        cTV.setTextSize(nTextH);
        cTV.setTypeface(Typeface.create("arial", Typeface.BOLD));
        RelativeLayout.LayoutParams lpcTV = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lpcTV.addRule(RelativeLayout.CENTER_IN_PARENT);
        ibMenu.addView(cTV, lpcTV);

        // +++++++++++++ BOTTOM COMPONENT: the footer
        nTextH =  13;
         ibMenuPadding = (int) 15;
        RelativeLayout ibMenuBot = new RelativeLayout(this);
        ibMenuBot.setId(idBotLayout);
        ibMenuBot.setBackgroundDrawable(getResources().getDrawable(R.drawable.line));
        ibMenuBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams botParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        botParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        global_panel.addView(ibMenuBot,botParams);
        // textview in ibMenu : card holder

        TextView cTVBot = new TextView(this);
        cTVBot.setId(5001);
        cTVBot.setText("Home"); 
        cTVBot.setTextColor(Color.rgb(179,116,197));
        cTVBot.setTextSize(nTextH);
        cTVBot.setTypeface(Typeface.create("arial", Typeface.NORMAL));
        cTVBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);   
        lpcTVBot.addRule(RelativeLayout.CENTER_VERTICAL);
        ibMenuBot.addView(cTVBot, lpcTVBot);

        TextView cTVBot2 = new TextView(this);
        cTVBot2.setId(5002);        
        cTVBot2.setText("Booking History");     
        cTVBot2.setTextColor(Color.rgb(179,116,197));
        cTVBot2.setTextSize(nTextH);
        cTVBot2.setTypeface(Typeface.create("arial", Typeface.NORMAL));
        cTVBot2.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);      
        lpcTVBot2.addRule(RelativeLayout.RIGHT_OF,cTVBot.getId() );
        lpcTVBot2.addRule(RelativeLayout.CENTER_VERTICAL);      
        ibMenuBot.addView(cTVBot2, lpcTVBot2);

        TextView cTVBot3 = new TextView(this);
        cTVBot3.setId(5003);        
        cTVBot3.setText("Help?");       
        cTVBot3.setTextColor(Color.rgb(179,116,197));
        cTVBot3.setTextSize(nTextH);
        cTVBot3.setTypeface(Typeface.create("arial", Typeface.NORMAL));
        cTVBot3.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);      
        lpcTVBot3.addRule(RelativeLayout.RIGHT_OF,cTVBot2.getId() );
        lpcTVBot3.addRule(RelativeLayout.CENTER_VERTICAL);      
        ibMenuBot.addView(cTVBot3, lpcTVBot3);      

        TextView cTVBot4 = new TextView(this);
        cTVBot4.setId(5004);        
        cTVBot4.setText("About Us");        
        cTVBot4.setTextColor(Color.rgb(179,116,197));
        cTVBot4.setTextSize(nTextH);
        cTVBot4.setTypeface(Typeface.create("arial", Typeface.NORMAL));
        cTVBot4.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot4= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       
        lpcTVBot4.addRule(RelativeLayout.RIGHT_OF,cTVBot3.getId() );
        lpcTVBot4.addRule(RelativeLayout.CENTER_VERTICAL);      
        ibMenuBot.addView(cTVBot4, lpcTVBot4);      


        // +++++++++++++ MIDDLE COMPONENT: all our GUI content
        LinearLayout midLayout = new LinearLayout (this);
        midLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        midLayout.setOrientation(LinearLayout.VERTICAL);
        RelativeLayout.LayoutParams midParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        midParams.addRule(RelativeLayout.ABOVE,ibMenuBot.getId());
        midParams.addRule(RelativeLayout.BELOW,ibMenu.getId());
        global_panel.addView(midLayout,midParams );


        ListView maListViewPerso = new ListView(this);
        maListViewPerso.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;     
        map = new HashMap<String, String>();
        map.put("titre", "Movie Ticket");
        map.put("description", "Movie Ticket Booking");
        map.put("img", String.valueOf(R.drawable.reel_icon));
        listItem.add(map);


        map = new HashMap<String, String>();
        map.put("titre", "Snow World");
        map.put("description", "SnowWorld Tickets");
        map.put("img", String.valueOf(R.drawable.reel_icon));
        listItem.add(map);

        map = new HashMap<String, String>();
        map.put("titre", "Kissing Car");
        map.put("description", "Kissing Car Tickets");
        map.put("img", String.valueOf(R.drawable.reel_icon));
        listItem.add(map);

        map = new HashMap<String, String>();
        map.put("titre", "Events");
        map.put("description", "Events in Mall");
        map.put("img", String.valueOf(R.drawable.reel_icon));
        listItem.add(map);

        map = new HashMap<String, String>();
        map.put("titre", "Screens");
        map.put("description", "Screens @ Abirami");
        map.put("img", String.valueOf(R.drawable.reel_icon));
        listItem.add(map);


        SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.main_menu_list,
               new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});


        maListViewPerso.setAdapter(mSchedule);
        midLayout.addView(maListViewPerso);



        setContentView(global_panel);
    }


}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!