BaseAdapter getLayoutInflater giving runtime exception

advertisements

I have a class postedJobHistory (Fragment):

public class PostedJobHistory extends Fragment {
    String employerName;
    private List<ParseObject> jobDetails;
    Context context;
    public PostedJobHistory() {}
    public PostedJobHistory(Context context, String employerName) {
        this.context = context;
        this.employerName = employerName;
    }
    ListView listViewJobHistory;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View jobsView = inflater.inflate(R.layout.fragment_posted_job_history, container, false);
        listViewJobHistory = (ListView) jobsView.findViewById(R.id.listViewJobHistory);

        if(jobDetails==null){
            jobDetails=new ArrayList<ParseObject>();
            EmployerHistory jobsQuery=new EmployerHistory();
            jobDetails = jobsQuery.getJobHistory(employerName);
        }

        listViewJobHistory.setAdapter(new TimelineList(context, jobDetails));

        return jobsView;
    }
}

For the Adapter I have this class in the same .java file as above:

class TimelineList extends BaseAdapter {
    private List<ParseObject> elements;
    Context mContext;
    public TimelineList(Context mContext, List<ParseObject> elements) {
        this.mContext = mContext;
        this.elements = elements;
    }

    @Override
    public int getCount() {
        return elements.size();
    }

    @Override
    public ParseObject getItem(int position) {
        return elements.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

//this line is giving runtime exception
            LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
            View row;
            row = inflater.inflate(R.layout.job_history_listitem, parent, false);
            TextView title, detail;
            title = (TextView) row.findViewById(R.id.title);
            detail = (TextView) row.findViewById(R.id.detail);
            title.setText(getItem(position).get("JobName").toString());
            title.setMovementMethod(LinkMovementMethod.getInstance());
            detail.setText(getItem(position).get("updatedAt").toString());
            return (row);
        }
    }

07-28 02:06:12.424 17392-17392/com.example.ankit.job_depot E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.ankit.job_depot, PID: 17392 java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity at com.example.ankit.job_depot.TimelineList.getView(PostedJobHistory.java:80) at android.widget.AbsListView.obtainView(AbsListView.java:2347) at android.widget.ListView.measureHeightOfChildren(ListView.java:1270) at android.widget.ListView.onMeasure(ListView.java:1182) at android.view.View.measure(View.java:17547) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463) at android.view.View.measure(View.java:17547) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1455) at android.view.View.measure(View.java:17547) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)

here it is given that Context cannot be casted to an Activity as it is not its subclass. I am not sure how to resolve this issue.


This is wrong

public PostedJobHistory(Context context, String employerName) {
    this.context = context;
    this.employerName = employerName;
}

Fragment should have no argument constructor

Secondly you can get context of activity using getActivity() in onCreateView

listViewJobHistory.setAdapter(new TimelineList(getActivity(), jobDetails));

Then in Constructor of your adapter

LayoutInflate inflater;
public TimelineList(Context mContext, List<ParseObject> elements) {
    inflater = LayoutInflater.from(mContext);
    this.elements = elements;
}

If you need to pass data from activity to fragment

Send data from activity to fragment in android

Also you should be using a ViewHolder pattern

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Note: All subclasses of Fragment must include a public no-argument constructor.

Source :

http://developer.android.com/reference/android/app/Fragment.html