我使用了自定义ArrayAdapter
,但未调用getView()
方法。
但是,我已经使用getCount()
方法进行了检查,它返回value > 0. 2
在我的情况下。
这是我的适配器的代码段。
public class ProfilesSwipableAdapter extends ArrayAdapter<SoundProfile> {
private final ArrayList<SoundProfile> cards;
private final LayoutInflater layoutInflater;
private GoogleApiClient mGoogleApiClient;
private Context mContext;
public ProfilesSwipableAdapter(Context context, int resource, ArrayList<SoundProfile> cards) {
super(context, 0, cards);
this.mContext = context;
this.cards = cards;
//this.layoutInflater = LayoutInflater.from(context);
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final SoundProfile card = cards.get(position);
View view = layoutInflater.inflate(R.layout.list_item_profile, parent, false);
Glide.with(mContext)
.load(getMapURLBlack)
.into(((ImageView) view.findViewById(R.id.map_lite)));
((TextView) view.findViewById(R.id.txtProfileName)).setText(card.profileName);
view.findViewById(R.id.imgEdit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final LocationManager manager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
} else {
Intent intent = new Intent(mContext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("name", card.profileName);
intent.putExtra("key", card.mKey);
intent.putExtra("lat", card.latitude);
intent.putExtra("lng", card.longitude);
mContext.startActivity(intent);
}
}
});
view.findViewById(R.id.btnDelete).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(mContext.getResources().getString(R.string.prompt_discard_profile))
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
ArrayList<String> list = new ArrayList<String>();
list.add(card.mKey);
LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, list);
FirebaseDatabase.getInstance().getReference().child("profiles").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child(card.mKey).removeValue();
EventBus.getDefault().post(new ProfileDeletedEvent(position));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
});
return view;
}
public void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
mContext.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
@Override
public SoundProfile getItem(int position) {
return cards.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getCount() {
return cards.size();
}
}
我正在从Fragment初始化此适配器。
我已经检查过,我传递的ArrayList也有items > 0
和&amp; adapter.getCount returns > 0
也是如此,所以不确定这里发生了什么问题。
这是代码片段。
profilesSwipableAdapter = new ProfilesSwipableAdapter(getActivity(), R.layout.list_item_profile, profileArrayList);
swipeCardView.setAdapter(profilesSwipableAdapter);
对我来说真正令人沮丧的是,如果我通过Activity执行相同的步骤,我会得到正确的结果,即调用适配器getView()
。
什么可能是错的?
答案 0 :(得分:0)
/Just try to use below code Snippet/
Put the below code below onCreateView of Fragment
Activity mactivity;
MainActivity main_activity;
Note: MainActivity --> is Activity on which Fragment is Attached.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mactivity = getActivity();
main_activity = (MainActivity) mactivity;
}
And then try to use "main_activity" instead of getActivity() in
below code.
profilesSwipableAdapter = new ProfilesSwipableAdapter(main_activity, R.layout.list_item_profile, profileArrayList);
swipeCardView.setAdapter(profilesSwipableAdapter);