Android - 尝试从片段调用父方法

时间:2016-04-28 22:13:00

标签: android listadapter

我无法从列表适配器调用父函数。 基本上这是一个按钮,在执行一些数据库后,会再次重新加载列表。

我的列表适配器如下:

列表适配器

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) {
  

//这是想要调用但得到的错误“不在封闭类”

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

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

从列表片段调用此列表适配器,该片段具有公共方法“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();

    }
}


}

我希望这是一个很容易解决的问题,但我对各层感到困惑。

1 个答案:

答案 0 :(得分:0)

在您的情况下,所需的方式如下。

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

但这不是一个好的解决方案。分离AsyncTask更好

因为GetBlogPostsTask只能通过CheckInListViewFragment实例访问。

用法1

public class SampleClass {

    public class NestedClass {

    }
}

以上用途的可访问方式

new SampleClass().new NestedClass();

用法2

public class SampleClass {

    public static class NestedClass {

    }
}

以上用途的可访问方式

new SampleClass.NestedClass();