Dynamically add ListView in the code to ScrollView
I am trying to add a ListView
to a ScrollView
inside the code. But it's not working for some reason. I am new to this. Can somebody help me out?
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.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
/* HeaderContentFooterGUI
*
* Create a sample GUI with a header bar, scroll-able content and a footer
*
* (C) 2011 Radu Motisan, radu.motisan@gmail.com
* www.pocketmagic.net
* All rights reserved.
*/
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 );
//scroll - so our content will be scrollable between the header and the footer
ScrollView vscroll = new ScrollView(this);
vscroll.setId(6004);
vscroll.setFillViewport(false);
midLayout.addView(vscroll);
//below code not working *******
ListView maListViewPerso = (ListView) findViewById(vscroll.getId());
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 Strin开发者_JAVA技巧g[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
maListViewPerso.setAdapter(mSchedule);
setContentView(global_panel);
}
}
main_menu_list.xml
------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/img"
android:layout_gravity="center_vertical"
android:padding="10px"
android:layout_width="70dp"
android:layout_height="70dp"/>
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10px"
android:paddingRight="10px"
android:layout_weight="1"
>
<TextView android:id="@+id/titre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="26px"
android:textStyle="bold"
/>
<TextView android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
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);
}
}
精彩评论