标签: android-fragments

将 Parcelable 对象传递给片段类型不匹配

我是 kotlin 的新手,并试图将一个对象从我的适配器传递给一个片段。但是我在booklist. 它说Required: Parceleable Found: List<ResourcesList>?

我也试过使用putParcelableArrayListandputParcelableArraySerializable但也有相同的类型不匹配。

我的数据模型如下所示:

@Parcelize
class ResourcesList (val id: Int,
                     val name: String,
                     val content: Contents,
                     val tags: List<Tags>) : Parcelable

@Parcelize
class Contents       (val id: Int,
                     val producers: List<Producers>,
                     val categories: List<Categories>,
                     val isAvailable: Boolean): Parcelable
@Parcelize
class Producers     (val name: String,
                     val role: String): Parcelable
@Parcelize
class Categories    (val id: Int,
                     val name: String): Parcelable
Run Code Online (Sandbox Code Playgroud)

分段

class SeeAllFragment: Fragment() {

    override fun onCreateView(inflater: LayoutInflater, …
Run Code Online (Sandbox Code Playgroud)

android parcelable android-fragments kotlin

0
推荐指数
1
解决办法
2477
查看次数

RecyclerViewCalendar 不能为空

我目前正在创建一个自定义对话框来显示从 00:00 到 23:59 的时间,我使用 kotlin 作为开发语言。我的问题是,当我打开对话框时,回收站视图出现错误。这是我的代码

日历对话框类

class CalendarDialog : DialogFragment() {

    /**
     * Define variables
     */
    private val mDaysList : MutableList<Days> = ArrayList()
    private val dayMonthYear = "2018-06-14" //TODO fetch the date of today

    /**
     * Initialize the adapter
     */
    private val adapter = CalendarAdapter(mDaysList)

    /**
     * Initialize the layout manager
     */
    private fun getLinearLayoutManager(): LinearLayoutManager {
        return LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
    }

    private fun initView() {
        setDataListItems()
        recyclerViewCalendar?.adapter = adapter
        recyclerViewCalendar?.layoutManager = getLinearLayoutManager()
        recyclerViewCalendar?.setHasFixedSize(true)
    }

    override fun onCreateView(inflater: …
Run Code Online (Sandbox Code Playgroud)

android android-lifecycle android-fragments kotlin android-recyclerview

0
推荐指数
1
解决办法
1834
查看次数

如何将数组列表从适配器传递给片段?

实际上,我正在从 RecyclerAdapter 在 ArrayList 中添加对象,并且我已经编写了一个用于在适配器中获取数组列表的函数。并且我正在从 MainActivity 中的该函数获取数组列表。但是每当我尝试将该数组列表从 MainActivity 传递到 Fragment 时它给出了 NullPointer(Null 值)。

帮我。

package com.example.codingmounrtain.addtocartbadgecount.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.codingmounrtain.addtocartbadgecount.activity.MainActivity;
import com.example.codingmounrtain.addtocartbadgecount.ModelClasses.Movie;
import com.example.codingmounrtain.addtocartbadgecount.R;
import com.example.codingmounrtain.addtocartbadgecount.interfaces.AddorRemoveCallbacks;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;



public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

    ArrayList<Movie> cartmovies = new ArrayList<>();

    public interface Listener {
        void onSelectMovie(int position);
    }


     Context context;
    private final ArrayList<Movie> movies;
    private final Listener listener;

    public RecyclerAdapter(Context context, ArrayList<Movie> movies,Listener …
Run Code Online (Sandbox Code Playgroud)

java android arraylist android-fragments android-activity

0
推荐指数
1
解决办法
1638
查看次数

调用consumeAsync

我已经在我的应用程序中实现了应用程序内购买,但它们只能在我的 onPurchaseFinished 代码中购买一次,我应该在哪里调用 onConsumeListener?

@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {


    if (responseCode == BillingClient.BillingResponse.OK
            && purchases != null) {
       Toast.makeText(getContext(), "Purchased", Toast.LENGTH_SHORT).show();
        adddata();


        for (Purchase purchase : purchases) {

        }
    } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
        // Handle an error caused by a user cancelling the purchase flow.

    } else if (responseCode == BillingClient.BillingResponse.ITEM_ALREADY_OWNED) {

        Toast.makeText(getContext(), "Purchase Limit Reached! Try again later", Toast.LENGTH_SHORT).show();

    } else{

        // Handle any other error codes.
    }
}
Run Code Online (Sandbox Code Playgroud)

android android-fragments

0
推荐指数
1
解决办法
1565
查看次数

在 PreferenceFragmentCompat 中单击时如何触发事件

我用的是安卓x。在设置片段中,我想创建“首选项”按钮并单击它们以触发一些单独的事件。

如何在特定首选项上实现点击侦听器?

那是我的一些代码:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

        addPreferencesFromResource(R.xml.preference);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = super.onCreateView(inflater, container, savedInstanceState);
        view.setBackgroundColor(getResources().getColor(R.color.graylight));

        Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.action_settings);
        toolbar.setLogo(R.drawable.ic_settings_white_24dp);

        PreferenceManager preferenceManager = getPreferenceManager();
        PreferenceScreen preferenceScreen = getPreferenceScreen();

        return view;
    }

}
Run Code Online (Sandbox Code Playgroud)

和 XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory …
Run Code Online (Sandbox Code Playgroud)

java android listener android-fragments preferencefragment

0
推荐指数
2
解决办法
1726
查看次数

如何在 recyclerview 适配器中为 Activity 扩展上下文

我在活动中的片段内的回收视图的行中有按钮。当用户单击其中一个按钮时,我想切换到不同的活动。如何为 Intent 建立上下文以切换到新活动。

或者,我是否应该将 onClickListener 移回片段或活动(将 onClickListener 放在 recyclerview 适配器代码中是否不正确)。如果是这样,我如何获取对从 recyclerview 适配器外部单击的按钮的引用。

这是片段代码(CheckInRecentList.java)

package com.example.checkingin;
import android.app.Application;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;

import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;



import com.google.android.material.snackbar.Snackbar;

import java.util.List;

import static androidx.constraintlayout.widget.Constraints.TAG;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link …
Run Code Online (Sandbox Code Playgroud)

java android-fragments android-recyclerview

0
推荐指数
1
解决办法
68
查看次数

android中片段的参数化构造函数

我在片段中有以下构造函数:-

public PlaceDialogFragment(Place place, DisplayMetrics dm){
        super();
        this.mPlace = place;
        this.mMetrics = dm;
    }
Run Code Online (Sandbox Code Playgroud)

我也试过这个:-

public static final DialogFragment newInstance(Place place, DisplayMetrics dm)
{
    DialogFragment fragment = new DialogFragment();
    Bundle bundle = new Bundle(2);
    bundle.putParcelable("Place", place);
    bundle.putLong("Metrics", dm);
    fragment.setArguments(bundle);
    return fragment ;
}
Run Code Online (Sandbox Code Playgroud)

但是bundle.putLong("Metrics", dm) 网上有错误

Place是一个实现 Parceable 接口的类

但我收到一条错误消息:-

Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead
Run Code Online (Sandbox Code Playgroud)

任何建议如何解决这个问题?

android android-fragments

-1
推荐指数
1
解决办法
4076
查看次数

在两个片段之间进行通信

我有两个扩展Fragment的类,两个都在同一个Activity中,我想在另一个片段类中按下按钮时在一个片段类中触发一个方法,我知道这可以使用接口或意图来完成。我这样做的代码示例。提前致谢

java android communication android-fragments android-activity

-1
推荐指数
1
解决办法
1068
查看次数

指定的子项已有父项.您必须首先在子级父级上调用removeView()(具有片段和ViewPager的Activity)

我创建这篇文章是因为我无法解决这个问题.我的想法是一个片段活动,在顶部有一个片段,它是一个地图,在底部有一个视图寻呼机,允许用户用信息扫描片段.


主要活动(片段活动)

public class RestaurantDetails extends FragmentActivity {

    private static final String TAG = "ImageGridActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.restaurantinfo);
    inicialize();

            /* Adding fragment in a relativelayout */
    if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
        final FragmentTransaction ft = getSupportFragmentManager()
                .beginTransaction();
        ft.add(R.id.fragment_container, new MapFragment(), TAG);
        ft.commit();
    }

        /* Pager on bottom */
        mPager.setCurrentItem(getIntent().getExtras().getInt("idRest"));
}
Run Code Online (Sandbox Code Playgroud)

布局主要活动

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

      <RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="212dp"
        tools:context=".MapFragment" />


    <android.support.v4.view.ViewPager
        android:id="@+id/pagerDetails"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

地图片段

public class MapFragment extends …
Run Code Online (Sandbox Code Playgroud)

java android android-layout android-fragments android-viewpager

-1
推荐指数
1
解决办法
2415
查看次数

如何仅对数字使用 editText

我想使用带有类型编号的 editText。
如果你使用它,它不起作用:

    Number editText = (Number) v.findViewById(R.id.editText);
Run Code Online (Sandbox Code Playgroud)

错误是在

Number editText = (Number) v.findViewById(R.id.editText) ;

它说:

不可转换的类型;无法投射android.view.Viewjava.lang.Number

解决方案很简单(感谢@Ehsan 提供有用的答案):

Edittext editText = (Edittext) v.findViewById(R.id.editText);

android android-edittext android-fragments

-1
推荐指数
1
解决办法
143
查看次数