Home > DeveloperSection > Forums > Android: Not Able to Access the button in a ListView present inside a Fragment?
Alex Leblois
Alex Leblois

Total Post:67

Posted on    February-03-2015 11:08 PM

 Mobile Development Android  Android Listview 

 1 Reply(s)
 487  View(s)
Rate this:

I am trying to access a button which is present inside a listview. On the buttonclick, the value of the button should change to "Hello"

The listview is part of a fragment.

The adapter takes in the values of the image, the file name and the date of creation of the file. Along with the three elements there is a button(which i will be using to delete a file)

But I am not able to access the button.

public class FragmentC extends Fragment implements AdapterView.OnItemClickListener{


ListView list;

public FragmentC() {

    // Required empty public constructor


String[] days;


public View onCreateView(LayoutInflater inflater, ViewGroup container,

                         Bundle savedInstanceState) {


    String path = Environment.getExternalStorageDirectory().toString()+"/Notate";

    File f = new File(path);

    File file[] = f.listFiles();

    days=new String[file.length];

    Integer[] imageId=new Integer[file.length];

    String[] dateTime=new String[file.length];


    for (int i=0; i < file.length; i++)


        String temp=file[i].getName();

        String temp2=temp.substring(0,temp.length()-4);

        Date lastModDate = new Date(file[i].lastModified());






    final View contextView = inflater.inflate(R.layout.fragment_fragment_c,container,false);

    CustomList adapter = new

            CustomList(this.getActivity(), days, imageId,dateTime);

    list=(ListView) contextView.findViewById(R.id.listView);



    return contextView;




public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        final Button b1=(Button) view.findViewById(R.id.button1);

        b1.setOnClickListener(new View.OnClickListener(){


            public void onClick(View v) {






Barbara Jones

Total Post:13

Posted on    February-03-2015 11:24 PM

In your CustomList class goto getview method and inside that method set click on the button. in that class you will probably be doinig like


so in getview method below that above line do like

  viewHolder.button.setOnClickListener(new View.OnClickListener(){


        public void onClick(View v) {




Don't want to miss updates? Please click the below button!

Follow MindStick