Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a method sendData() in my fragment. This method starts a new Activity. I want to call this method from my ArrayAdapter.

Here is my code:-

HomeFragment.java

stagAdaper = new StaggeredAdapter(myContext, android.R.layout.simple_list_item_1, getList);
            mGridView.setAdapter(stagAdaper);
    private void sendData(int position)
    {

        myDialog = new ProgressDialog(myContext).show(getActivity(), "Fetching news..", "Just a moment");

        myDialog.getWindow().setContentView(R.layout.openarticlewaitprogress);
        myDialog.getWindow().setTitle("Loading..");
        myDialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
         new  NewsDetails(myDialog);



            Intent nIntent = new Intent(getActivity(),Details.class);

               String Body=getList.get(position).getBody();

            newsIntent.putExtra("Body", Body);


            startActivity(nIntent);

    }

StaggeredAdapter.java

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

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            //viewHolder.layGridLayout.setForeground(R.drawable.foreground_selector);

        }
    });
    return convertView;
    }

How can I do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
203 views
Welcome To Ask or Share your Answers For Others

1 Answer

Edit : Here is what I would use now. Older, "easier" solutions are available below.

MyFragment extends Fragment implements CustomAdapter.EventListener {

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        CustomAdapter adapter = new CustomAdapter(..., this);

    }

    void onEvent(int data) {
        doSomething(data);
    }

}

CustomAdapter extends BaseAdapter {

    EventListener listener; 

    public interface EventListener {
        void onEvent(int data);   
    }

    public CustomAdapter(..., EventListener listener) {
        this.listener = listener; 
    }

    ...

}

Now from any place in the adapter we can call listener.onEvent(data); to trigger the method in the fragment.

Moreover, instead of providing a listener through the constructor, we can add another method in the adapter such as registerListener(EventListener eventListener) and then maintain a list of listeners if needed.

Old Answer:

Solution 1 : Make the adapter an inner class of your fragment, so that you can call the method directly.

Solution 2 : Update your adapter constructor to accept the Fragment as a parameter.

Something like :

customAdapter = new CustomAdapter(myContext, android.R.layout.simple_list_item_1, getList, HomeFragment.this);

and update the constructor of the Adapter :

public CustomAdapter(Context context, int id, HomeFragment fragment) {
    this.fragment = fragment;
}

then you call methods using the fragment variable.

fragment.doSomething();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...