Android : Unable to delete ListView item from menu - android

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>

Related

grid view items not scrolling down to up (repeating the items) and when i am loading data from server in android?

I have a grid view and data updated in grid view using api calls and grid item loads a image from url using picasso library all are perfect but when i am scrolling down to up the grid items not scrolled and repeating the items(up to down scrolling was perfect).
Adapterclass.java
private class ProductAdapter extends BaseAdapter {
ViewHolder holder = null;
#Override
public int getCount() {
return mProducts != null ? mProducts.size() : 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
holder = new ViewHolder();
if (convertView == null) {
// convertView = LayoutInflater.from(Products_activity.this).inflate(R.layout.product_layout, null, false);
LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.product_layout, null);
holder.product_name = (TextView) convertView.findViewById(R.id.product_name);
holder.product_descr = (TextView) convertView.findViewById(R.id.product_desc);
holder.product_price = (TextView) convertView.findViewById(R.id.product_price);
holder.product_pic = (ImageView) convertView.findViewById(R.id.iv_division_img);
holder.product_share = (ImageView) convertView.findViewById(R.id.productshare_button);
holder.like_image=(ImageView) convertView.findViewById(R.id.like_image);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
if (mProducts != null) {
String url = "http://staggershop.pivalue.net/assets/product_images/"+mProducts.get(position).product_image;
String urls = url.replaceAll(" ", "%20");
Picasso.with(getApplicationContext()).load(urls).resize(300,300).noFade().error(R.drawable.home_brands).into(holder.product_pic);
holder.product_name.setText(mProducts.get(position).product_name);
//product_descr.setText(mProducts.get(position).pr);
// product_price.setText(mProducts.get(position).product_price);
/* Picasso
.with(getApplicationContext())
.setLoggingEnabled(true);
Picasso.with(getApplicationContext())
.load(urls)
.error(R.drawable.home_brands).memoryPolicy(MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_STORE)
.into(product_pic);*/
holder.product_descr.setText(mProducts.get(position).product_description);
if(!recently_beans.isEmpty()){
for(int i=0;i<recently_beans.size();i++) {
if (mProducts.get(position).prdid.equals(recently_beans.get(i).prod_id))
{
holder.like_image.setImageResource(R.drawable.like);
}
}
notifyDataSetChanged();
}
}
holder.product_share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}
});
holder.like_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
if (!recently_beans.isEmpty()) {
for (int i = 0; i < recently_beans.size(); i++) {
if (mProducts.get(position).prdid.equals(recently_beans.get(i).prod_id)) {
holder.like_image.setImageResource(R.drawable.dislike);
Inserfavorite(mProducts.get(position).prdid);
}
}
}
}
});
}
});
// notifyDataSetChanged();
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Inserrecent(mProducts.get(position).prdid);
Intent intent = new Intent(Products_activity.this, Product_description.class);
startActivity(intent);
}
});
return convertView;
}
}
public static class ViewHolder {
public TextView product_name;
public TextView product_descr;
public TextView product_price;
public ImageView product_pic ;
public ImageView product_share;
public ImageView like_image;
}
This my product.xml
<RelativeLayout
android:id="#+id/rl_divisioin_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/product_img">
<ImageView
android:id="#+id/iv_division_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/view"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:src="#drawable/home_brands" />
</LinearLayout>
<LinearLayout
android:id="#+id/prod_name_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/product_img"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="4dp"
android:fontFamily="#string/font_family_regular"
android:gravity="center"
android:padding="5dp"
android:maxLines="1"
android:text="product name"
android:textColor="#color/black"
android:textSize="10sp" />
<TextView
android:id="#+id/product_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="4dp"
android:fontFamily="#string/font_family_regular"
android:gravity="center"
android:maxLines="1"
android:padding="5dp"
android:text="product Description"
android:textColor="#color/black"
android:textSize="10sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/product_desc"
android:layout_gravity="center"
android:layout_marginTop="20dp">
<TextView
android:id="#+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:elevation="4dp"
android:text="100$"
android:textColor="#color/black"
android:textSize="12sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/productshare_button"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:elevation="4dp"
android:layout_marginRight="5dp"
android:src="#mipmap/symbol_dotss"
android:text="product name"
android:textColor="#color/black"
android:textSize="10sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="5dp"
android:id="#+id/like_image"
android:layout_alignParentEnd="true"
android:src="#drawable/dislike" />
</RelativeLayout>
activty_product.xml
<android.support.design.widget.CoordinatorLayout 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:id="#+id/coordinator"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rltool"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar1"
android:background="#color/textColorPrimary"
app:titleTextColor="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/AppTheme.Toolbar"
app:layout_scrollFlags="scroll|enterAlways">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/search1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/background_shape_buttons">
<!-- <EditText
android:id="#+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#D6D5C0"
android:hint="Search..................."
android:layout_gravity="center"
android:backgroundTint="#00000000" />-->
<SearchView
android:id="#+id/search"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textColorHint="#D6D5C0"
android:text="#string/search"
android:layout_gravity="center"
android:iconifiedByDefault="false">
<requestFocus />
</SearchView>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<View
android:id="#+id/shadow_view"
android:layout_width="match_parent"
android:layout_alignBottom="#+id/toolbar1"
android:visibility="visible"
android:layout_height="3dp"
android:background="#drawable/toolbar_shadow" />
</RelativeLayout>
<GridView
android:id="#+id/product_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scrollbars="vertical"
android:layout_margin="15dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="10dp">
</GridView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
To avoid the unexpected behaviour , provide the implementations of
#Override
public Object getItem(int position) {
return mProducts.get(position);
}
#Override
public long getItemId(int position) {
//return position; or better to provide some unique id
// as there is one in your code, product id
return mProducts.get(position).prod_id;
}
Note : remove notifyDataSetChanged(); in getView because it will trigger the adapter to refresh all view while they are under creating which can cause this behaviour.
Recommended Read
What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?

How to expand and collapse view when click on listview adapter in android

I am developing android app in which i want to show user some coupon and for that i used custom ListView and set custom layout to it.In custom layout I want to expand and collapse the relative layout when click on textview,below is my code for this:
Coupon_Layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/coupon_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:background="#drawable/dash_rectangular_background"
android:padding="15dp"
android:text="FIRST10"
android:textColor="#color/input_login"
android:textSize="16.5sp" />
<TextView
android:id="#+id/coupon_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/coupon_code"
android:layout_alignBottom="#+id/coupon_code"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/coupon_code"
android:maxLines="2"
android:text="Get 10₹/- off on your 1st ride*."
android:textColor="#color/input_login"
android:textSize="16sp" />
<TextView
android:id="#+id/coupon_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/coupon_code"
android:layout_below="#+id/coupon_percentage"
android:layout_marginTop="24dp"
android:ellipsize="end"
android:maxLines="2"
android:text="*Only for new user."
android:textColor="#color/input_login"
android:textSize="16sp" />
<TextView
android:id="#+id/tv_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/coupon_details"
android:layout_marginStart="5dp"
android:layout_toEndOf="#+id/coupon_details"
android:background="#null"
android:text="Details"
android:textSize="16sp"
android:textColor="#color/bg_main"
android:textAllCaps="false"/>
<RelativeLayout
android:id="#+id/details_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/coupon_details"
android:layout_marginTop="10dp">
<ImageView
android:id="#+id/imageView2"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:background="#0000"
android:src="#drawable/ic_dot_bullet" />
<TextView
android:id="#+id/tv_limitation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="19dp"
android:layout_toEndOf="#+id/imageView2"
android:text="You can avail this offer on your 1st ride"
android:textSize="15sp" />
</RelativeLayout>
<View
android:id="#+id/v_seprator_coupon_2"
style="#style/Divider_History"
android:layout_below="#+id/details_layout"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp" />
</RelativeLayout>
Coupon_Adapter.java
public class Coupon_Adapter extends BaseAdapter{
private Activity activity;
private LayoutInflater inflater;
private List<Coupon_List> couponList;
private TextView coupon_description,coupon_percentage,coupon_code,details,limitations;
private Coupon_List coupon_list;
private RelativeLayout details_layout;
private boolean isexpand = false;
public Coupon_Adapter(Activity activity, List<Coupon_List> couponList) {
this.activity = activity;
this.couponList = couponList;
}
#Override
public int getCount() {
return couponList.size();
}
#Override
public Object getItem(int i) {
return couponList.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(inflater == null){
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(view == null){
view = inflater.inflate(R.layout.coupon_layout,null);
}
coupon_code = view.findViewById(R.id.coupon_code);
coupon_description =view.findViewById(R.id.coupon_details);
coupon_percentage = view.findViewById(R.id.coupon_percentage);
limitations = view.findViewById(R.id.tv_limitation);
details = view.findViewById(R.id.tv_details);
details_layout = view.findViewById(R.id.details_layout);
collapse(details_layout);
details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!isexpand) {
Log.d("texview onclick listner","true");
expand(details_layout);
}else {
collapse(details_layout);
}
}
});
coupon_list = couponList.get(i);
coupon_code.setText(coupon_list.getCoupon_code());
Log.d("coupon list",coupon_list.getCoupon_code());
coupon_percentage.setText(coupon_list.getCoupon_percentage());
// Log.d("coupon list",coupon_list.getCoupon_description());
coupon_description.setText(coupon_list.getCoupon_detail_info());
Log.d("coupon list",coupon_list.getCoupon_limitation());
limitations.setText(coupon_list.getCoupon_detail_info());
return view;
}
public void expand(final View v) {
v.measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
Animation a = new Animation()
{
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? RelativeLayout.LayoutParams.WRAP_CONTENT
: (int)(targetHeight * interpolatedTime);
v.requestLayout();
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
isexpand = true;
}
public void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation()
{
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
isexpand = false;
}
}
I use the method to expand and collapse the view and it's working in other activity and i debug the code and when i click on textview its going in setOnClickListener but not expanding or collapsing my view.Please help me for this issue.
There is too much to change. Add your drawable and color to your text, as I removed them while testing, here I have modified your code and xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:animationCache="true"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/coupon_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:padding="15dp"
android:text="FIRST10"
android:textSize="16.5sp" />
<TextView
android:id="#+id/coupon_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/coupon_code"
android:layout_alignBottom="#+id/coupon_code"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/coupon_code"
android:maxLines="2"
android:text="Get 10₹/- off on your 1st ride*."
android:textSize="16sp" />
<TextView
android:id="#+id/coupon_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/coupon_code"
android:layout_below="#+id/coupon_percentage"
android:layout_marginTop="24dp"
android:ellipsize="end"
android:maxLines="2"
android:text="*Only for new user."
android:textSize="16sp" />
<TextView
android:id="#+id/tv_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/coupon_details"
android:layout_marginStart="5dp"
android:layout_toEndOf="#+id/coupon_details"
android:background="#null"
android:text="Details"
android:textAllCaps="false"
android:textSize="16sp" />
<RelativeLayout
android:id="#+id/details_layout"
android:layout_width="wrap_content"
android:background="#color/colorAccent"
android:layout_height="wrap_content"
android:layout_below="#+id/coupon_details"
android:layout_marginTop="10dp">
<ImageView
android:id="#+id/imageView2"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:background="#0000" />
<TextView
android:id="#+id/tv_limitation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="19dp"
android:layout_toEndOf="#+id/imageView2"
android:text="You can avail this offer on your 1st ride"
android:textSize="15sp" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
And make change in your adapter class which is Coupon_Adapter
//add this line in the constructor of adapter class.
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
change your onView and add your code, I have also provided a easier way to show hide views.
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
final Holder listViewHolder;
if (view == null) {
listViewHolder = new Holder();
view = inflater.inflate(R.layout.coupon_layout,viewGroup,false);
listViewHolder.coupon_code = view.findViewById(R.id.coupon_code);
listViewHolder.coupon_description =view.findViewById(R.id.coupon_details);
listViewHolder.coupon_percentage = view.findViewById(R.id.coupon_percentage);
listViewHolder.limitations = view.findViewById(R.id.tv_limitation);
listViewHolder.details = view.findViewById(R.id.tv_details);
listViewHolder.details_layout = view.findViewById(R.id.details_layout);
view.setTag(listViewHolder);
} else {
listViewHolder = (Holder) view.getTag();
}
collapse(listViewHolder.details_layout);
listViewHolder.coupon_code.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!isexpand) {
Log.d("texview onclick listner","true");
expand(listViewHolder.details_layout);
}else {
collapse(listViewHolder.details_layout);
}
/*if(listViewHolder.details_layout.getVisibility() == View.VISIBLE)
listViewHolder.details_layout.setVisibility(View.GONE);
else listViewHolder.details_layout.setVisibility(View.VISIBLE);*/
}
});
listViewHolder.coupon_code.setText(couponList.get(i));
return view;
}
Last make a holder class inside adapter Class
public class Holder{
TextView coupon_description,coupon_percentage,coupon_code,details,limitations;
RelativeLayout details_layout;
}
Read some concepts of listview and adapter. How adapter works.

Button doesn't get focus in Recyclerview item

am trying to click button in Recyclerview item but it not receive onclick event.but it worked for double tap to respond onclick listener
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="5dp"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_marginRight="5dp"
android:focusableInTouchMode="false"
android:scaleType="fitXY"
android:layout_marginLeft="5dp"
android:id="#+id/calllog_contact_img"
android:src="#mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calllog_contact_name"
android:layout_marginBottom="2dp"
android:text="Pounkumar Purushothaman"
android:textSize="18dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="20dp"
android:focusableInTouchMode="false"
android:src="#android:drawable/sym_call_incoming"
android:layout_marginBottom="10dp"
android:id="#+id/calllog_call_type"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calllog_no"
android:text="+919043974134"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:20"
android:layout_marginRight="20dp"
android:id="#+id/calllog_call_duration"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:20"
android:id="#+id/calllog_call_timing"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/llExpandArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:id="#+id/call_txt"
android:text="Call" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:paddingLeft="15dp"
android:id="#+id/block_txt"
android:text="Msg" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
am trying to click button in Recyclerview item but it not receive onclick event.but it worked for double tap to respond onclick listener
Adapter:
public class calllog_customadapter extends RecyclerView.Adapter<calllog_customadapter.RecordHolder> {
private List<calllog_item> calllog_items;
Context c;
RecyclerView list;
private int expandedPosition = -1;
#Override
public RecordHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.calllog_item, parent, false);
return new RecordHolder(itemView);
}
calllog_customadapter(Context context, RecyclerView list, final List<calllog_item> calllog_item){
this.c=context;
this.list=list;
this.calllog_items=calllog_item;
list.addOnItemTouchListener(new RecyclerItemClickListener(c, list ,new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Button call= (Button) view.findViewById(R.id.call_txt);
Button block= (Button) view.findViewById(R.id.block_txt);
call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(c, "call", Toast.LENGTH_SHORT).show();
}
});
block.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(c, "blockbbb222", Toast.LENGTH_SHORT).show();
}
});
if (expandedPosition >= 0) {
int prev = expandedPosition;
notifyItemChanged(prev);
}
expandedPosition = position;
notifyItemChanged(expandedPosition);
}
#Override
public void onLongItemClick(View view, int position) {
// do whatever
}
}));
}
#Override
public void onBindViewHolder(RecordHolder holder, int position) {
calllog_item list=calllog_items.get(position);
holder.contact_name.setText(list.getContact_name());
holder.contact_img.setImageBitmap(list.getContact_img());
holder.call_type.setImageBitmap(list.getCall_type());
holder.call_duration.setText(list.getCall_duration());
holder.call_timing.setText(list.call_timing);
holder.contact_no.setText(list.getCall_no());
if (position == expandedPosition) {
holder.llExpandArea.setVisibility(View.VISIBLE);
} else {
holder.llExpandArea.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return calllog_items.size();
}
public class RecordHolder extends RecyclerView.ViewHolder {
TextView contact_name,call_timing,call_duration,contact_no;
ImageView contact_img,call_type;
LinearLayout llExpandArea;
public RecordHolder(View itemView) {
super(itemView);
contact_img= (ImageView) itemView.findViewById(R.id.calllog_contact_img);
call_type= (ImageView) itemView.findViewById(R.id.calllog_call_type);
call_duration= (TextView) itemView.findViewById(R.id.calllog_call_duration);
call_timing= (TextView) itemView.findViewById(R.id.calllog_call_timing);
contact_name= (TextView) itemView.findViewById(R.id.calllog_contact_name);
contact_no= (TextView) itemView.findViewById(R.id.calllog_no);
llExpandArea=(LinearLayout)itemView.findViewById(R.id.llExpandArea);
}
}
}
Did you try to set it as clicable and focusable?
XML
android:focusable="true"
android:clickable="true"
Java
button.setClickable("true");
button.setFocusable("true");
If nothing changes, try to put a log call in your onClick method (maybe there's something bad in the code), or in OnTouch.

The height of ExpandableListView impacts the display of ChildView

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&apos;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&apos;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.

ListView row containing another ListView

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.

Categories

Resources