我有一个自定义ArrayAdapter,每个项目都有一个共享按钮。我使用facebook分享对话框在常规活动中没有任何问题,但现在我无法让它共享我的列表项。
如果我是对的,facebook共享对话框需要一个活动才能工作,我的自定义ArrayAdapter没有,但我必须将数据从单个项目传递到共享对话框,所以它必须进入arrayadapter类,对吗?
我试图使用界面将图像地址和名称传回主活动,但是当我按下按钮时它会一直崩溃。
我是朝着正确的方向,还是有其他方式从列表视图中分享项目数据?
谢谢!
FeedGridViewAdapter (custom ArrayAdapter):
public class FeedGridViewAdapter extends ArrayAdapter<FeedItemsSetter> {
private Context context;
private int layoutResourceId;
private ArrayList<FeedItemsSetter> data = new ArrayList<FeedItemsSetter>();
private TextView locationName;
private ImageView image;
private ImageView feedUserImage;
private TextView cityName;
private TextView userInfo;
private String[] separatedLikerData;
private FeedItemsSetter item;
public interface facebookShareListener {
public void onSharePressed(String values);
}
facebookShareListener mListener;
public FeedGridViewAdapter(Context context, ArrayList<FeedItemsSetter> data) {
super(context, android.R.id.content, data);
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.feed_grid, null);
item = data.get(position);
TextView[] likers = new TextView[10];
locationName = (TextView) view.findViewById(R.id.locName);
locationName.setText(item.getTitle());
cityName = (TextView) view.findViewById(R.id.cityName);
cityName.setText(item.getCategory());
userInfo = (TextView) view.findViewById(R.id.feedUserName);
userInfo.setText(item.getUser());
feedUserImage = (ImageView) view.findViewById(R.id.feedUserImage);
Picasso.with(context)
.load("https://graph.facebook.com/" + item.getUserId() + "/picture?type=large")
.placeholder(R.drawable.placeholder)
.error(R.drawable.ic_launcher)
.transform(new RoundedTransformation(90, 4))
.resize(100, 100)
.centerCrop()
.into(feedUserImage);
image = (ImageView) view.findViewById(R.id.image);
Picasso.with(context).load("http://www.edmondvarga.com/gastrolove/uploads/" + item.getImage())
.into(image);
separatedLikerData = item.getLiker().split(",");
if(separatedLikerData.length < 5){
LinearLayout layout = (LinearLayout) view.findViewById(R.id.likerNames);
likers[position] = new TextView(context);
likers[position].setText(separatedLikerData[0] + ", ");
likers[position].setTextColor(Color.parseColor("#d52e2e"));
likers[position].setTypeface(null, Typeface.BOLD_ITALIC);
likers[position].setTextSize(13);
layout.addView(likers[position]);
likers[position].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent in= new Intent(context, UserProfile.class);
in.putExtra("user", separatedLikerData[0]);
in.putExtra("userId", separatedLikerData[1]);
context.startActivity(in);
}
});
}
//-----------Iterface implmentation -----------------
Button feedShare = (Button) view.findViewById(R.id.feedShareBtn);
feedShare.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.onSharePressed("image link");
}
});
return view;
}
}
主类:
...
public class OffersFragmentActivity extends Fragment implements
OnItemClickListener, FeedGridViewAdapter.facebookShareListener {
private GridView gridView;
private GridViewAdapter customGridAdapter;
ArrayList<ImageItem> imageItems = new ArrayList<ImageItem>();
View v;
private SQLiteDataSource datasource;
private ListView TasteMakersListView;
private ArrayList<TasteMakers> tasteMakersList = new ArrayList<TasteMakers>();
private TasteMakers member;
private GridView feedGridView;
private String user;
private ArrayList<FeedItemsSetter> feedList = new ArrayList<FeedItemsSetter>();
protected String names;
private UiLifecycleHelper uiHelper;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
super.onCreateView(inflater, container, savedInstanceState);
v = inflater.inflate(R.layout.offers_view, null, false);
SharedPreferences preferences = getActivity().getSharedPreferences(
"USERINFO", Context.MODE_PRIVATE);
user = preferences.getString("name", null);
feedGridView = (GridView) v.findViewById(R.id.feedGridView);
gridView = (GridView) v.findViewById(R.id.gridView);
TasteMakersListView = (ListView) v
.findViewById(R.id.RecomendedUsersList);
feedGridView.setFocusable(false);
gridView.setFocusable(false);
TasteMakersListView.setFocusable(false);
...
@Override
public void onSharePressed(String values) {
System.out.println("Share button pressed!" + values);
}
...
logcat的:
12-12 20:30:54.926: E/AndroidRuntime(9647): FATAL EXCEPTION: main
12-12 20:30:54.926: E/AndroidRuntime(9647): Process: com.fideli, PID: 9647
12-12 20:30:54.926: E/AndroidRuntime(9647): java.lang.NullPointerException
12-12 20:30:54.926: E/AndroidRuntime(9647): at com.fideli.userimages.FeedGridViewAdapter$2.onClick(FeedGridViewAdapter.java:111)
12-12 20:30:54.926: E/AndroidRuntime(9647): at android.view.View.performClick(View.java:4654)
12-12 20:30:54.926: E/AndroidRuntime(9647): at android.view.View$PerformClick.run(View.java:19438)
12-12 20:30:54.926: E/AndroidRuntime(9647): at android.os.Handler.handleCallback(Handler.java:733)
12-12 20:30:54.926: E/AndroidRuntime(9647): at android.os.Handler.dispatchMessage(Handler.java:95)
12-12 20:30:54.926: E/AndroidRuntime(9647): at android.os.Looper.loop(Looper.java:146)
12-12 20:30:54.926: E/AndroidRuntime(9647): at android.app.ActivityThread.main(ActivityThread.java:5602)
12-12 20:30:54.926: E/AndroidRuntime(9647): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 20:30:54.926: E/AndroidRuntime(9647): at java.lang.reflect.Method.invoke(Method.java:515)
12-12 20:30:54.926: E/AndroidRuntime(9647): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-12 20:30:54.926: E/AndroidRuntime(9647): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-12 20:30:54.926: E/AndroidRuntime(9647): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您正在获取NPE,这似乎表明您的mListener变量为空。你真的从你的Activity中设置了监听器吗?
此外,您的ArrayAdapter类有一个Context,如果您从Activity类创建它,它可能是一个Activity,为什么不使用它并将其传递给共享对话框?如果您只有片段,则共享对话框也可以采用片段。