getView方法没有调用,ArrayAdapter里面片段
我已经使用自定义ArrayAdapter
但getView()
方法没有被调用。 但是,我检查了getCount()
方法,它返回value > 0. 2
在我的情况。 这是我的适配器的代码片段。getView方法没有调用,ArrayAdapter里面片段
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();
}
}
我从初始化片段这个适配器。 我已经检查过,我通过的ArrayList也有items > 0
& adapter.getCount returns > 0
以及不知道这里发生了什么错误。 这是这段代码片段。
profilesSwipableAdapter = new ProfilesSwipableAdapter(getActivity(), R.layout.list_item_profile, profileArrayList);
swipeCardView.setAdapter(profilesSwipableAdapter);
什么是真正令人沮丧的我来说,如果我跟进活动相同的步骤,我得到正确的结果,即适配器getView()
获取调用。 什么可能是错的?
/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);
没有工作,有'main_activity =(MainActivity)mactivity;'''ClassCastException“。 –
注意:MainActivity - >是附有片段的活动。 你有没有使用你的活动只附加了哪个片段 – yash786
尝试但没有运气。 getView仍然没有被调用。 –
我觉得你的适配器是好的(因为你说的它的工作没有片段),所以我们需要看你如何设置的片段。 – 0X0nosugar
没有什么奇怪的设置片段,这就是我所说的。 '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);' –
如果您仍在寻求帮助,请张贴足够的代码,以便重现此问题。然后,我将运行您的代码并尝试查找错误。 – 0X0nosugar