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.
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.
Comments