I am using Facebbok
sharing in my app,i have one listview
and in my list item i have one Button
,on click of the Button
i am trying to share my stuffs,but the issue is my code works fine when i extend my class with activity,but it does not work with Fragment
,Following is my snippet code,any help
TestFrag.java
public class TestFrag extends Fragment{
static CallbackManager callbackManager;
private static LoginManager manager;
ListView list;
Button btnPost;
Context context;
static Activity activity;
ArrayList prgmName;
public static int[] prgmImages = {R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher};
public static String[] prgmNameList = {"Let Us C", "c++", "JAVA", "Jsp",
"Microsoft .Net", "Android", "PHP", "Jquery", "JavaScript"};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getActivity());
View rootView = inflater.inflate(R.layout.tester, container, false);
callbackManager = CallbackManager.Factory.create();
context = getActivity();
activity = getActivity();
list = (ListView)rootView.findViewById(R.id.listView);
list.setAdapter(new CustomListAdapter(getActivity(), prgmNameList, prgmImages));
return rootView;
}
public static void publishImage() {
Bitmap image = BitmapFactory.decodeResource(activity.getResources(),
R.drawable.ic_launcher);
SharePhoto photo = new SharePhoto.Builder().setBitmap(image)
.setCaption("Welcome To Facebook Photo Sharing on steroids!")
.build();
SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(
photo).build();
ShareApi.share(content, null);
Toast.makeText(activity, "Succsesfully posted on your wall",
Toast.LENGTH_LONG).show();
}
@Override
public void onActivityResult(int requestCode, int responseCode,
Intent data) {
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}
public static void FaceBookSharing() {
// title = ed_title.getText().toString();
// des = ed_dec.getText().toString();
//
// callbackManager = CallbackManager.Factory.create();
// shareDialog = new ShareDialog(this);
// shareDialog.registerCallback(callbackManager,
// new FacebookCallback<Sharer.Result>() {
//
// @Override
// public void onCancel() {
// System.out.println("Cancel");
// }
//
// @Override
// public void onError(FacebookException e) {
// e.printStackTrace();
// }
//
// @Override
// public void onSuccess(Sharer.Result result) {
// System.out.println("Success");
//
// }
// });
//
// if (ShareDialog.canShow(ShareLinkContent.class)) {
//
// ShareLinkContent linkContent = new ShareLinkContent.Builder()
// .setContentTitle(title).setContentDescription(des)
// .setContentUrl(Uri.parse(image))
// .setImageUrl(Uri.parse(image)).build();
//
// shareDialog.show(linkContent);
//
// }
List<String> permissionNeeds = Arrays.asList("publish_actions");
manager = LoginManager.getInstance();
manager.logInWithPublishPermissions(activity, permissionNeeds);
manager.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
publishImage();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
@Override
public void onError(FacebookException error) {
// TODO Auto-generated method stub
}
});
}
public void onClick(View v) {
if (v == btnPost) {
FaceBookSharing();
}
}
public class CustomListAdapter extends BaseAdapter {
String[] result;
Context context;
int[] imageId;
private LayoutInflater inflater = null;
public CustomListAdapter(Context context, String[] prgmNameList,
int[] prgmImages) {
// TODO Auto-generated constructor stub
result = prgmNameList;
this.context = context;
imageId = prgmImages;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
Button tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom, null);
holder.tv = (Button) rowView.findViewById(R.id.btnPost);
holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
// holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
holder.tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + result[position],
Toast.LENGTH_LONG).show();
TestFrag.FaceBookSharing();
}
});
return rowView;
}
}
}