My ExpandableListView behaves normally on my Galaxy Note 3 as pictured below:
But on my Nexus 7 and Galaxy Note 10.1, there's a bunch of extra space that appears when I expand the list to show a child. (The space does not appear until I click a parent.)
From Nexus 7:
My first guess is that there is something inconsistent with my layout resources (I had different layout resources for sw-600 and sw-700) but removing the larger screen resources, the behaviour is the same. I also commented out everything in the getGroupView and getChildView methods in my adapter class other than inflating the layouts, and still this behaviour. So now I have no idea what is causing this behaviour. I guess the other significant detail is that I'm doing this in an AlertDialog, but it doesn't explain why it only seems to happen on larger devices.
Initializing the ExpandableListAdapter and AlertDialog:
private void showServicesDialog(final ArrayList<Service> services) {
View dView = activity.getLayoutInflater().inflate(R.layout.expandable_list_view, null);
View header = activity.getLayoutInflater().inflate(R.layout.service_list_header, null);
AlertDialog servicesDialog = DialogHelper.createLayoutAlertDialog(activity, null, dView,
R.string.cancel, R.string.add_services,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (selectedServices.size() > 0) {
addServicesToBackend();
}
}
});
ExpandableListView list = (ExpandableListView) dView.findViewById(R.id.expandableListView);
ServicesExpandableListAdapter adapter = new ServicesExpandableListAdapter(activity, services);
list.setGroupIndicator(null);
list.setSelector(R.drawable.list_selector_bg);
list.setDivider(null);
list.addHeaderView(header);
list.setAdapter(adapter);
servicesDialog.show();
}
From my Adapter class:
public ServicesExpandableListAdapter(Activity act, ArrayList<Service> s) {
activity = (MyActivity) act;
services = s;
itemChecked = new ArrayList<Boolean>();
for (int i = 0; i < services.size(); i++)
itemChecked.add(false);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return services.get(groupPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return services.get(groupPosition);
}
#Override
public int getGroupCount() {
return services.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private static class ParentViewHolder {
TextView nameTV, categoryTV, expandTV, typeTV, amountTV, warningTV;
CheckBox checkbox;
}
#Override
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ParentViewHolder holder = null;
final Service service = services.get(groupPosition);
if (convertView == null) {
convertView = activity.getLayoutInflater().inflate(R.layout.service_list_parent_item, null);
holder = new ParentViewHolder();
holder.nameTV = (TextView) convertView.findViewById(R.id.nameTV);
holder.typeTV = (TextView) convertView.findViewById(R.id.typeTV);
holder.categoryTV = (TextView) convertView.findViewById(R.id.categoryTV);
holder.expandTV = (TextView) convertView.findViewById(R.id.expandTV);
holder.amountTV = (TextView) convertView.findViewById(R.id.amountTV);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.selectCB);
holder.warningTV = (TextView) convertView.findViewById(R.id.warningTV);
convertView.setTag(holder);
} else {
holder = (ParentViewHolder) convertView.getTag();
}
//content commented out
return convertView;
}
private static class ChildViewHolder {
TextView typeTV, priceTV, infoTV;
CheckBox weekdaysCB, saturdaysCB, sundaysCB, holidaysCB;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder holder = null;
if (convertView == null) {
convertView = activity.getLayoutInflater().inflate(R.layout.service_list_child_item, null);
holder = new ChildViewHolder();
holder.typeTV = (TextView) convertView.findViewById(R.id.typeTV);
holder.priceTV = (TextView) convertView.findViewById(R.id.priceTV);
holder.infoTV = (TextView) convertView.findViewById(R.id.infoTV);
holder.weekdaysCB = (CheckBox) convertView.findViewById(R.id.weekdaysCB);
holder.saturdaysCB = (CheckBox) convertView.findViewById(R.id.saturdaysCB);
holder.sundaysCB = (CheckBox) convertView.findViewById(R.id.sundaysCB);
holder.holidaysCB = (CheckBox) convertView.findViewById(R.id.holidaysCB);
convertView.setTag(holder);
} else {
holder = (ChildViewHolder) convertView.getTag();
}
//content commented out
return convertView;
}
service_list_parent_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:id="#+id/topDivider"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignParentTop="true"
android:background="#color/gray_divider" />
<TextView
android:id="#+id/nameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="#+id/warningTV"
android:text="Service name"
android:textAppearance="#style/regularText" />
<TextView
android:id="#+id/typeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nameTV"
android:layout_below="#+id/nameTV"
android:layout_marginBottom="3dp"
android:layout_marginRight="5dp"
android:background="#color/holo_light_purple"
android:drawableLeft="#drawable/ic_clock_tiny_white" />
<TextView
android:id="#+id/categoryTV"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/typeTV"
android:layout_marginBottom="3dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/expandTV"
android:layout_toRightOf="#+id/typeTV"
android:ellipsize="end"
android:lines="1"
android:text="Category Name that is super long" />
<TextView
android:id="#+id/warningTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/selectCB"
android:layout_marginRight="5dp"
android:background="#drawable/action_bar_selector"
android:drawableRight="#drawable/ic_warning_gray"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="5dp"
android:visibility="invisible" />
<CheckBox
android:id="#+id/selectCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="5dp" />
<TextView
android:id="#+id/amountTV"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/blue_button_selector"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:text="0.0"
android:textColor="#color/white"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/expandTV"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/bottomDivider"
android:drawableRight="#drawable/ic_expand_arrow_gray"
android:gravity="bottom"
android:paddingBottom="5dp"
android:text="#string/expand"
android:textColor="#color/lighter_gray_text" />
<View
android:id="#+id/bottomDivider"
android:layout_width="90dp"
android:layout_height="3dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="60dp"
android:background="#color/blue" />
</RelativeLayout>
service_list_child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/gray_tasklist" >
<View
android:id="#+id/topDivider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentTop="true"
android:background="#color/blue" />
<View
android:id="#+id/centerView"
android:layout_width="1px"
android:layout_height="1px"
android:layout_below="#+id/topDivider"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/typeTV"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/topDivider"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/centerView"
android:drawableLeft="#drawable/ic_clock_tiny_purple"
android:drawablePadding="4dp"
android:gravity="center_vertical"
android:text="#string/time_service"
android:textColor="#color/holo_light_purple" />
<TextView
android:id="#+id/priceTV"
style="#style/regularTextSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/typeTV"
android:layout_marginLeft="35dp"
android:layout_toRightOf="#+id/centerView"
android:text="35e/hr"
android:textColor="#color/blue" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/typeTV"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" >
<CheckBox
android:id="#+id/weekdaysCB"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:enabled="false"
android:text="#string/weekdays" />
<CheckBox
android:id="#+id/holidaysCB"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/weekdaysCB"
android:layout_below="#+id/weekdaysCB"
android:enabled="false"
android:text="#string/holidays" />
<CheckBox
android:id="#+id/saturdaysCB"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/weekdaysCB"
android:layout_toRightOf="#+id/weekdaysCB"
android:enabled="false"
android:text="#string/saturdays" />
<CheckBox
android:id="#+id/sundaysCB"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/holidaysCB"
android:layout_alignLeft="#+id/saturdaysCB"
android:enabled="false"
android:text="#string/sundays" />
</RelativeLayout>
<TextView
android:id="#+id/infoTV"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:padding="5dp" />
</RelativeLayout>
And lastly, expandable_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/gray_tasklist"
android:orientation="vertical" >
<ExpandableListView
android:id="#+id/expandableListView"
style="#style/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/gray_divider"
android:dividerHeight="1px"
android:childDivider="#00000000"/>
</LinearLayout>
Related
I am new here so please bear with my naivety. I am working on a music player that will display a menu on clicking 'dots' buttton(given in code below) in listview item. Inside the menu there will be a button to delete. The listview is getting populated by a custom adapter. Here is the code of adapter...
private class hashmapAdapter extends BaseAdapter implements Filterable {
HashMap<Integer, NowPlayingActivity.Details> NewDetails = new HashMap<>();
private hashmapAdapter(HashMap<Integer, NowPlayingActivity.Details> hashMap) {
if(NewDetails.size()==0) {
NewDetails.putAll(hashMap);
}
}
#Override
public int getCount() {
return NewDetails.size();
}
#Override
public Object getItem(int position) {
return NewDetails.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View result;
if(convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item,parent,false);
} else {
result = convertView;
}
((TextView)result.findViewById(R.id.Name)).setText(NewDetails.get(position).getName());
((TextView)result.findViewById(R.id.artist)).setText(NewDetails.get(position).getArtist());
((TextView)result.findViewById(R.id.duration)).setText(NewDetails.get(position).getDuration()+" || ");
dots = (Button)result.findViewById(R.id.dots);
dots.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = (TextView)result.findViewById(R.id.Name);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View popup_view = inflater.inflate(R.layout.option_layout,null);
RelativeLayout item = (RelativeLayout) popup_view.findViewById(R.id.popup);
final float width= (getResources().getDimension(R.dimen.width_entry_in_dp));
final float height= getResources().getDimension(R.dimen.height_entry_in_dp);
final PopupWindow popupWindow = new PopupWindow(popup_view,Math.round(width),Math.round(height),true);
popupWindow.showAtLocation(item, Gravity.END, 0, 0);
popupWindow.showAsDropDown(dots);
play_next_btn = (Button)popup_view.findViewById(R.id.play_next);
add_to_playlist = (Button)popup_view.findViewById(R.id.add_to_playlist);
share = (Button)popup_view.findViewById(R.id.share);
delete = (Button)popup_view.findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<NewDetails.size();i++) {
if(NewDetails.get(i).getName().equals(name.getText().toString())) {
for(int j=0;j<hashMap.size();j++) {
if(hashMap.get(j).getName().equals(name.getText().toString())) {
if (true) {
NewDetails.remove(i);
hashMap.remove(j);
}
}
}
}
}
populate1(NewDetails);
popupWindow.dismiss();
}
});
}
});
return result;}
Now the problem is that after deleting element when i call populate1(NewDetails), it gives a null pointer exception on NewDetails.get(position).getName(). So on repopulating listview i am getting NPE error. Following is the populate1() function code...
public void populate1(HashMap<Integer,NowPlayingActivity.Details> hashMap) {
adapter = new hashmapAdapter(hashMap);
library.setAdapter(adapter);
adapter.notifyDataSetChanged();
library.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
name = (TextView)view.findViewById(R.id.Name);
String SongName = name.getText().toString();
Intent intent = new Intent(getApplicationContext(),NowPlayingActivity.class);
intent.putExtra("SongName",SongName);
setResult(555,intent);
finish();
}
});
}
I have already tried notifydatasetchanged() and listview.invalidateviews(), both didnt work. File is getting deleted but the adapter is not refreshing the list to remove element. Here is the xml layout of single_item for listview....`
`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="70dp"
android:paddingRight="8dp"
android:paddingEnd="8dp"
android:background="#drawable/element_shape_list"
android:descendantFocusability="blocksDescendants">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/v1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:foreground="?android:attr/selectableItemBackground">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/v11"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/art_work"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#mipmap/ic_launcher"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"/>
<TextView
android:id="#+id/Name"
android:textSize="17sp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_toRightOf="#id/art_work"
android:layout_toEndOf="#+id/art_work"
android:singleLine="true"
/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#+id/art_work"
android:layout_toEndOf="#id/art_work"
android:layout_below="#id/Name">
<TextView
android:id="#+id/duration"
android:textSize="12sp"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_height="20dp"
android:singleLine="true"
android:gravity="clip_horizontal"
android:ellipsize="end"/>
<TextView
android:id="#+id/artist"
android:textSize="12sp"
android:textColor="#000"
android:layout_toRightOf="#+id/duration"
android:layout_toEndOf="#+id/duration"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:gravity="fill_horizontal"
android:layout_height="20dp"
android:singleLine="true" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent">
<Button
android:id="#+id/dots"
android:layout_width="40dp"
android:textStyle="bold"
android:background="?android:attr/selectableItemBackground"
android:layout_height="70dp"
android:text="#string/vertical_ellipsis"
android:textSize="25sp" />
</FrameLayout>
</RelativeLayout>
I have searched for this issue a few days. Here's the structure of the app.
activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- The main content view -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<fragment android:id="#+id/fragment_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="ca.bluecross.ab.view.fragments.DrawerFragment"
tools:layout="#layout/abc_drawer_layout" />
</android.support.v4.widget.DrawerLayout>
Here's the layout of the fragment (some elements are not included to save space for post):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:id="#+id/scrollView"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
..
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="5dp"
android:background="#21c1c1c1">
..
</RelativeLayout>
..
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/expLVClaims"
/>
.
..
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TableRow
..
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
..
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
..
</TableRow>
</TableLayout>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="I have more to add"
android:id="#+id/btnAddMoreClaim"
android:layout_gravity="center_horizontal"
android:layout_span="2"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"
android:background="#color/abc_blue"
android:textColor="#FFFFFF" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="I'm ready to submit"
android:id="#+id/btnReadyToSubmitClaim"
android:layout_span="2"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/btnAddMoreClaim"
android:background="#color/abc_blue"
android:textColor="#FFF" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/btnCancelEnterClaim"
android:layout_alignWithParentIfMissing="false"
android:layout_centerInParent="true"
android:background="#color/abc_blue"
android:textColor="#FFF" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
Here's the layout for group header:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/imgIndicator"
android:src="#drawable/icon_collapse"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Provider name"
android:id="#+id/txtProviderName"
android:layout_toRightOf="#+id/imgIndicator" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/imgDelete"
android:src="#drawable/icon_cross_delete"
android:layout_toLeftOf="#+id/imgEdit" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/imgEdit"
android:layout_alignParentEnd="true"
android:src="#drawable/icon_edit" />
</RelativeLayout>
Here's the child layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#59868686">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutServiceDate">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of service"
android:id="#+id/txtClaimDetailServiceDateLabel"
android:layout_alignParentStart="true"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feb 10, 2015"
android:id="#+id/txtClaimDetailServiceDate"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutProvider">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Provider"
android:id="#+id/txtClaimDetailProviderLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="provider's name"
android:id="#+id/txtClaimDetailProviderName"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayoutProduct">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product or Service"
android:id="#+id/txtProductLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product/service"
android:id="#+id/txtProduct"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutClaimAmount">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Claim amount($)"
android:id="#+id/txtClaimDetailClaimAmountLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:id="#+id/txtClaimDetailClaimAmount"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutOtherAmount">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other amount($)"
android:id="#+id/txtClaimDetailOtherAmountLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:id="#+id/txtClaimDetailOtherAmount"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
Here's the adapter for the ExpandableListView:
public class EclaimsExpandableListAdapter extends BaseExpandableListAdapter {
private ArrayList<ClaimGroup> groups;
public LayoutInflater inflater;
public Activity activity;
float mDensity;
private static final int MAX_ITEMS_MEASURED = 15;
public EclaimsExpandableListAdapter(Activity act, ArrayList<ClaimGroup> groups){
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
mDensity = activity.getResources().getDisplayMetrics().density;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).getChildren().get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
//return 0;
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Log.d("EclaimsExpandableListAdapter :: getChildView() :: ", "Start");
ClaimDetail child = (ClaimDetail)getChild(groupPosition, childPosition);
if(convertView == null){
convertView = inflater.inflate(R.layout.eclaims_claim_detail_health_layout,null);
}
TextView txtServiceDate = (TextView)convertView.findViewById(R.id.txtClaimDetailServiceDate);
txtServiceDate.setText(child.getmServiceDate());
TextView txtProviderName = (TextView)convertView.findViewById(R.id.txtClaimDetailProviderName);
txtProviderName.setText(child.getProvider().getName());
TextView txtClaimAmount = (TextView)convertView.findViewById(R.id.txtClaimDetailClaimAmount);
txtClaimAmount.setText(child.getClaimAmount().toString());
TextView txtOtherAmout = (TextView)convertView.findViewById(R.id.txtClaimDetailOtherAmount);
if(child.getOtherAmount() == null){
txtOtherAmout.setVisibility(View.GONE);
}else{
txtOtherAmout.setText(child.getOtherAmount().toString());
}
return convertView;
}
#Override
public int getChildrenCount(int grpPostion) {
System.out.println("Group position :: " + grpPostion);
return groups.get(grpPostion).getChildren().size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Log.d("EclaimsExpandableListAdapter :: getGroupView() :: ", "Start");
if (convertView == null) {
convertView = inflater.inflate(R.layout.eclaims_claim_group_row, null);
}
/*TextView claimsLabel = (TextView)activity.findViewById(R.id.txtClaimItemsLabel);
if(groups.isEmpty()){
claimsLabel.setVisibility(View.GONE);
}else{
claimsLabel.setVisibility(View.VISIBLE);
}*/
ClaimGroup group = (ClaimGroup) getGroup(groupPosition);
ImageView indicator = (ImageView)convertView.findViewById(R.id.imgIndicator);
int nH = 0;
if(isExpanded){
indicator.setImageResource(R.drawable.icon_expand);
//nH = measureChildrenHeight(groupPosition);
}else{
indicator.setImageResource(R.drawable.icon_collapse);
//nH = convertView.getMeasuredHeight();
}
//parent.getLayoutParams().height = (int) (nH * mDensity);
//parent.invalidate();
TextView providerName = (TextView)convertView.findViewById(R.id.txtProviderName);
providerName.setText(group.getProvider().getName());
ImageView imgDelete = (ImageView)convertView.findViewById(R.id.imgDelete);
imgDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
String confirmation = activity.getString(R.string.eclaims_delete_claim_confirmation);
builder.setMessage(confirmation);
builder.setPositiveButton(R.string.alert_Cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
builder.setNegativeButton(R.string.alert_OK, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
groups.remove(groupPosition);
notifyDataSetChanged();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
ImageView imgEdit = (ImageView)convertView.findViewById(R.id.imgEdit);
imgEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
String confirmation = activity.getString(R.string.eclaims_edit_claim_confirmation);
builder.setMessage(confirmation);
builder.setPositiveButton(R.string.alert_Cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
builder.setNegativeButton(R.string.alert_OK, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// first: remove from the completed claims
Provider selectedProvider = ((ClaimGroup)getGroup(groupPosition)).getProvider();
ClaimGroup parent = groups.remove(groupPosition);
List<Product> prodList = parent.getProductList(); // retrieved the saved product list
ClaimDetail child = parent.getChildren().get(0); // there is only one child
Product selectedProduct = child.getProduct();
notifyDataSetChanged();
// second: populate the fields in claim detail block
TextView serviceDate = (TextView)activity.findViewById(R.id.txtServiceDate);
serviceDate.setText(child.getmServiceDate());
Spinner spinnerProvider = (Spinner)activity.findViewById(R.id.spinnerProviders);
ArrayAdapter<Provider> adapterProvider = (ArrayAdapter< Provider >)spinnerProvider.getAdapter();
int size = adapterProvider.getCount();
int pos_seletion = 0;
for(int i=0; i<size; i++){
Provider prvd = adapterProvider.getItem(i);
if(selectedProvider.equals(prvd)){
pos_seletion = i;
break;
}
}
System.out.println("Selected position for provider list :: " + pos_seletion);
spinnerProvider.setSelection(pos_seletion);
adapterProvider.notifyDataSetChanged();// do we need to notify?
Spinner spinnerProduct = (Spinner)activity.findViewById(R.id.spinnerProducts);
ArrayAdapter<Product> adapterProduct = (ArrayAdapter < Product >)spinnerProduct.getAdapter();
size = prodList.size();
pos_seletion = 0;
for(int j=0; j<size; j++){
Product prod = prodList.get(j);
if(selectedProduct.equals(prod)){
pos_seletion = j;
break;
}
}
System.out.println("Selected position for product list :: " + pos_seletion);
adapterProduct.clear();
adapterProduct.addAll(prodList);
spinnerProduct.setSelection(pos_seletion);
adapterProduct.notifyDataSetChanged(); // do we need to notify?
EditText claimAmt = (EditText)activity.findViewById(R.id.editClaimAmount);
claimAmt.setText(child.getClaimAmount().toString());
EditText otherAmt = (EditText)activity.findViewById(R.id.editOtherAmount);
if(child.getOtherAmount() != null){
otherAmt.setText(child.getOtherAmount().toString());
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
}
Here's the behavior: when clicking the group header, no child view is displayed. From LogCat, I can see that getViewGroup() in the adapter is called twice though, but getChildView() is not called at all. After searching the internet and trying them out, I still can't get the child view displayed when clicking on the group header, but when I tried setting the ExpanandableListView height with a fix height, say, 400dp, like this:
<ExpandableListView xmlns:
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="#+id/expLVClaims"/>
the child view is then expanded and collapsed as expected. But the thing is that because the fixed height will makes the ExpandableListView occupy 400dp space vertically, even no data is in the adapter at all, which is unwanted behavior.
Can someone throw some hints?
Thanks in advance!
Shawn
First thing:
You should not use any scrolling component like expandable list, inside a scroll view here's why Listview inside ScrollView is not scrolling on Android.
Second thing:
Height of ExpandableListView should be match_parent.
I was also facing the same problem related to height, and making ExpandableListView height as match_parent resolved my problem. I think this will help you.
I've a strange problem with my custom spinner. My implementation is to hide the spinner and only fire the choices whenever i click a button so my spinner visibility is set to INVISIBLE and only fire a list of choices and assign the selected value back to a TextView. My code works only in emulator but when i went to my actual device the list of choices is not opened and only the default value is selected back to my textview. I've tried to set spinner visibility to Visible. This time i got the choices. I remember i have another implementation with the visibility is set to INVISIBLE and it worked as desired. Any clues?
XML Layout:
<?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:background="#EEE8E0"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:weightSum="100" >
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#E69100" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="100"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#ffffff"
android:orientation="horizontal" >
<ImageView
android:id="#+id/attach4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/attach" />
<ImageView
android:id="#+id/attach3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/attach" />
<ImageView
android:id="#+id/attach2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/attach" />
<ImageView
android:id="#+id/attach1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/attach" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
<View
android:id="#+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_centerInParent="true"
android:background="#222222"
android:visibility="invisible" />
<EditText
android:id="#+id/addtitle"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="#+id/selecttype"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:gravity="right|center_vertical"
android:hint="عنوان الأعلان"
android:inputType="text"
android:lines="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#444545"
android:textSize="17dp" >
</EditText>
<EditText
android:id="#+id/addbrief"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_below="#+id/addtitle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:gravity="right|center_vertical"
android:hint="المواصفات"
android:inputType="text"
android:lines="4"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#444545"
android:textSize="17dp" />
<EditText
android:id="#+id/addprice"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/addbrief"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:gravity="right|center_vertical"
android:hint="السعر"
android:inputType="number"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#444545"
android:textSize="17dp" />
<Spinner
android:id="#+id/addmoney"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/addbrief"
android:layout_alignTop="#+id/addprice"
android:layout_toLeftOf="#+id/addprice"
android:background="#drawable/edittextback"
android:entries="#array/country_arrays"
android:gravity="center_horizontal|center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:prompt="#string/country_prompt"
android:textColor="#444545"
android:textSize="17dp" />
<EditText
android:id="#+id/addkilometrage"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/addprice"
android:layout_alignRight="#+id/addprice"
android:layout_below="#+id/addprice"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:gravity="right|center_vertical"
android:hint="المسافة المقطوعة"
android:inputType="number"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#444545"
android:textSize="17dp" />
<Spinner
android:id="#+id/addkilo"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/addmoney"
android:layout_alignTop="#+id/addkilometrage"
android:layout_toLeftOf="#+id/addprice"
android:background="#drawable/edittextback"
android:entries="#array/kilo"
android:gravity="center_vertical|center_horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:prompt="#string/country_prompt"
android:textColor="#444545"
android:textSize="17dp" />
<Spinner
android:id="#+id/Spinner01"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/addkilo"
android:layout_alignRight="#+id/addkilometrage"
android:layout_below="#+id/addkilometrage"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:entries="#array/year"
android:gravity="right|center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:prompt="#string/country_prompt"
android:textColor="#444545"
android:textSize="17dp" />
<EditText
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignLeft="#+id/Spinner01"
android:layout_alignRight="#+id/Spinner01"
android:layout_below="#+id/Spinner01"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:ems="10"
android:gravity="right|center_vertical"
android:hint="رقم الهاتف/الجوال"
android:inputType="number"
android:lines="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#444545"
android:textSize="17dp" />
<Button
android:id="#+id/continueregist"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignLeft="#+id/EditText01"
android:layout_alignRight="#+id/addprice"
android:layout_below="#+id/EditText01"
android:layout_marginTop="26dp"
android:background="#drawable/green_buttons"
android:text="Continue"
android:textColor="#ffffff"
android:textSize="19dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:visibility="invisible" />
<Spinner
android:id="#+id/spinner_model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:visibility="invisible" />
<ImageView
android:id="#+id/selecttype"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/addtitle"
android:layout_marginTop="20dp"
android:scaleType="fitXY"
android:src="#drawable/sell" />
<TextView
android:id="#+id/textcattype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/spinner_type"
android:layout_marginRight="21dp"
android:layout_toLeftOf="#+id/selecttype"
android:text=""
android:textSize="19dp" />
<ImageView
android:id="#+id/selectmodel"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignRight="#+id/addmoney"
android:layout_alignTop="#+id/selecttype"
android:scaleType="fitXY"
android:src="#drawable/buy" />
<TextView
android:id="#+id/textcarmodel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/selectmodel"
android:layout_toLeftOf="#+id/selectmodel"
android:text=""
android:textSize="19dp" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/mainaddshekh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#222222"
android:orientation="vertical" >
</RelativeLayout>
Java Class:
public class Sell_Class extends Activity implements OnClickListener {
Spinner Type, Model;
MyAdapter adapter1;
MyAdapter2 adapter2;
EditText cartypeedit;
ImageView cartype, carmodel;
TextView cartypetext, carmodeltext;
String CarType="",CarModel;
int arr_images[] = {
R.drawable.hy,
R.drawable.hy
};
final String[] CarsType = new String[] {
"هيونداي", "هوندا",
};
final String[] Hyundai = new String[] {
"أكسنت", "أكسيل"
};
final String[] Honda = new String[] {
"أكورد", "سيفيك",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainsell);
Type = (Spinner) findViewById(R.id.spinner_type);
Model = (Spinner) findViewById(R.id.spinner_model);
cartype = (ImageView) findViewById(R.id.selecttype);
carmodel = (ImageView) findViewById(R.id.selectmodel);
cartypetext = (TextView) findViewById(R.id.textcattype);
carmodeltext = (TextView) findViewById(R.id.textcarmodel);
cartype.setOnClickListener(this);
carmodel.setOnClickListener(this);
Type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// get spinner value
cartypetext.setText(CarsType[arg2]);
CarType = CarsType[arg2];
CarModel="";
carmodeltext.setText("");
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Model.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (CarType.contentEquals("هيونداي")) {
carmodeltext.setText(Hyundai[arg2]);
CarModel = Hyundai[arg2];
}
if (CarType.contentEquals("هوندا")) {
carmodeltext.setText(Honda[arg2]);
CarModel = Honda[arg2];
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.selecttype:
adapter1 = new MyAdapter(this, CarsType, arr_images);
Type.setAdapter(adapter1);
Type.performClick();
cartypetext.setText("");
break;
case R.id.selectmodel:
if (CarType.contentEquals("")) {
Toast.makeText(getApplicationContext(),
"Please select car first", Toast.LENGTH_LONG).show();
}
else
{
if (CarType.contentEquals("هيونداي")) {
adapter2 = new MyAdapter2(Sell_Class.this, Hyundai);
Model.setAdapter(adapter2);
Model.performClick();
carmodeltext.setText("");
}
if (CarType.contentEquals("هوندا")) {
adapter2 = new MyAdapter2(Sell_Class.this, Honda);
Model.setAdapter(adapter2);
Model.performClick();
carmodeltext.setText("");
}
}
break;
}
}
}
MyAdapter:
public class MyAdapter extends BaseAdapter {
Context c;
String[] title;
int[] pic;
LayoutInflater inflater;
Typeface font;
int arr_images[] = {
R.drawable.hy,
R.drawable.hy
};
public MyAdapter(Context context, String[] title, int[] pic) {
super();
this.c = context;
this.title = title;
this.pic = pic;
inflater = LayoutInflater.from(context);
// font = Typeface.createFromAsset(context.getAssets(),"fonts/khalaadsara.ttf");
}
#Override
public int getCount() {
return title.length;
}
public class ViewHolder {
TextView cat;
ImageView icon;
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView== null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.spinnertext, null);
holder.cat = (TextView) convertView.findViewById(R.id.titlesub);
holder.icon = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.cat.setText(title[position]);
// holder.cat.setTypeface(font);
holder.icon.setImageResource(arr_images[position]);
return convertView;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
}
I am using ListView inside the row of another ListView. In wishlist.xml, I have one ListView. That items were in wishlist_items.xml, In that wishlist_items also having one more listView. that was designed in wishlist_items_advisors.xml. My problem is that send ListView is showing only one item. Can any one tell me how to fix this?
And the adapters also given below.
wishlist.xml
<LinearLayout
android:id="#+id/logo_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:contentDescription="#string/app_name" />
<LinearLayout
android:id="#+id/title_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/titleredbg"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/wishlist_title"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<Button
android:id="#+id/help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:contentDescription="#string/app_name" />
<Button
android:id="#+id/add_person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:contentDescription="#string/app_name"
android:padding="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="100" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="57"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:id="#+id/wishlist_name_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="40"
android:text="#string/wishlist_name"
android:textColor="#color/Black" />
<TextView
android:id="#+id/wishlist_email_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="60"
android:text="#string/wishlist_email"
android:textColor="#color/Black" />
</LinearLayout>
<TextView
android:id="#+id/wishlist_relation_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="43"
android:text="#string/wishlist_relation"
android:textColor="#color/Black" />
</LinearLayout>
<LinearLayout
android:id="#+id/items_footer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:weightSum="100" >
<ListView
android:id="#+id/listView_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="4dp"
android:visibility="visible" >
</ListView>
<LinearLayout
android:id="#+id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/no_data"
android:textColor="#000" >
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
wishlist_items.xml
<TextView
android:id="#+id/hr1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:background="#D2D2D2" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:weightSum="100" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="57"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:id="#+id/wishlist_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="40"
android:ellipsize="end"
android:maxLines="2"
android:text="John John John John John John"
android:textColor="#color/Black" />
<TextView
android:id="#+id/wishlist_email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="60"
android:ellipsize="end"
android:maxLines="2"
android:text="krishna.mondeddu#gmail.com krishna.mondeddu#gmail.com"
android:textColor="#color/Black" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="43"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:id="#+id/wishlist_relation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="60"
android:ellipsize="end"
android:maxLines="2"
android:text="Birthday Birthday vv Birthday Birthday"
android:textColor="#color/Black" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="40" >
<ImageButton
android:id="#+id/editButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/deleteButton"
android:background="#drawable/wishlistediticon"
android:contentDescription="#string/app_name" />
<ImageButton
android:id="#+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:contentDescription="#string/app_name" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/hr4"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#D2D2D2" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/gift_advisor_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/wishlist_getadvisor"
android:textColor="#color/Black" />
</LinearLayout>
<TextView
android:id="#+id/hr1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#D2D2D2" />
<ListView android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
></ListView>
<TextView
android:id="#+id/hr5"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#D2D2D2" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#fff"
android:paddingTop="5dp"
android:paddingBottom="5dp"
>
<ImageButton
android:id="#+id/invite_advisor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:contentDescription="#string/app_name" />
</RelativeLayout>
</LinearLayout>
wishlist_items_advisors.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:weightSum="100" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="57"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:id="#+id/advisor_name_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="40"
android:ellipsize="end"
android:maxLines="2"
android:text="John John"
android:textColor="#color/Black" />
<TextView
android:id="#+id/advisor_email_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="60"
android:ellipsize="end"
android:maxLines="2"
android:text="krishna."
android:textColor="#color/Black" />
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="43" >
<TextView
android:id="#+id/status_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/deleteButton"
android:text="Accept" />
<ImageButton
android:id="#+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:contentDescription="#string/app_name" />
</RelativeLayout>
</LinearLayout>
CustomAdapter
public class CustomAdapter extends BaseAdapter{
private String guestIds[]=null;
private String names[]=null;
private String emails[] = null;
private String relationships[] = null;
private String occasions[] = null;
DisplayImageOptions doption=null;
private ImageLoadingListener animateFirstListener =null;
private Context context=null;
public CustomAdapter(Activity activity,String[] guestId,String[] name,String[] email,String[] relationship, String[] occasion)
{
this.context=activity;
this.guestIds = guestId;
this.names =name;
this.emails = email;
this.relationships = relationship;
this.occasions = occasion;
doption=new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).showStubImage(R.drawable.ic_stub).cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(5)).build();
animateFirstListener = new AnimateFirstDisplayListener();
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
//CustomAdapter item = (CustomAdapter) getItem(position);
if (isItemAnAd(position)) {
return 0;
} else {
return 1;
}
}
private boolean isItemAnAd(int position) {
// Place an ad at the first
return (position == 0);
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = ((Activity) context).getLayoutInflater().inflate(R.layout.wishlist_items, parent, false);
holder = new ViewHolder();
holder.wishlistName = (TextView) view.findViewById(R.id.wishlist_name);
holder.wishlistEmail = (TextView) view.findViewById(R.id.wishlist_email);
holder.wishlistRelation = (TextView) view.findViewById(R.id.wishlist_relation);
holder.wishGiftAdvisorText = (TextView) view.findViewById(R.id.gift_advisor_text);
holder.advisorListview = (ListView) view.findViewById(R.id.listView);
holder.inviteAdvisor = (ImageButton) view.findViewById(R.id.invite_advisor);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.wishlistName.setText(names[position]);
holder.wishlistEmail.setText(emails[position]);
holder.wishlistRelation.setText(relationships[position]);
holder.wishGiftAdvisorText.setText(getResources().getString( R.string.wishlist_getadvisor)+" "+names[position]+"'s "+getResources().getString( R.string.wishlist_title) );
GuestId = guestIds[position];
holder.wishlistName.setTypeface(tf);
holder.wishlistEmail.setTypeface(tf);
holder.wishlistRelation.setTypeface(tf);
holder.wishGiftAdvisorText.setTypeface(tf);
if(occasions[position].contains("[")) {
try {
array = new JSONArray(occasions[position]);
System.out.println(array.toString(2));
//loadOccasionData(array);
// TODO Auto-generated method stub
if(array!= null) {
advisorIds = new String[array.length()];
advisorNames = new String[array.length()];
advisorEmails = new String[array.length()];
advisorRelationships = new String[array.length()];
advisorStatuses = new String[array.length()];
for (int i = 0; i < array.length(); i++) {
JSONObject c;
try {
c = array.getJSONObject(i);
// Storing each json item in variable
advisorIds[i] = c.getString("advisor_id");
advisorNames[i] = c.getString("name");
advisorEmails[i] = c.getString("email");
advisorRelationships[i] = c.getString("relationship");
advisorStatuses[i] = c.getString("status");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
CustomAdvisorAdapter adapter = new CustomAdvisorAdapter(WishList.this,
advisorIds, advisorNames, advisorEmails, advisorRelationships , advisorStatuses);
holder.advisorListview.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
holder.advisorListview.setAdapter(null);
}
return view;
}
private class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
private class ViewHolder {
public TextView wishlistName;
public TextView wishlistEmail;
public TextView wishlistRelation;
public TextView wishGiftAdvisorText;
public ListView advisorListview;
public ImageButton inviteAdvisor;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
}
CustomAdvisorAdapter
public class CustomAdvisorAdapter extends BaseAdapter{
private String advisorIds[]=null;
private String advisorNames[]=null;
private String advisorEmails[] = null;
private String advisorRelationships[] = null;
private String advisorStatuses[] = null;
DisplayImageOptions doption=null;
private ImageLoadingListener animateFirstListener =null;
private Context context=null;
public CustomAdvisorAdapter(Activity activity,String[] advisorId,String[] advisorName,String[] advisorEmail,String[] advisorRelationship, String[] advisorStatus)
{
this.context=activity;
this.advisorIds = advisorId;
this.advisorNames =advisorName;
this.advisorEmails = advisorEmail;
this.advisorRelationships = advisorRelationship;
this.advisorStatuses = advisorStatus;
doption=new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).showStubImage(R.drawable.ic_stub).cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(5)).build();
animateFirstListener = new AnimateFirstDisplayListener();
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View viewAdvisor = convertView;
final ViewHolder advisorHolder;
if (convertView == null) {
viewAdvisor = ((Activity) context).getLayoutInflater().inflate(R.layout.wishlist_items_advisor, parent, false);
advisorHolder = new ViewHolder();
advisorHolder.advisorNameText = (TextView) viewAdvisor.findViewById(R.id.advisor_name_text);
advisorHolder.advisorEmailText = (TextView) viewAdvisor.findViewById(R.id.advisor_email_text);
advisorHolder.statusText = (TextView) viewAdvisor.findViewById(R.id.status_text);
viewAdvisor.setTag(advisorHolder);
} else {
advisorHolder = (ViewHolder) viewAdvisor.getTag();
}
advisorHolder.advisorNameText.setText(advisorNames[position]);
advisorHolder.advisorEmailText.setText(advisorEmails[position]);
advisorHolder.statusText.setText(advisorStatuses[position]);
advisorHolder.advisorNameText.setTypeface(tf);
advisorHolder.advisorEmailText.setTypeface(tf);
advisorHolder.statusText.setTypeface(tf);
return viewAdvisor;
}
private class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
private class ViewHolder {
public TextView advisorNameText;
public TextView advisorEmailText;
public TextView statusText;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
}
It's not possible to make a scrollable view inside a scrollable view. But as a work around this, and only in case that this listviews doesn't take much memory if all views are loaded.
you can use this
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class NonScrollableListView extends ListView {
public NonScrollableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Do not use the highest two bits of Integer.MAX_VALUE because they are
// reserved for the MeasureSpec mode
int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightSpec);
getLayoutParams().height = getMeasuredHeight();
}
}
Again, it's not good to use this workaround
you will use this non Scrollable listview in the child.xml layout by adding it as a customized UI component
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.youpackage.uiutils.NonScrollableListView
android:id="#+id/non_scrollable_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
You can ExpandableListView in place of making custom view also for data handling you can use ExpandableListAdapter.
ListView in Graphical Layout is fit on appropriate height but when i imported into android device, ListView is reduced in a small field. i want ListView are the same in Graphical Layout in xml file and in real device screen
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10sp"
android:id="#+id/header"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/id"
android:layout_weight="1.25"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/amount"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/alert"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/detail"
android:layout_weight="1"
android:gravity="center"/>
</LinearLayout>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10sp"></ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.5">
<Button
android:id="#+id/addQueue"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:text="#string/addQueue"/>
<Button
android:id="#+id/submit"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:text="#string/submit"/>
<Button
android:id="#+id/reset"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/addQueue"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="#string/resetQueue" />
</RelativeLayout>
I have tried to modify your layout. Try this may be it will be helpful for you.. just create a new xml layout file and check it is working for you or not.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/addQueue"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/submit"
android:text="addQueue" />
<Button
android:id="#+id/submit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="submit" />
<Button
android:id="#+id/reset"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="resetQueue" />
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.25"
android:gravity="center"
android:text="text1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text4" />
</LinearLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/addQueue"
android:layout_alignParentLeft="true"
android:layout_below="#+id/header" >
</ListView>
</RelativeLayout>
My custom adapter class is here. :
public class my_custom_adapter extends ArrayAdapter<String> {
private Context context = null;
String[] elements = null;
private ArrayList<Boolean> itemChecked = null;
public my_custom_adapter(Context context, int type, String[] elements2)
{
super(context, type, elements2);
this.elements = elements2;
this.context = context;
itemChecked = new ArrayList<Boolean>();
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("master_check_change")) {
boolean check_value = intent.getBooleanExtra("check_value", false);
set_checked(check_value);
notifyDataSetChanged();
}
}
};
context.registerReceiver(receiver, new IntentFilter("master_check_change"));
set_checked(false);
}
// AS EVERY TIME LISTVIEW INFLATE YOUR VIEWS WHEN YOU MOVE THEM SO YOU NEED TO SAVE ALL OF YOUR CHECKBOX STATES IN SOME ARRAYLIST OTHERWISE IT WILL SET ANY DEFAULT VALUE.
private void set_checked(boolean is_checked)
{
for (int i=0; i < elements.length; i++) {
itemChecked.add(i, is_checked);
}
}
//THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder
{
public TextView tv;
public CheckBox cb;
public ImageView iv;
}
#Override
public View getView (final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.inflated_layout, null, false);
holder = new ViewHolder();
holder.cb = (CheckBox) rowView.findViewById(R.id.checkBox1);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.iv = (ImageView) rowView.findViewById(R.id.imageView1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
if (holder != null) {
holder.tv.setText(elements[position]);
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
itemChecked.set(position, isChecked);
}
});
if(position < itemChecked.size()) {
holder.cb.setChecked(itemChecked.get(position));
}
}
return rowView;
}
}
My activity class is here where i am create custom adapter object,
public class Test_stflowActivity extends Activity {
public CheckBox master_cb = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp_layout);
String[] elements = new String[10];
for (int i = 0; i < 10; i++) {
elements[i] = "elements " + i;
}
CheckBox master_cb = new CheckBox(getApplicationContext());
master_cb.setText("Check All");
//HERE IS THE LIST VIEW WHICH I HAVE CREATED IN MY XML FILE.
ListView lv = (ListView) findViewById(R.id.listView1);
//HERE I AM CREATING CUSTOM ADAPTER OBJECT.
my_custom_adapter adapter = new my_custom_adapter(this, android.R.layout.simple_list_item_1, elements);
lv.addHeaderView(master_cb);
lv.setAdapter(adapter);
master_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Intent my_intent = new Intent("master_check_change");
my_intent.putExtra("check_value", isChecked);
sendBroadcast(my_intent);
}
});
}
}