【发布时间】:2017-08-02 13:47:13
【问题描述】:
我使用了自定义的ArrayAdapter,但没有调用相同的getView() 方法。
但是,我检查了 getCount() 方法,它返回 value > 0. 2 在我的情况下。
这是我的适配器的代码 sn-p。
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 和 adapter.getCount returns > 0,所以不确定这里发生了什么问题。
这是代码 sn-p。
profilesSwipableAdapter = new ProfilesSwipableAdapter(getActivity(), R.layout.list_item_profile, profileArrayList);
swipeCardView.setAdapter(profilesSwipableAdapter);
真正令我沮丧的是,如果我在 Activity 中遵循相同的步骤,我会得到正确的结果,即适配器的 getView() 被调用。
有什么问题?
【问题讨论】:
-
我认为你的 Adapter 很好(因为你说它在没有 Fragment 的情况下也可以工作),所以我们需要看看你是如何设置 Fragment 的。
-
设置片段没什么好说的,我就是这么称呼它的。
ProfilesFragment profilesFragment = new ProfilesFragment(); FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frame_Content, profilesFragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit();。在 createview 中,view = inflater.inflate(R.layout.fragment_profiles, container, false); mContext = getContext(); swipeCardView=(SwipeCardView)view.findViewById(R.id.card_stack_view); -
如果您仍在寻求帮助,请发布足够的代码,以便我重现问题。然后我会运行你的代码并尝试找出错误。
-
我搞定了。我在填充 ArrayList 后分配适配器。相反,我为适配器分配了空的 ArrayList,当 ArrayList 被填充时,我只是通知了适配器,一切都开始像魅力一样工作。不知道为什么会这样,但它的工作原理!
-
你说得对,这看起来很奇怪。但很高兴听到您已修复它:)
标签: android android-fragments android-arrayadapter android-adapter