forum

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

Android: Not Able to Access the button in a ListView present inside a Fragment?

Anonymous User 2477 03-Feb-2015

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;
@Override
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());
 
        days[i]=temp2;
        imageId[i]=R.drawable.ic_launcher;
        dateTime[i]=lastModDate.toString().substring(0,lastModDate.toString().length()-14);
    }
    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);
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);
    return contextView;
    }
 
@Override
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(){
            @Override
            public void onClick(View v) {
                b1.setText("Hello");
            }
        });
    }
}


Updated on 15-May-2018
I am a content writter !

Can you answer this question?


Answer

2 Answers

Liked By