1

Can any one tell me the approach to set number of buttons dynamically in Grid-View items.I mean,Suppose i am having the grid-view(consist of categories of different products) of 3X3 and when i click on its first item i get the list of buttons of its sub-product inside grid-view first time and number differs for every item based on the availability of sub-products.

The list of buttons shown inside grid-view item may differ for every item click.I am new to android so any help will be appreciated

Here is my xml of adapter :-

<?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:gravity="center"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <com.mss.skyjack.custom.views.SkyjackCustomTextview
                android:id="@+id/tv_product"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawablePadding="5dp"
                android:gravity="center"
                android:text="@string/product"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/black_color" />

            <ImageView
                android:id="@+id/img_product"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="60dp"
                android:src="@drawable/ic_launcher" />



                <LinearLayout
                    android:id="@+id/lnr_test"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="30dp"
                    android:orientation="vertical"
                    android:visibility="gone">
                </LinearLayout>

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#000000" />
    </LinearLayout>

    <View
        android:id="@+id/view_right_line"
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:background="#000000" />

</LinearLayout>

Here is my adapter class :-

public class ProductSelectorAdapter extends BaseAdapter {

    Activity activity;
    List<SelectorTest> listSelector;
    private Button btn;
    List<catagoriesListModal> _categories;

    public ProductSelectorAdapter(Activity activity,
            List<SelectorTest> listSelector) {
        this.activity = activity;
        this.listSelector = listSelector;
    }

    @Override
    public int getCount() {
        return listSelector.size();
    }

    @Override
    public SelectorTest getItem(int position) {
        return listSelector.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        _categories = listSelector.get(position).getCategories();
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.selector_items, parent,
                    false);
            viewHolder = new ViewHolder();
            viewHolder.tvProductName = (TextView) convertView
                    .findViewById(R.id.tv_product);
            viewHolder.imgView = (ImageView) convertView
                    .findViewById(R.id.img_product);
            viewHolder.viewRightLine = (View) convertView
                    .findViewById(R.id.view_right_line);
            viewHolder.lnrLayout = (LinearLayout) convertView
                    .findViewById(R.id.lnr_test);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (!activity.getResources().getBoolean(R.bool.landscape_only)) {
            if (position % 2 != 0) {
                viewHolder.viewRightLine.setVisibility(View.GONE);
            } else {
                viewHolder.viewRightLine.setVisibility(View.VISIBLE);
            }
        } else {
            if (position % 3 != 0 & position % 3 != 1) {
                viewHolder.viewRightLine.setVisibility(View.GONE);
            } else {
                viewHolder.viewRightLine.setVisibility(View.VISIBLE);
            }
        }

        SelectorTest selectorTest = listSelector.get(position);
        viewHolder.tvProductName.setText(selectorTest.getName());
        viewHolder.imgView.setImageResource(selectorTest.getImage());
        viewHolder.imgView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (activity.getResources().getBoolean(R.bool.landscape_only)) {
                    viewHolder.lnrLayout.setVisibility(View.VISIBLE);
                    viewHolder.imgView.setVisibility(View.GONE);

                    for (int i = 0; i < _categories.size(); i++) {

                        btn = new Button(activity);
                        btn.setText(_categories.get(i).getName());

                        viewHolder.lnrLayout.addView(btn);
                        int idx = viewHolder.lnrLayout.indexOfChild(btn);
                        btn.setTag(Integer.toString(idx));

                        btn.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                String idxStr = (String) v.getTag();
                                Toast.makeText(activity, idxStr,
                                        Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                } else {
                    Session.ChangeFragmentWithBack(new ProductDetailFragment(),
                            "ProductDetailFragment", "ProductDetailFragment", 0);
                }

            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView tvProductName, tvTest;
        ImageView imgView;
        View viewRightLine;
        ListView lstroduct;
        ScrollView scrlLstItems;
        LinearLayout lnrLayout;
        Button btn1, btn2, btn3;
    }

Here is my SelectorTest model class :-

import java.util.List;

public class SelectorTest {
    int id;
    String name;
    int image;
    List<catagoriesListModal> categories;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public List<catagoriesListModal> getCategories() {
        return categories;
    }

    public void setCategories(List<catagoriesListModal> categories) {
        this.categories = categories;
    }

}

Here is my catagoriesListModal class :-

public class catagoriesListModal {
    String name;
    int id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

}

Thanks

4
  • you must be using using an adapter for you grid view right? can you post the code for that. Commented Aug 27, 2015 at 10:31
  • Hey hars , thanks for your reply please check my updated post Commented Aug 27, 2015 at 10:50
  • What is it that you are getting after running the code that you pasted? Any error? Crash? or something else? Commented Aug 27, 2015 at 11:02
  • No i am not getting any crash but my view is getting overlapped and i am not sure that how can i shown different number of buttons in each grid-Item. Commented Aug 27, 2015 at 11:30

1 Answer 1

1

Your approach is correct but your model doesn't take into account the categories in itself. Modify your Model to Below.

import java.util.List;


public class SelectorTest {
int id;
String name;
int image;
List<String> categories;


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}



public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}

public List<String> getCategories() {
    return categories;
}

public void setCategories(List<String> categories) {
    this.categories = categories;
}
}

Now modify your adapter Class accordingly

public class ProductSelectorAdapter extends BaseAdapter {

Activity activity;
List<SelectorTest> listSelector;
private Button btn;

public ProductSelectorAdapter(Activity activity,
                              List<SelectorTest> listSelector) {
    this.activity = activity;
    this.listSelector = listSelector;

}

@Override
public int getCount() {
    return listSelector.size();
}

@Override
public SelectorTest getItem(int position) {
    return listSelector.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.selector_items, parent,
                false);
        viewHolder = new ViewHolder();
        viewHolder.tvProductName = (TextView) convertView
                .findViewById(R.id.tv_product);
        viewHolder.imgView = (ImageView) convertView
                .findViewById(R.id.img_product);
        viewHolder.viewRightLine = (View) convertView
                .findViewById(R.id.view_right_line);
        viewHolder.lnrLayout = (LinearLayout) convertView
                .findViewById(R.id.lnr_test);

        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    if (!activity.getResources().getBoolean(R.bool.landscape_only)) {
        if (position % 2 != 0) {
            viewHolder.viewRightLine.setVisibility(View.GONE);
        } else {
            viewHolder.viewRightLine.setVisibility(View.VISIBLE);
        }
    } else {
        if (position % 3 != 0 & position % 3 != 1) {
            viewHolder.viewRightLine.setVisibility(View.GONE);
        } else {
            viewHolder.viewRightLine.setVisibility(View.VISIBLE);
        }
    }

    SelectorTest selectorTest = listSelector.get(position);
    viewHolder.tvProductName.setText(selectorTest.getName());
    viewHolder.imgView.setImageResource(selectorTest.getImage());


    convertView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (activity.getResources().getBoolean(R.bool.landscape_only)) {

                viewHolder.lnrLayout.setVisibility(View.VISIBLE);
                viewHolder.imgView.setVisibility(View.GONE);
                List<String> _categories = listSelector.get(position).getCategories();
                for (int i = 0; i < _categories.size(); i++) {

                    btn = new Button(activity);
                    btn.setText(_categories.get(i));
                    viewHolder.lnrLayout.addView(btn);
                    int idx = viewHolder.lnrLayout.indexOfChild(btn);
                    btn.setTag(Integer.toString(idx));
                }
                btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String idxStr = (String) v.getTag();
                        Toast.makeText(activity, idxStr, Toast.LENGTH_SHORT).show();

                    }
                });

            }
        }
    });

    return convertView;
}

static class ViewHolder {
    TextView tvProductName, tvTest;
    ImageView imgView;
    View viewRightLine;
    ListView lstroduct;
    ScrollView scrlLstItems;
    LinearLayout lnrLayout;

}
}

You must see that i have changed the variable position to final.(Just for a faster response) if you do not want to that. you can create a custom View.OnclickListener which takes position as a parameter. something like this

class MyOwnClickListener implements OnClickListener {
    int position;

    public MyOwnClickListener(int position) {
        this.position = position;
    }

    @Override
    public void onClick(View v) {
....

and for your convertView you can then use

convertView.setOnClickListener(new MyOwnClickListener(position) {

If any doubts do reach out. Thanks

Update

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:fillViewport="true" > 
    <LinearLayout
                android:id="@+id/lnr_test"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginTop="30dp"
                android:orientation="vertical"                        android:visibility="gone">
            </LinearLayout>
   </ScrollView>
Sign up to request clarification or add additional context in comments.

4 Comments

I probably think you would be getting the list of your models over the network hence you would have to include those categories inside your response data
Hey hars Thanks for you idea ...and yes its working but i am still getting same problem that my dynamically created buttons are getting overlapped with my grid items can you please suggest a way to me so that i can handle this issue..
move your android:id="@+id/lnr_test" Linear Layout Inside a ScrollView. find the update. let me know if this works or not.
Hey ,Hars i had already tried this thing ...its was giving me same response my buttons are getting overlapped with my grid item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.