我正在使用自定义适配器向/从Sq-lite传递数据。我需要在适配器类的以下方法中传递片段的引用。怎么能实现呢? **要写什么而不是“FragmentGroups.getActivity()”?
public void Set_Referash_Data() {
data.clear();
db = new SendJobDataBase(new FragmentGroups.getActivity());
ArrayList<member> contact_array_from_db = db.Get_Contacts();
for (int i = 0; i < contact_array_from_db.size(); i++) {
int tidno = contact_array_from_db.get(i).getID();
String name = contact_array_from_db.get(i).getName();
// String mobile = contact_array_from_db.get(i).getPhoneNumber();
// String email = contact_array_from_db.get(i).getEmail();
member cnt = new member();
cnt.setID(tidno);
cnt.setName(name);
// cnt.setEmail(email);
// cnt.setPhoneNumber(mobile);
data.add(cnt);
}
db.close();
gdb = new GroupAdapter(FragmentGroups.getActivity(), R.layout.listrow_item,
data);
l1.setAdapter(gdb);
gdb.notifyDataSetChanged();
}
GroupAdapter.java:
package com.sunmobile.navigationdrawer;
import java.util.ArrayList;
import com.sunmobileappnow.mobileappnow.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class GroupAdapter extends ArrayAdapter<member> {
GroupAdapter gdb;
Activity activity;
int layoutResourceId;
member user;
ArrayList<member> data = new ArrayList<member>();
ListView l1;
SendJobDataBase db;
public GroupAdapter(Activity act, int layoutResourceId,
ArrayList<member> data) {
super(act, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.activity = act;
this.data = data;
notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MemberHolder holder = null;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MemberHolder();
holder.name = (TextView) row.findViewById(R.id.user_name_txt);
holder.delete = (Button) row.findViewById(R.id.btn_delete);
row.setTag(holder);
} else {
holder = (MemberHolder) row.getTag();
}
user = data.get(position);
holder.name.setText(user.getName());
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(activity);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete ");
final int user_id = Integer.parseInt(v.getTag().toString());
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok",
new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// MyDataObject.remove(positionToRemove);
SendJobDataBase dBHandler = new SendJobDataBase(
activity.getApplicationContext());
dBHandler.Delete_Contact(user_id);
GroupAdapter.this.onResume();
}
});
adb.show();
}
});
return row;
}
public void Set_Referash_Data() {
data.clear();
db = new SendJobDataBase(FragmentGroups.this.getActivity());
ArrayList<member> contact_array_from_db = db.Get_Contacts();
for (int i = 0; i < contact_array_from_db.size(); i++) {
int tidno = contact_array_from_db.get(i).getID();
String name = contact_array_from_db.get(i).getName();
data.add(cnt);
}
db.close();
gdb = new GroupAdapter(FragmentGroups.this.getActivity(), R.layout.listrow_item, data);
l1.setAdapter(gdb);
gdb.notifyDataSetChanged();
}
class MemberHolder {
TextView name;
Button delete;
}
}
FragmentGroups:
package com.sunmobile.navigationdrawer;
import java.util.ArrayList;
import java.util.List;
import android.app.Fragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.sunmobileappnow.mobileappnow.PasswordDB;
import com.sunmobileappnow.mobileappnow.R;
public class FragmentGroups extends Fragment {
SendJobDataBase db;
Cursor cursor;
ArrayList<member> membr,membr2;
ListView list1 ;
ListView list2 ;
Button group1;
Button group2;
GroupAdapter groupadapter;
public FragmentGroups(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.group_fragment, container, false);
group1 = (Button) rootView.findViewById(R.id.Gp1);
group2 = (Button) rootView.findViewById(R.id.Gp2);
list1 = (ListView) rootView.findViewById(R.id.list1);
this.db = new SendJobDataBase(getActivity());
membr=db.getMembersList();
groupadapter= new GroupAdapter(this.getActivity(),R.layout.listrow_item ,membr);
list1.setAdapter( groupadapter);
list2 = (ListView) rootView.findViewById(R.id.list2);
membr2=db.getMembersList2();
groupadapter= new GroupAdapter(this.getActivity(),R.layout.listrow_item ,membr2);
list2.setAdapter( groupadapter);
group1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
list1.setVisibility(View.VISIBLE);
list2.setVisibility(View.INVISIBLE);
}
});
group2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
list1.setVisibility(View.GONE);
list2.setVisibility(View.VISIBLE);
}
});
return rootView;
}
}
答案 0 :(得分:0)
形成您的评论
无法对非静态方法进行静态引用getActivity() 来自Fragment
类型
使用
FragmentGroups.this.getActivity()
而不是
FragmentGroups.getActivity()
编辑:
将以下内容移至FragmentGroups
班级
public void Set_Referash_Data() {
data.clear();
db = new SendJobDataBase(FragmentGroups.this.getActivity());
ArrayList<member> contact_array_from_db = db.Get_Contacts();
for (int i = 0; i < contact_array_from_db.size(); i++) {
int tidno = contact_array_from_db.get(i).getID();
String name = contact_array_from_db.get(i).getName();
data.add(cnt);
}
db.close();
gdb = new GroupAdapter(FragmentGroups.this.getActivity(), R.layout.listrow_item, data);
l1.setAdapter(gdb);
gdb.notifyDataSetChanged();
}
如果您想要片段,请致电Set_Referash_Data
。