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 String[] {"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);
}
}
Related
I am trying to fetch the database using php,i fetched and shown in dynamic table layout,the rows where created according to the number of rows in the database,but the table layout is in center of the screen but i need it to be fit in whole screen of the mobile/tab.
i tried to change the height and width of the all layout used in the particular screen but nothing is changed can anyone help me to fix this.
this is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Colleagues_Schedule">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3d455b"
android:layout_alignParentLeft="true" >
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table_main"
android:layout_margin="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:onClick="Back"
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PREVIOUS" />
<Button
android:onClick="Home"
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HOME" />
<Button android:onClick="Next"
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
main activity
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.nex3z.notificationbadge.NotificationBadge;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import static java.util.Calendar.DATE;
public class Colleagues_Schedule extends AppCompatActivity {
int i;
String result;
int size=20;
Context context;
int count = 0;
NotificationBadge mBadge;
Button buttonname, btnlogout, btntoday, btnyesterday, btntomorrow;
JSONArray jsonArray_New = null;
String[] ScheduleDates, StartTimes, Endtimes,Names;
String Send_Yesterday, Send_Today, Send_Tomorrow;
String ScheduleDate, StartTime, Endtime, Today, Yesterday, Tomarrow, Date,Name;
java.util.Date date1, Todaydate, Yesterdaydate, Tomarrowdate,date2;
TextView yes_date, yes_ST, yes_ET, tod_date, tod_ST, tod_ET, tom_date, tom_ST, tom_ET;
private ArrayList<String> ScheduleDateArray = new ArrayList<>();
private ArrayList<String> StartTimeArray = new ArrayList<>();
private ArrayList<String> EndtimeArray = new ArrayList<>();
private ArrayList<String> NameArray = new ArrayList<>();
SharedPreferences mPrefs;
GridLayout mainGrid2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_colleagues__schedule);
result = getIntent().getStringExtra("rootJson");
init();
}
#SuppressLint("ResourceType")
public void init() {
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Name ");//date
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Date ");//name
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Start Time ");//starttime
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" End Time ");//endtime
tv3.setTextColor(Color.WHITE);
tbrow0.addView(tv3);
stk.addView(tbrow0);
try {
JSONObject jsonObjects = new JSONObject(result);
jsonArray_New = jsonObjects.getJSONArray("VIEW_SCHEDULE");
//Fetching the data from the php file
try {
for (int i = 0; i < jsonArray_New.length(); i++) {
JSONObject jsonObject = jsonArray_New.getJSONObject(i);
Name = jsonObject.getString("Name");
NameArray.add(Name);
ScheduleDate = jsonObject.getString("ScheduleDate");
ScheduleDateArray.add(ScheduleDate);
StartTime = jsonObject.getString("StartTime");
StartTimeArray.add(StartTime);
Endtime = jsonObject.getString("Endtime");
EndtimeArray.add(Endtime);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Initilize the arraylist to array
Names = new String[NameArray.size()];
ScheduleDates = new String[ScheduleDateArray.size()];
StartTimes = new String[StartTimeArray.size()];
Endtimes = new String[EndtimeArray.size()];
for (int i = 0; i < ScheduleDateArray.size(); i++) {
Names[i] = NameArray.get(i);
ScheduleDates[i] = ScheduleDateArray.get(i);
StartTimes[i] = StartTimeArray.get(i);
Endtimes[i] = EndtimeArray.get(i);
}
//Search for Today schedule
for(int i=0;i<jsonArray_New.length();i++)
{
TableRow tbrow = new TableRow(this);
tbrow.setBackgroundResource(R.drawable.row_border);
TextView t1v = new TextView(this);
TextView t2v = new TextView(this);
TextView t3v = new TextView(this);
TextView t4v = new TextView(this);
tbrow.setBackgroundResource(R.drawable.row_border);
t1v.setText(Names[i]);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
t2v.setText(ScheduleDates[i] );
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
t3v.setText( StartTimes[i]);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
t4v.setText(Endtimes[i]);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
stk.addView(tbrow);
tbrow.setClickable(true); //allows you to select a specific row
tbrow.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TableRow tablerow = (TableRow) view;
TextView sample = (TextView) tablerow.getChildAt(1);
String result=sample.getText().toString();
Toast toast = Toast.makeText(Colleagues_Schedule.this, result, Toast.LENGTH_LONG);
toast.show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
and this is my output scrren.
output screen
Try giving layout params to every row you add dynamically and update your init() function like below:
#SuppressLint("ResourceType")
public void init() {
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
//added layout params and weight
tbrow0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
TextView tv0 = new TextView(this);
tv0.setText(" Name ");//date
tv0.setTextColor(Color.WHITE);
//added layout params and weight
tv0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Date ");//name
tv1.setTextColor(Color.WHITE);
//added layout params and weight
tv1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Start Time ");//starttime
tv2.setTextColor(Color.WHITE);
//added layout params and weight
tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" End Time ");//endtime
tv3.setTextColor(Color.WHITE);
//added layout params and weight
tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv3);
//added layout params and weight
stk.addView(tbrow0, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
try {
JSONObject jsonObjects = new JSONObject(result);
jsonArray_New = jsonObjects.getJSONArray("VIEW_SCHEDULE");
//Fetching the data from the php file
try {
for (int i = 0; i < jsonArray_New.length(); i++) {
JSONObject jsonObject = jsonArray_New.getJSONObject(i);
Name = jsonObject.getString("Name");
NameArray.add(Name);
ScheduleDate = jsonObject.getString("ScheduleDate");
ScheduleDateArray.add(ScheduleDate);
StartTime = jsonObject.getString("StartTime");
StartTimeArray.add(StartTime);
Endtime = jsonObject.getString("Endtime");
EndtimeArray.add(Endtime);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Initilize the arraylist to array
Names = new String[NameArray.size()];
ScheduleDates = new String[ScheduleDateArray.size()];
StartTimes = new String[StartTimeArray.size()];
Endtimes = new String[EndtimeArray.size()];
for (int i = 0; i < ScheduleDateArray.size(); i++) {
Names[i] = NameArray.get(i);
ScheduleDates[i] = ScheduleDateArray.get(i);
StartTimes[i] = StartTimeArray.get(i);
Endtimes[i] = EndtimeArray.get(i);
}
//Search for Today schedule
for (int i = 0; i < jsonArray_New.length(); i++) {
TableRow tbrow = new TableRow(this);
tbrow.setBackgroundResource(R.drawable.row_border);
//added layout params and weight
tbrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
TextView t1v = new TextView(this);
TextView t2v = new TextView(this);
TextView t3v = new TextView(this);
TextView t4v = new TextView(this);
tbrow.setBackgroundResource(R.drawable.row_border);
t1v.setText(Names[i]);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
//added layout params and weight
t1v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t1v);
t2v.setText(ScheduleDates[i]);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
//added layout params and weight
t2v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t2v);
t3v.setText(StartTimes[i]);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
//added layout params and weight
t3v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t3v);
t4v.setText(Endtimes[i]);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
//added layout params and weight
t4v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t4v);
//added layout params and weight
stk.addView(tbrow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
tbrow.setClickable(true); //allows you to select a specific row
tbrow.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TableRow tablerow = (TableRow) view;
TextView sample = (TextView) tablerow.getChildAt(1);
String result = sample.getText().toString();
Toast toast = Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG);
toast.show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
And provide android:fillViewport="true" to your ScrollView with id=scrollView1 and HorizontalScrollView with id=hscrll1
Based on the source code from this webpage, it doesn't work because the data isn't displayed in the table. I also have tried a similar code but it didn't work.
What part am I missing?
The result don't display without any error message.
Today, I'm a newbie in Android.
Info:
*using Android studio with API 25
Thank you!
XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jfdimarzio.myapplication.MainActivity">
</TableLayout>
Java
package com.jfdimarzio.myapplication;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init()
{
TableLayout stk = (TableLayout) findViewById(R.id.activity_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Sl.No ");
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Product ");
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Unit Price ");
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" Stock Remaining ");
tv3.setTextColor(Color.WHITE);
tbrow0.addView(tv3);
stk.addView(tbrow0);
for (int i = 0; i < 25; i++) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setText("" + i);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("Product " + i);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText("Rs." + i);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText("" + i * 15 / 32 * 10);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
stk.addView(tbrow);
}
}
}
I am trying to programmatically create this xml view by extending tablerow view:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:weightSum="1.0" >
<Button
android:id="#+id/tester_button"
style="?android:attr/buttonStyleSmall"
android:layout_weight=".2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="A" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_weight=".7"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_weight=".1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
</TableRow>
This is my class of the extended row:
public class CategoryRow extends TableRow {
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
LayoutParams innerParams = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
CheckBox check;
Button arrowButton;
TextView categoryTitle;
public CategoryRow(Context context) {
super(context);
this.setLayoutParams(rowParams);
this.setGravity(Gravity.CENTER);
this.setWeightSum(1);
check = new CheckBox(context);
innerParams.weight = (float) 0.1;
check.setLayoutParams(innerParams);
arrowButton = new Button(context);
innerParams.weight = (float) 0.2;
arrowButton.setLayoutParams(innerParams);
arrowButton.setText("A");
categoryTitle = new TextView(context);
innerParams.weight = (float) 0.7;
categoryTitle.setLayoutParams(innerParams);
categoryTitle.setTextAppearance(context, android.R.attr.textAppearanceLarge);
//add views to row
this.addView(arrowButton);
this.addView(categoryTitle);
this.addView(check);
}
public void setCategoryTitle(String title){
categoryTitle.setText(title);
}
public void checkCategory(){
check.setChecked(true);
}
public void unCheckCategory(){
check.setChecked(false);
}
}
in my activity I am adding the CategoryRow view to the TableLayout by addView function.
public class NotificationCategorise extends Activity {
TableLayout categoryConteiner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_categorise);
categoryConteiner = (TableLayout) findViewById(R.id.categories_conteiner);
CategoryRow categoryRow = new CategoryRow(this);
categoryRow.setCategoryTitle("bla");
categoryConteiner.addView(categoryRow);
}
My problem is that it the CategoryRow not added to the activity sceen.
I have changed the extended class to this code, and it worked fine.
import android.content.Context;
import android.view.Gravity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableRow;
import android.widget.TextView;
public class CategoryRow extends TableRow {
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams innerParams1 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
LayoutParams innerParams2 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
LayoutParams innerParams3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
CheckBox check;
Button arrowButton;
TextView categoryTitle;
public CategoryRow(Context context) {
super(context);
this.setLayoutParams(rowParams);
this.setWeightSum(1f);
check = new CheckBox(context);
innerParams1.weight = 0.1f;
check.setLayoutParams(innerParams1);
arrowButton = new Button(context);
innerParams2.weight = 0.2f;
arrowButton.setLayoutParams(innerParams2);
arrowButton.setText("A");
categoryTitle = new TextView(context);
categoryTitle.setGravity(Gravity.CENTER);
innerParams3.weight = 0.7f;
categoryTitle.setLayoutParams(innerParams3);
categoryTitle.setTextAppearance(context, android.R.attr.textAppearanceLarge);
//add views to row
this.addView(arrowButton);
this.addView(categoryTitle);
this.addView(check);
}
public void setCategoryTitle(String title){
categoryTitle.setText(title);
}
public void checkCategory(){
check.setChecked(true);
}
public void unCheckCategory(){
check.setChecked(false);
}
}
Ok, I don't think you can pass in the GRAVITY.CENTER as an argument for the LayoutParams constructor for the row. You change it for each individual View added to the row, but not for the row itself. Change:
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
to
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
See if that works.
I want to create inner tablelayout and add there 1 row with 2 columns, following code shows nothing, why?
Here's main activity:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
TextView tv2=new TextView(this);
tv1.setLayoutParams(params);
tv2.setLayoutParams(params);
tv1.setText("Hello1!");
tv2.setText("Hello2!");
TableLayout layoutINNER = new TableLayout(this);
layoutINNER.setLayoutParams(params);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
tr.addView(tv1);
tr.addView(tv2);
layoutINNER.addView(tr);
LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
main.addView(layoutINNER);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/android:main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
UPDATE:
Well, finally i solved this problem, it was just needed to use TableRow.LayoutParams with TextViews, not LinearLayout.LayoutParams or some other
Here is the working code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
TextView tv2=new TextView(this);
android.widget.TableRow.LayoutParams trparams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(trparams);
tv2.setLayoutParams(trparams);
tv1.setText("Hello1!");
tv2.setText("Hello2!");
TableLayout layoutINNER = new TableLayout(this);
layoutINNER.setLayoutParams(params);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
tr.addView(tv1);
tr.addView(tv2);
layoutINNER.addView(tr);
LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
main.addView(layoutINNER);
}
}
I want to display different children in each section of Expandable list in android. So i have written following lines:
//SmplExpandable.java
package com.bogotobogo.android.smplexpandable;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HeterogeneousExpandableList;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Demonstrates expandable lists backed by a Simple Map-based adapter
*/
public class SmplExpandable extends ExpandableListActivity implements HeterogeneousExpandableList{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>();
List<List<Map<String, Object>>> childData = new ArrayList<List<Map<String, Object>>>();
Map<String, Object> curGroupMap = new HashMap<String, Object>();
curGroupMap = new HashMap<String, Object>();
curGroupMap.put("Name","Recipe");
groupData.add(curGroupMap);
curGroupMap = new HashMap<String, Object>();
curGroupMap.put("Name","Feedback");
groupData.add(curGroupMap);
curGroupMap = new HashMap<String, Object>();
curGroupMap.put("Name","Images");
groupData.add(curGroupMap);
curGroupMap = new HashMap<String, Object>();
curGroupMap.put("Name","Video");
groupData.add(curGroupMap);
List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
Map<String, Object> curChildMap = new HashMap<String, Object>();
curChildMap = new HashMap<String, Object>();
curChildMap.put("MyRecipe",getResources().getString(R.string.My_Recipe));
children.add(curChildMap);
childData.add(children);
children = new ArrayList<Map<String, Object>>();
curChildMap = new HashMap<String, Object>();
curChildMap.put("MyFeedback", getResources().getString(R.string.My_Feedback));
children.add(curChildMap);
childData.add(children);
children = new ArrayList<Map<String, Object>>();
curChildMap = new HashMap<String, Object>();
curChildMap.put("MyImage", getResources().getDrawable(R.drawable.icon));
children.add(curChildMap);
childData.add(children);
children = new ArrayList<Map<String, Object>>();
curChildMap = new HashMap<String, Object>();
curChildMap.put("MyImage", getResources().getDrawable(R.drawable.icon));
children.add(curChildMap);
childData.add(children);
// Set up our adapter
setListAdapter( new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] {"Name"}, // the name of the field data
new int[] { android.R.id.text1 }, // the text field to populate with the field data
childData,
0,
null,
new int[] {}
)
{
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
if (groupPosition == 0)
{
// Populate your custom view here
((TextView)v.findViewById(R.id.TextView01)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyRecipe") );
}
else if (groupPosition == 1)
{
// Populate your custom view here
((TextView)v.findViewById(R.id.TextView02)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyFeedback") );
}
else if (groupPosition == 2)
{
// Populate your custom view here
((ImageView)v.findViewById(R.id.ImageView01)).setImageDrawable((Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyImage"));
}
else if (groupPosition == 3)
{
// Populate your custom view here
((ImageView)v.findViewById(R.id.ImageView01)).setImageDrawable((Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyImage"));
}
return v;
}
#Override
public View newChildView(boolean isLastChild, ViewGroup parent) {
{
return layoutInflater.inflate(R.layout.myrow, null, false);
}
}
}
);
// Set the width and height of activity.
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height =400;
params.width = 400;
this.getWindow().setAttributes(params);
}
}
File myrow.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/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:id="#+id/TextView01"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:textColor="#FF0000"
android:textSize="10px"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/TextView02"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:textColor="#FF0000"
android:textSize="10px"
android:layout_height="fill_parent"/>
<VideoView android:id="#+id/VideoView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</VideoView>
</LinearLayout>
My problem is that i am able to see both text and image in all the four sections, whereas i want to display text only in first two sections and image in other two sections.
Can anybody tell me that what i am doing wrong?
Thanks,
Neha
Your problem is simple.
You are inflating a view in the
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
method of your adapter.
However this inflated view contains both a text view and an image view. If you only want to display one, you need to hide the other.
if (groupPosition == 0)
{
// set your text hide
// HIDE YOUR IMAGE VIEW
ImageView iv = ...
iv.setVisibility(View.GONE);
}
...
if (groupPosition == 3)
{
// set your image source
// HIDE YOUR TEXT VIEW
TextView tv = ...
t.setVisibility(View.GONE);
}
That should solve your problem and you should be able to finish it from here :)