-1

I am developing one application where I am using ListView now what i am trying is I want to add one Button in every alternate listitem,

Something like this

listitem1
Button listitem2
listitem3
Button listitem4

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="match_parent"
    android:orientation="vertical"
    >



    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="180dp"
        android:id="@+id/id"
        android:background="@drawable/heads">
        <RelativeLayout
            android:layout_height="40dp"
            android:id="@+id/rl"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:padding="2dp"
                       android:text="abd"
                android:id="@+id/txt_allproductsname"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_marginTop="5dp"
                android:padding="2dp"
                android:text="abddd"
                android:id="@+id/txt_allproductsquty"
                android:layout_alignParentRight="true"
                />
        </RelativeLayout>
    </RelativeLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btntest"
        android:text="test"
        />

</LinearLayout>

Code

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_homefrags, null);
        // holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
        holder.txtproname = (TextView) convertView.findViewById(R.id.txt_allproductsquty);
        // holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidacptedlist);
        holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txt_allproductsname);

        holder.testbtn=(Button)convertView.findViewById(R.id.btntest);
        // holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileageacptedlist);
        // holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplaceacptedlist);


        if (position % 1 == 0) {
            holder.testbtn.setVisibility(View.VISIBLE);
        } else {
            holder.testbtn.setVisibility(View.GONE);
        }

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
}
HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
Aditya
  • 1,508
  • 1
  • 19
  • 37

3 Answers3

2

position % 1 is always 0. You should use position % 2 == 1 to get the result you describe.

The '%' in Java represents a modulo operation. The modulo operation returns the fraction of an integer division which could not be divided. For example, if you divide 10 by 5 you get 2 with a rest of 0. But when you would divide 13 by 5 you would get 2 with a rest of 3, hence 12%5 would return 3

TmKVU
  • 2,910
  • 2
  • 16
  • 30
1

You can add button to the item layout of the ListView and set the visiblity of the adapters getView method. You need to go for the custom adapter for this type of ListView.

You need to put the below line of code

if (position % 2 == 0) {
    holder.testbtn.setVisibility(View.VISIBLE);
} else {
    holder.testbtn.setVisibility(View.GONE);
}

before the return statement.

ByteHamster
  • 4,884
  • 9
  • 38
  • 53
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40
  • tell me http://stackoverflow.com/questions/31847086/how-to-attach-jpg-or-png-file-to-gmail-or-facebook – Aditya Aug 06 '15 at 06:36
1

Try doing this

As you are writing the showing/hiding code in

if(convertView==null){
 // only executes if convertview is null
}else{
  // get cached element
}

so move showing/hiding logic after else

@Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_homefrags, null);
               // holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
                holder.txtproname = (TextView) convertView.findViewById(R.id.txt_allproductsquty);
               // holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidacptedlist);
                holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txt_allproductsname);

                holder.testbtn=(Button)convertView.findViewById(R.id.btntest);
               // holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileageacptedlist);
               // holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplaceacptedlist);




                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

                if (position % 2 == 0) {

                    holder.testbtn.setVisibility(View.VISIBLE);

                } else {
                    holder.testbtn.setVisibility(View.GONE);
                }
N J
  • 27,217
  • 13
  • 76
  • 96
  • as viewholder pattern is used for caching purpose see this link http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html – N J Jul 11 '15 at 11:59
  • can you tell http://stackoverflow.com/questions/31847086/how-to-attach-jpg-or-png-file-to-gmail-or-facebook – Aditya Aug 06 '15 at 06:36