0

I have two button inside mylist.xml which is inserted over listview in main.xml using arrayadapter.It is showing me the data in application but only click functionality is not working. Not be able to click list and both buttons.

activity_main.xml

( In this file I have just made listview using xml in which my custom list "mylist.xml" is going to inserted. )

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ListView>

</LinearLayout>

mylist.xml

( In this file all the content of listview lv which is there on mainactivity.These file need to be inserted in that listview. )

<?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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginTop="10dp"
        android:id="@+id/s"
        android:gravity="center_horizontal"
        android:textSize="20dp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:id="@+id/c"
        android:textSize="15dp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:id="@+id/d"
        android:textSize="15dp"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="left">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/b1"
            android:text="UPDATE"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/b2"
            android:text="DELETE"/>

    </LinearLayout>

</LinearLayout>

MainActivity Class

public class MainActivity extends AppCompatActivity
{
        String S1[]={"hello","1","2"},C1[]={"ello2","1","2"},D1[]={"hello3","3","4"};
        Button b1,b2;
        ListView lv;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lv=findViewById(R.id.lv);
            b1=findViewById(R.id.b1);
            b2=findViewById(R.id.b2);

            // display data
            CustomAdapter myAdapter=new CustomAdapter(this,S1,C1,D1);
            lv.setAdapter(myAdapter);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Toast.makeText(MainActivity.this,"ello"+position,Toast.LENGTH_LONG).show();
                    b1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(MainActivity.this,"ello",Toast.LENGTH_LONG).show();
                        }
                    });

                    b2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(MainActivity.this,"ello",Toast.LENGTH_LONG).show();
                        }
                    });
                }
            });

        }
}


CustomAdapter class

public class CustomAdapter extends ArrayAdapter<String>
{
    private final Activity context;
    private final String Subject1[],Chapter1[],Details1[];


    public CustomAdapter(Activity context,String Subject1[], String Chapter1[], String Details1[])
    {
        super(context,R.layout.mylist,Subject1);
        this.context=context;
        this.Subject1=Subject1;
        this.Chapter1=Chapter1;
        this.Details1=Details1;
    }

    public View getView(int position, View view, ViewGroup parent)
    {
        LayoutInflater inflater=context.getLayoutInflater();
        view=inflater.inflate(R.layout.mylist, null);
        TextView s =view.findViewById(R.id.s);
        TextView c= view.findViewById(R.id.c);
        TextView d= view.findViewById(R.id.d);
        s.setText(Subject1[position]);
        c.setText(Chapter1[position]);
        d.setText(Details1[position]);
        return view;
    };
}

3 Answers3

0

Try this :

In MainActivity :

   public class MainActivity extends AppCompatActivity{
    String S1[]={"hello","1","2"},C1[]={"ello2","1","2"},D1[]={"hello3","3","4"};
    ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=findViewById(R.id.lv);

        // display data
        CustomAdapter myAdapter=new CustomAdapter(this,S1,C1,D1);
        lv.setAdapter(myAdapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // this click work for only on list items not work on list components 
          //   you have to do this functionality in your **CustomAdapter** for list components.  

            }
        });

    }
 }

Now, in CustomAdapter :

 public class CustomAdapter extends ArrayAdapter<String>{
  private final Activity context;
  private final String Subject1[],Chapter1[],Details1[];


  public CustomAdapter(Activity context,String Subject1[], String Chapter1[], String Details1[])
{
    super(context,R.layout.mylist,Subject1);
    this.context=context;
    this.Subject1=Subject1;
    this.Chapter1=Chapter1;
    this.Details1=Details1;
}

public View getView(int position, View view, ViewGroup parent)
{
    LayoutInflater inflater=context.getLayoutInflater();
    view=inflater.inflate(R.layout.mylist, null);
    TextView s =view.findViewById(R.id.s);
    TextView c= view.findViewById(R.id.c);
    TextView d= view.findViewById(R.id.d);
    Button b1 = view.findViewById(R.id.b1);
    Button b2 = view.findViewById(R.id.b2);
    s.setText(Subject1[position]);
    c.setText(Chapter1[position]);
    d.setText(Details1[position]);

    // here you have to do click thing :

     b1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"HELLO BUTTON 1",Toast.LENGTH_LONG).show();
                    }
                });

      b2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"HELLO BUTTON 2",Toast.LENGTH_LONG).show();
                    }
                });

    return view;
};
 }
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
0

There are two places where you can put the butonClick.

1: Inside your adapter:-

    public class CustomAdapter extends ArrayAdapter<String>{
  private final Activity context;
  private final String Subject1[],Chapter1[],Details1[];


  public CustomAdapter(Activity context,String Subject1[], String Chapter1[], String Details1[])
{
    super(context,R.layout.mylist,Subject1);
    this.context=context;
    this.Subject1=Subject1;
    this.Chapter1=Chapter1;
    this.Details1=Details1;
}

public View getView(int position, View view, ViewGroup parent)
{
    LayoutInflater inflater=context.getLayoutInflater();
    view=inflater.inflate(R.layout.mylist, null);
    TextView s =view.findViewById(R.id.s);
    TextView c= view.findViewById(R.id.c);
    TextView d= view.findViewById(R.id.d);
    Button b1 = view.findViewById(R.id.b1);
    Button b2 = view.findViewById(R.id.b2);
    s.setText(Subject1[position]);
    c.setText(Chapter1[position]);
    d.setText(Details1[position]);




     button1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                           //Button1 click here
                        Toast.makeText(getApplicationContext(),"BUTTON 1 Position : "+ position,Toast.LENGTH_LONG).show();

                        }
                    });

          button2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        //Button2 click here
Toast.makeText(getApplicationContext(),"BUTTON 2 Position : "+ position,Toast.LENGTH_LONG).show();
                        }
                    });

        return view;
    };
     }

2:In your Activity:-

to put a buttonClick listener inside your listview first make a listener like:

    public interface OnclickListener {
    void onButtonClickHere(int position);
}

And then in your Activity:

ListAdapter adapter;
OnclickListener onclickListener;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

 private void init() {
    onclickListener = this;
    filterNames = getResources().getStringArray(R.array.finalfilter_names);
    filterMethods = getResources().getStringArray(R.array.filter_method_names);

    listView = (ListView) 
       findViewById(R.id.idListView);

          adapter = new ListAdapter(groceryList, getApplicationContext(), onclickListener);


}

you can access the onclick here

    @Override
public void onButtonClickHere(int position) {
    Filter filter = null;
    if(position == 0){
         filter = FilterPack.getAweStruckVibeFilter(this);
    }else if(position == 1){
         filter = FilterPack.getMarsFilter(this);
    }else if(position == 2){
         filter = FilterPack.getStarLitFilter(this);
    }
    filtered = bitmap.copy(bitmap.getConfig(), true);

    // apply filter
    imgImage.setImageBitmap(filter.processFilter(filtered));

    // struck mars starlit

}

And then in your adapter :

add this in your constructor

OnclickListener onclickListener;


 public ListAdapter(List<ButtonsBean> horizontalGrocderyList, Context context,OnclickListener onclickListener ){
    this.buttonsBeanList = horizontalGrocderyList;
    this.context = context;
    this.onclickListener = onclickListener;  // Add this to your constructor
}

and inside getView()

add this line:

            onclickListener.onButtonClickHere(position);
Kevin Kurien
  • 812
  • 6
  • 14
  • brother how to get position of that button means it is clicked from which position . Because no of list items are there. I just want position of that update and delete button at which position it is situated. Do change on your first way of doing that in adapter – Sachin Pant Mar 23 '19 at 10:25
  • @Sachin Pant the position in the getView() method is the the position of the list item i.e. if you clicked the button in the 4t item then position will be 3 (since position of listview starts from 0 ) – Kevin Kurien Mar 23 '19 at 12:06
  • would you please use getview inside your 1st solution. Just do change in the comment.Because it will be easy to see that in code.I am not understanding how to use getview. Just toast one message- if user click on button , print "button in position 0 or wherever" – Sachin Pant Mar 23 '19 at 13:11
  • @SachinPant I have edited my answer.Hope you have understood by this – Kevin Kurien Mar 23 '19 at 13:25
  • I'm getting 10 and 20 from the update and delete button which are in first place. I have checked it out. – Sachin Pant Mar 23 '19 at 13:32
  • Thanks brother , It's working "Button 10" that was correct. String got concatenated. In "10" 1 for button and 0 for position – Sachin Pant Mar 23 '19 at 13:38
  • @SachinPant glad I could help you. If this is the right answer for your ques then can you mark it has the right answer. cheers:) – Kevin Kurien Mar 23 '19 at 19:24
0

update the parent layout in your mylist.xml with

android:descendantFocusability="blocksDescendants"

like the following...

<?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:descendantFocusability="blocksDescendants"
    android:orientation="vertical">

If any row item of list contains focusable or clickable view then OnItemClickListener won't work.

I have found the solution here

Alternatively, you may try setting onClickListeners under getView() method of the list adapter like this (make int position parameter of getView() method final and use it inside your onclick() method). Just copy and paste the getView() method in your adapter ...

public View getView(final int position, View view, ViewGroup parent)
    {
        LayoutInflater inflater=context.getLayoutInflater();
        view=inflater.inflate(R.layout.mylist, null);
        TextView s =view.findViewById(R.id.s);
        TextView c= view.findViewById(R.id.c);
        TextView d= view.findViewById(R.id.d);
        s.setText(Subject1[position]);
        c.setText(Chapter1[position]);
        d.setText(Details1[position]);

        //Do like this
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do whatever you want here with the position

            }
        });
        b1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"BUTTON1 clicked: position=" + position,Toast.LENGTH_LONG).show();
                    }
                });

        b2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"BUTTON2 clicked: position=" + position,Toast.LENGTH_LONG).show();
                    }
                });
        return view;
    };
Sayan Mukherjee
  • 352
  • 2
  • 21
  • Sir i also want the position of that button means from which item no is clicked. That's why I have written btn.setonclicklistener inside list.setonitemlistener to get position . Please tell me how do I get that position – Sachin Pant Mar 23 '19 at 09:44
  • You can easily get the position in the getView() method. I will edit my answer. – Sayan Mukherjee Mar 23 '19 at 12:21
  • did you try using "android:descendantFocusability="blocksDescendants" " in your layout? and check the OnItemClickListener in your activity? did it not work? – Sayan Mukherjee Mar 23 '19 at 12:28
  • tried "android:descendantFocusability="blocksDescendants". While clicking the list, app get closed. – Sachin Pant Mar 23 '19 at 13:04
  • I have updated my answer just dont forget to make the position parameter "final", then use it in the onClick() method. – Sayan Mukherjee Mar 23 '19 at 13:06
  • Brother just do changes in code for that getView(). I just want a toast message when b1 and b2 is clicked with their respected position in the listview – Sachin Pant Mar 23 '19 at 13:14
  • updated the answer, just copy and paste the getView() method only. Delete setOnItemClickListener() in your activity and run the code. – Sayan Mukherjee Mar 23 '19 at 13:27
  • for first item , its giving me 10 and 20. That's not giving the right position. I think that position is different – Sachin Pant Mar 23 '19 at 13:29
  • Thanks Brother, It was correct . I got confused with output, because string were concatenated. I thought 10 but it was Button 1+0 – Sachin Pant Mar 23 '19 at 13:39
  • Kindly up vote the answer so that other novice developers find it helpful. – Sayan Mukherjee Mar 25 '19 at 09:07