1

I have a custom Adapter that has a click Listener on the ListView image.

This code is inside the Adapter GetView:

holder.iconImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

          //code to do stuff when the image is clicked
}

My images in the listview consist of images of folders (to represent a folder) and actual images.

So if the user clicks on a folder image, I want to be able to call a method called "moveDownFolder" in ActivityA.

I'm not sure if this can be done inside of the Adapter?

[EDIT] Basically in ActivityA I have a Click Listener on the custom ListView, so if they click on the text in the custom ListView, it moves down a folder (if it is a folder).

But they can click on an image in the ListView, it opens the image in a new activity. BUT if they click on the folder icon in the listview (which is an image of course in the ImageView) - I want the code to move down a folder, instead of trying to open the image.

user3437721
  • 2,227
  • 4
  • 31
  • 61

3 Answers3

9

I Recommending on using interface as listener.

For example:

1, create your interface:

public interface MyListener {

void folderClicked();
}

2, Let your ActivityA to implement this Interface like this:

public class ActivityA extends Activity implements MyListener 

3, you will have to auto overide the method folderClicked it will look like this:

@Override
protected void folderClicked() {
  // Do your stuff here.

}

4, send the activity listener to the adapter with constructor like this:

MyAdpater adapter = new MyAdpater(ActivityA.this);

5, in your adapter class your code should be like this :

public class TimeLineAdapter extends BaseAdapter {

private MyListener mListener;


public TimeLineAdapter(MyListener listener) {
    super();

    mListener = listener;


}

holder.iconImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         mListener.onFolderClicked()
      //code to do stuff when the image is clicked
 }
SacreDeveloper
  • 1,253
  • 1
  • 9
  • 16
  • Thanks I'll try this. Is there any easy way to tell if the user has clicked on a folder image (it is a drawable called folder.png). So if they click on the folder then I can call movedownfolder? – user3437721 Apr 24 '14 at 13:54
  • Yes, are you using objects in your adapter? You can always get the clicked position and then get the item position with the folder details. – SacreDeveloper Apr 24 '14 at 14:00
  • yeah I am, I have the clicked position, but I want to know if it is a folder that was clicked. Specifically, is there any way to check if the image at that position is a drawable? – user3437721 Apr 24 '14 at 14:14
  • im not sure what you asking for, but if the user clicked on the imageView in lets say row 5, isnt it mean he clicked the folder picture? – SacreDeveloper Apr 24 '14 at 14:15
  • the list view has 'proper' images, and folder images. If they click on a folder image I want to "movedownfolder". Ideally lookign for something like this: images.get(position) == Bitmap(R.drawable.dbfolder); – user3437721 Apr 24 '14 at 14:26
  • Oh, there no such method that can equals drawable. you will have to think on something smarter. like give a boolean inside each object lets say isFolder = true; then check it on the onClick method. – SacreDeveloper Apr 24 '14 at 14:29
  • Ok I'll mark your answer as correct as I think it will work. but one last question, if they click on the folder icon, is there a way to simulate the ListView on click Listener in ActivityA? – user3437721 Apr 24 '14 at 14:52
  • Yes, you can use the listView function onItemClickListener like this: listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> adapterView, View view, int position, long l) { } }); To get the item clicked when your listView. – SacreDeveloper Apr 24 '14 at 14:55
  • I need to pass an int position from the Interface to ActivityA. public void folderClicked(int position) - but when calling the interface in the adapter like this mListener.folderClicked(position); -- I'm getting a null pointer exception? The position does have a value.. – user3437721 Apr 24 '14 at 15:32
  • could you change your example to pass an int position, to see if it is the same as what I have? Note that I already had a constructor in my Adapter, but now I have two, a new one I added for the Interface.. – user3437721 Apr 24 '14 at 15:36
  • First, change your method in interface class to void folderClicked(int position); second, add the int position to overrided method in your activity. third, when you call this method just pass the row position – SacreDeveloper Apr 24 '14 at 16:10
  • I've asked another question as I'm getting anull pointerhttp://stackoverflow.com/questions/23273906/interface-use-in-adapter – user3437721 Apr 24 '14 at 17:23
  • perhaps you will be able to answer as its based on the code you provided. http://stackoverflow.com/questions/23273906/interface-use-in-adapter – user3437721 Apr 25 '14 at 08:56
  • i will help you there then. – SacreDeveloper Apr 25 '14 at 18:22
  • Thank you this is the proper code to change textview from adapter to activity – Faisal Jun 20 '17 at 07:55
1

Yes it can be done , just make your function as follows static public moveDownFolder() . You need to be sure that activity is created. to call that function from Adapter just simply do : YourActivity.moveDownFolder()

1

If the adapter is an inner class of ActivityA, try:

ActivityA.this.moveDownFolder();
copakawusau
  • 145
  • 1
  • 9