12

So I have a custom listview. It's like Instagram layout with 1 image and bunch of buttons in each list items. So here's the problem:

I want to implement the share button. To do this, I tried to create a callback from adapter to activity. But it didn't seem to work. Here is what I have so far(I cropped out the unrelated parts):

MainActivity

public class MainActivity extends ActionBarActivity implements ListAdapter.OnShareClickedListener{

    ListView main_list;
    List<String> url_list; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_list = (ListView) findViewById(R.id.listView);
        ListAdapter nListAdapter = new ListAdapter(context, url_list);
        main_list.setAdapter(nListAdapter);
    }

    @Override
    public void ShareClicked(String url) {
        Log.e("Test",url);
    }

}

ListAdapter

public class ListAdapter extends BaseAdapter implements View.OnClickListener {

    OnShareClickedListener mCallback;
    Context context;
    public static List<String> url_list;

    public ListAdapter(Context c, List<String> list) {
        this.context = c;
        url_list = list;
    }

    public interface OnShareClickedListener {
        public void ShareClicked(String url);
    }


    @Override
    public void onClick(View v) {
                mCallback.ShareClicked("Share this text.");
        }
    }

}

Error Log:

Attempt to invoke interface method 'void com.packagename.ListAdapter$OnShareClickedListener.ShareClicked(java.lang.String)' on a null object reference
Wiillyx3
  • 151
  • 2
  • 2
  • 5
  • Can't.Gives the error: OnShareClickedListener is abstract. Cannot be instantiated. – Wiillyx3 Aug 16 '15 at 07:47
  • Is it correct to assume ListAdapter.OnShareClickedListener is a custom implementation of a third-party library? Search leads me to `codota` website. If yes, what are the native alternatives. Possibly follow [link](https://www.milesburton.com/Android_-_Building_a_ListView_with_an_OnClick_Position) – r.sumesh Mar 05 '20 at 11:42

1 Answers1

21

You need to tell the adapter which implementation of the OnShareClickedListener() to use. Right now in your adapter the field mCallback is never assigned to, either you need to have a setOnSharedClickedListener() method in your adapter which you then call from your mainActivity and set it with the main activity's implementation or you need to take in the constructor.

My suggestion would be to use a setter instead of constructor. So what you need to do is this.

Your ListAdapter

public class ListAdapter extends BaseAdapter implements View.OnClickListener {

    OnShareClickedListener mCallback;
    Context context;
    public static List<String> url_list;

    public ListAdapter(Context c, List<String> list) {
        this.context = c;
        url_list = list;
    }

    public void setOnShareClickedListener(OnShareClickedListener mCallback) {
        this.mCallback = mCallback;
    }

    public interface OnShareClickedListener {
        public void ShareClicked(String url);
    }


    @Override
    public void onClick(View v) {
        mCallback.ShareClicked("Share this text.");
    }
}

Your MainActivty

public class MainActivity extends ActionBarActivity implements ListAdapter.OnShareClickedListener{

    ListView main_list;
    List<String> url_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_list = (ListView) findViewById(R.id.listView);
        ListAdapter nListAdapter = new ListAdapter(this, url_list);
        nListAdapter.setOnShareClickedListener(this);
        main_list.setAdapter(nListAdapter);
    }

    @Override
    public void ShareClicked(String url) {
        Log.e("Test", url);
    }

}
Bhargav
  • 8,118
  • 6
  • 40
  • 63
  • You have to instantiate OnShareClickedListener in your activity and pass that instance in constructor of your List adapter. Assign mCallback with that passed OnShareClickedListener instance. The problem with your code was you are not initialized Listener object. – Suhas K Aug 16 '15 at 08:11
  • I like de coupling callbacks from adapter initializations – Bhargav Aug 16 '15 at 08:13