繁体   English   中英

Android Fragment Recyclerview 添加带有对话框的项目

[英]Android Fragment Recyclerview add item with dialog

我有一个片段,其中包含带有网格布局的 recyclerview。

问题:当我在对话框中单击按钮(확인)时,应用程序关闭..带有提到的按钮的对话框

我想用对话框按钮在recyclerview中添加项目,我混淆了我在哪里放置代码添加项目

item_list.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="check"/>
    
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_charater"
                android:scrollbars="vertical"
                android:scrollbarSize="5dp"
                android:scrollbarFadeDuration="0"
                android:scrollbarThumbVertical="@android:color/darker_gray"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
                >
    
            </androidx.recyclerview.widget.RecyclerView>
        
        <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/flt_addcharacter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right"
                android:backgroundTint="@color/grey"
                android:background="@color/grey"
                android:layout_margin="16dp"
                android:src="@drawable/add_24_white" />


</FrameLayout>

fragment_checkhome.xml

 <?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="check"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_charater"
            android:scrollbars="vertical"
            android:scrollbarSize="5dp"
            android:scrollbarFadeDuration="0"
            android:scrollbarThumbVertical="@android:color/darker_gray"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            >

        </androidx.recyclerview.widget.RecyclerView>
    
    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/flt_addcharacter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:backgroundTint="@color/grey"
            android:background="@color/grey"
            android:layout_margin="16dp"
            android:src="@drawable/add_24_white" />



</FrameLayout>

fab_item.xml

 <?xml version="1.0" encoding="utf-8"?>
<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"
    >
<!--dialog fagment! -->
<FrameLayout
    android:id="@+id/dialog"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:gravity="center"
        android:orientation="vertical"
        android:background="@color/grey"
        android:padding="24dp">

        <EditText
            android:id="@+id/et_name"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:layout_marginTop="30dp"
            android:hint="이름"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="bottom"
            android:gravity="right">

            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="취소"
                android:textColor="@color/white"
                android:background="#00ff0000"
                android:textSize="18dp"
                android:layout_marginTop="30dp"
                android:layout_marginRight="10dp"
                />
            <Button
                android:id="@+id/btn_confi"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="확인"
                android:textColor="@color/white"
                android:background="#00ff0000"
                android:textSize="18dp"
                android:layout_marginTop="30dp"
                android:layout_marginRight="10dp"
                />
        </LinearLayout>
    </LinearLayout>



</FrameLayout>



</LinearLayout>

Fragment_check.java

 package com.example.lostarkcheck;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;


public class Fragment_check extends Fragment {

    FloatingActionButton fab;
    private ArrayList<Maindata> arrayList;
    private MainAdapter mainAdapter;
    private RecyclerView recyclerView;
    private GridLayoutManager gridLayoutManager;
    DialogFragment dialogAdd;
    private Maindata maindata;
    EditText editText;

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @org.jetbrains.annotations.NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {

        View frag_check = inflater.inflate(R.layout.fragment_checkhome, container, false);
        fab =(FloatingActionButton)frag_check.findViewById(R.id.flt_addcharacter);
        arrayList = new ArrayList<>();

        recyclerView = (RecyclerView)frag_check.findViewById(R.id.rv_charater);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(mainAdapter);
        mainAdapter = new MainAdapter(arrayList);

        dialogAdd = new DialogAdd();
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogAdd.show(getFragmentManager(),"addchracter");
            }
        });

        return frag_check;
    }

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public void additem(Maindata data) {
        arrayList.add(data);
        mainAdapter.notifyDataSetChanged();
    }
}

MainAdapter.java

 package com.example.lostarkcheck;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    public ArrayList<Maindata> arrayList = new ArrayList<Maindata>();

    public MainAdapter(ArrayList<Maindata> arrayList) {
        this.arrayList = arrayList;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView loa_name;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            this.loa_name = (TextView)itemView.findViewById(R.id.loa_name);
        }
    }
    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
        ViewHolder holder = new ViewHolder(v);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MainAdapter.ViewHolder holder, int position) {
        holder.loa_name.setText(arrayList.get(position).getLoa_name());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String curName = holder.loa_name.getText().toString();
                Toast.makeText(v.getContext(), curName, Toast.LENGTH_SHORT).show();
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                remove(holder.getAdapterPosition());
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {

        return (arrayList !=null ? arrayList.size() : 0);
    }

    public void remove(int position){
        try{
            arrayList.remove(position);
            notifyItemRemoved(position);
        }catch (IndexOutOfBoundsException ex){
            ex.printStackTrace();
        }

    }



}

DialogAdd.java

package com.example.lostarkcheck;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Instrumentation;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class DialogAdd extends DialogFragment{

    EditText editText;
    ArrayList<Maindata> arrayList;
    private MainAdapter mainAdapter;
    private Fragment_check fragmentCheck;
    private Maindata maindata;
    private View v;
    private Button btn_confi;
    private Button btn_cancel;

    @Nullable
    @org.jetbrains.annotations.Nullable

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        v = getLayoutInflater().inflate(R.layout.fab_itme, null);
        editText = v.findViewById(R.id.et_name);
        arrayList = new ArrayList<>();
        mainAdapter = new MainAdapter(arrayList);
        btn_confi = (Button)v.findViewById(R.id.btn_confi);
        btn_cancel = (Button)v.findViewById(R.id.btn_cancel);
        fragmentCheck = new Fragment_check();
        maindata = new Maindata(editText.getText().toString());

        btn_confi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragmentCheck.additem(maindata);
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @NonNull
    @NotNull
    @Override
    public Dialog onCreateDialog(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();

        builder.setView(v);

        
        return builder.create();
    }



}

请帮我解决这个问题

Fragment_check的目的是什么?

您的代码中的主要问题是您在DialogAddonCreate上创建了一个带有new关键字的片段。 片段不应该以这种方式创建,它们应该由片段管理器管理并托管在 Activity 中。 当您以这种方式创建片段时,它的onCreateView方法将永远不会被调用,因为它不是托管片段,它与您的应用程序的视图树分离。 因此,如果从未调用过onCreateView ,则它的arrayList永远不会被实例化,并且当调用additem时,它仍然是 null 从 logcat 生成堆栈跟踪的 NullPointerException。

我认为您应该尝试更好地理解什么是片段以及如何创建片段。 更好的是,你真的需要一个片段来做你想做的事吗?

你真正想做什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM