我在我的项目中使用片段,我想在项目的一部分中使用sqlite
数据库,但我在以下类的部分内容有错误
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;
}
}
我在android studio IDE上的错误信息是:
不可转换的类型;无法投射&#39; android.content.Context&#39;来自com.androiddeft.navigationdrawer.fragments.CustomersFragment
注意:
Customersfragment是我的片段名称,我在第59行有错误 里面有两颗星符号
答案 0 :(得分:0)
适配器已有上下文。只是使用它。
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;
}
}
如果从Fragment类设置此适配器,则只需使用此
adapter = new MyListAdapter(getActivity(), employees,CustomersFragment.this);
就是这样。
您可以点击此链接获取更多信息 Call a Fragment method from an Adapter
答案 1 :(得分:-1)
在Fragment中使用getActivity()
来获取可以使用构造函数传递给MyListAdapter
类的Context。这是有效的,因为Activity继承自Context。
作为替代方案,您可以使用getApplicationContext()
来获取上下文。