Android - Trying to call parent method from fragment

Daryl Fincham

I'm having trouble calling a parent function from a list adapter. Essentially this is a button that will reload the list again, after performing some DB stuff.

My list adapter is as follows:

LIST ADAPTER

public class LazyAdapterFragmentCheckInList extends BaseAdapter  {

private FragmentActivity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;

public LazyAdapterFragmentCheckInList(FragmentActivity a, ArrayList<HashMap<String, String>> d) {

    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity);

}

 public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row_checkin, null);
 Button checkin=(Button)vi.findViewById(R.id.checkin); 
    checkin.setText("reload");


    checkin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

//This is what want to call but getting error "not in enclosing class"

          CheckInListViewFragment.GetBlogPostsTask getBlogPostsTask = new CheckInListViewFragment.GetBlogPostsTask();
         getBlogPostsTask.execute();

            if(mClickListener != null)
                mClickListener.onBtnClick((Integer) v.getTag());
        }
    });
    return vi;
}

This list adapter is called from a list fragment, which has the public method "GetBlogPostsTask"

LIST FRAGMENT

public class CheckInListViewFragment extends ListFragment {

Context _context;


ListView list;
LazyAdapterFragmentCheckInList adapter;
Context appContext;
ArrayList<HashMap<String, String>> songsList;

View view;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    appContext = inflater.getContext().getApplicationContext();

     View rootView = inflater.inflate(R.layout.fragment_custom_listview_fragment, container, false);
    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.v("a=", "b");
    super.onCreate(savedInstanceState);

}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


    GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
    getBlogPostsTask.execute();
    list = (ListView) getActivity().findViewById(android.R.id.list);

    mProgressBar = (ProgressBar) getActivity().findViewById(R.id.progressBar1);
    mProgressBar.setVisibility(View.VISIBLE);

    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "trade-gothic-next-condensed-latin-1.ttf");





}



public class GetBlogPostsTask extends AsyncTask<Object, Void, JSONObject> {
    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    @Override
    public void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }



    @Override
    public JSONObject doInBackground(Object... arg0){
        Log.v("NEWURL=", NEWURL);
        JSONObject jsonResponse = null;
        jsonResponse=  JSONFunctions.getJSONfromURL(NEWURL);

        return jsonResponse;
    }
    public void logException(Exception e) {
     Log.e(TAG, "Exception Caught", e);
 }



    public void handleBlogRequest(){
      mProgressBar.setVisibility(View.INVISIBLE);
        if(mBlogData==null){
            ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> blogPost = new HashMap<String, String>();
            blogPost.put("position", "1");


            blogPosts.add(blogPost);



            adapter=new LazyAdapterFragmentCheckInList(getActivity(),blogPosts);

            list.setAdapter(adapter);



        }
        else{
            try{

                JSONArray jsonPosts = mBlogData.getJSONArray("tickets");

                int jsonLength = jsonPosts.length();
                if(jsonLength>20){
                    jsonLength = 20;
                }
                ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
                for(int  i = 0; i <jsonLength; i++){
                    JSONObject post = jsonPosts.getJSONObject(i);


                    HashMap<String, String> blogPost = new HashMap<String, String>();


                }

                adapter=new LazyAdapterFragmentCheckInList(getActivity(),blogPosts);

                list.setAdapter(adapter);

                list.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> Parent, View view, int position, long id) {

                        try{



                        }
                        catch(JSONException e){
                            logException(e);


                        }

                    }


                });


            }
            catch (JSONException e){
                logException(e);
            }

        }
    }


    @Override
    public void onPostExecute(JSONObject result) {

        mBlogData = result;
        handleBlogRequest();

    }
}


}

Im hoping this is an easy one to solve, but I'm getting confused by the layers.

blackkara

In your case the required way is below.

CheckInListViewFragment.GetBlogPostsTask getBlogPostsTask = new CheckInListViewFragment.new GetBlogPostsTask();

But this not a good solution. Separating the AsyncTask is better

Because, GetBlogPostsTask is only accessible thorough CheckInListViewFragment instance.

Usage 1

public class SampleClass {

    public class NestedClass {

    }
}

The accessible way for above usage

new SampleClass().new NestedClass();

Usage 2

public class SampleClass {

    public static class NestedClass {

    }
}

The accessible way for above usage

new SampleClass.NestedClass();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trying to call a fragment method from parent activity but the method is not being resolved

From Dev

Android Call Fragment Method from Fragment

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Call method in child fragment in parent fragment from activity

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

Unable to call Fragment's Method from Parent View

From Dev

how to call method of one fragment from another fragment class in android

From Dev

Trying to call a call back from parent actifty to fragment created in a Libary Module

From Dev

Android - How to access a getter method in parent Activity from Fragment

From Dev

Call method of interface implements with child fragment From container activity Android

From Dev

Call method of interface implements with child fragment From container activity Android

From Dev

Android Fragment - BroadcastReceiver call fragment method

From Java

Call an activity method from a fragment

From Dev

Call a Fragment method from an Adapter

From Dev

Call Fragment method from Activity

From Dev

Call Fragment method from Activity

From Dev

Call SQLiteOpenHelper method from fragment

From Dev

Call method in MainActivty from Fragment

From Dev

Call method in fragment from another fragment/activity

From Dev

Call method in fragment from another fragment/activity

From Dev

call method from fragment to fragment ( refresh adapter )

From Dev

Call AsyncTask in the parent activity from fragment

From Dev

Call AsyncTask in the parent activity from fragment

From Java

Call child method from parent

From Dev

Call method from parent class

From Dev

call childrens method from parent

From Dev

Android - How to call a fragment from inside a fragment?

From Dev

Call activity from fragment in android

From Dev

Call activity from fragment in android

Related Related

  1. 1

    Trying to call a fragment method from parent activity but the method is not being resolved

  2. 2

    Android Call Fragment Method from Fragment

  3. 3

    Call method in child fragment in parent fragment from activity

  4. 4

    Call method in child fragment in parent fragment from activity

  5. 5

    (Android) how to call method in fragment from fragmentActivity

  6. 6

    Unable to call Fragment's Method from Parent View

  7. 7

    how to call method of one fragment from another fragment class in android

  8. 8

    Trying to call a call back from parent actifty to fragment created in a Libary Module

  9. 9

    Android - How to access a getter method in parent Activity from Fragment

  10. 10

    Call method of interface implements with child fragment From container activity Android

  11. 11

    Call method of interface implements with child fragment From container activity Android

  12. 12

    Android Fragment - BroadcastReceiver call fragment method

  13. 13

    Call an activity method from a fragment

  14. 14

    Call a Fragment method from an Adapter

  15. 15

    Call Fragment method from Activity

  16. 16

    Call Fragment method from Activity

  17. 17

    Call SQLiteOpenHelper method from fragment

  18. 18

    Call method in MainActivty from Fragment

  19. 19

    Call method in fragment from another fragment/activity

  20. 20

    Call method in fragment from another fragment/activity

  21. 21

    call method from fragment to fragment ( refresh adapter )

  22. 22

    Call AsyncTask in the parent activity from fragment

  23. 23

    Call AsyncTask in the parent activity from fragment

  24. 24

    Call child method from parent

  25. 25

    Call method from parent class

  26. 26

    call childrens method from parent

  27. 27

    Android - How to call a fragment from inside a fragment?

  28. 28

    Call activity from fragment in android

  29. 29

    Call activity from fragment in android

HotTag

Archive