1

I'm using fragment in my project and I want to use sqlite database in part of project but I have error in part of The following class

package com.androiddeft.navigationdrawer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;


import com.androiddeft.navigationdrawer.fragments.CustomersFragment;

import java.util.List;

public class MyListAdapter extends BaseAdapter {
    private Context context;
    private List<Employee> employees;

    public MyListAdapter(Context context, List<Employee> employees){
        this.context = context;
        this.employees = employees;
    }

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

    @Override
    public Object getItem(int i) {
        return employees.get(i);
    }

    @Override
    public long getItemId(int i) {
        return employees.get(i).getId();
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        View rowView = LayoutInflater.from(context).inflate(R.layout.list_item, viewGroup, false);
        TextView txtName = (TextView) rowView.findViewById(R.id.txt_employee_name);
        TextView txtPhone = (TextView) rowView.findViewById(R.id.txt_employee_phone);
        TextView txtAddress = (TextView) rowView.findViewById(R.id.txt_employee_address);
        Button btnEdit = (Button) rowView.findViewById(R.id.btn_edit);
        Button btnDelete = (Button) rowView.findViewById(R.id.btn_delete);

        txtName.setText(employees.get(position).getName());
        txtPhone.setText(employees.get(position).getPhone());
        txtAddress.setText(employees.get(position).getAddress());

        btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                **((CustomersFragment) context).editEmployee(employees.get(position))**;
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatabaseHelper(context).deleteEmployee(employees.get(position));
                employees.remove(position);
                notifyDataSetChanged();
            }
        });

        return rowView;
    }
}

and my error message on android studio IDE is :

Inconvertible types; cannot cast 'android.content.Context' to 'com.androiddeft.navigationdrawer.fragments.CustomersFragment

Note:

Customersfragment is my fragment name and i have error in line 59 Inside two stars symbols

2 Answers2

0

Adapter already has a context. just use that.

  public class MyListAdapter extends BaseAdapter {
    private Context context;
    private List<Employee> employees;
    private CustomersFragment fragment;

    public MyListAdapter(Context context, List<Employee> employees, CustomersFragment fragment){
        this.context = context;
        this.employees = employees;
        this.fragment = fragment;
    }

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

    @Override
    public Object getItem(int i) {
        return employees.get(i);
    }

    @Override
    public long getItemId(int i) {
        return employees.get(i).getId();
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        View rowView = LayoutInflater.from(context).inflate(R.layout.list_item, viewGroup, false);
        TextView txtName = (TextView) rowView.findViewById(R.id.txt_employee_name);
        TextView txtPhone = (TextView) rowView.findViewById(R.id.txt_employee_phone);
        TextView txtAddress = (TextView) rowView.findViewById(R.id.txt_employee_address);
        Button btnEdit = (Button) rowView.findViewById(R.id.btn_edit);
        Button btnDelete = (Button) rowView.findViewById(R.id.btn_delete);

        txtName.setText(employees.get(position).getName());
        txtPhone.setText(employees.get(position).getPhone());
        txtAddress.setText(employees.get(position).getAddress());

        btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fragment.editEmployee(employees.get(position));
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatabaseHelper(context).deleteEmployee(employees.get(position));
                employees.remove(position);
                notifyDataSetChanged();
            }
        });

        return rowView;
    }
}

If you set this adapter from Fragment class then just use this

adapter = new MyListAdapter(getActivity(), employees,CustomersFragment.this);

That's it.

More information you can follow this link.
Call a Fragment method from an Adapter

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
-1

Use getActivity() inside the Fragment to obtain a Context that you can pass to MyListAdapter class using a constructor. That works, since Activity inherits from Context.

As alternative you can use getApplicationContext() to obtain the Context.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Muhammad Ibrahim
  • 289
  • 2
  • 12
  • The context exists already, look at the error - its a `ClassCastException` - `(CustomersFragment) context`, just needs to remove the cast and use `context`. – Mark Feb 04 '18 at 19:23