繁体   English   中英

为什么来自AlertDialog的输入未显示在RecyclerView中?

[英]Why does Input from an AlertDialog not show in RecyclerView?

我想从AlertDialog框中获取输入并将其设置为2个变量,然后将这些变量用作自变量以使用recyclerview创建列表。

代码处于这种状态时,它会弹出对话框,当我输入信息并按“添加”时,屏幕上没有任何显示。

这是我的Java文件:

public class tab1Expenses extends Fragment {
List<ExRow> expenseList = new ArrayList<>();
RecyclerView recyclerView;
ExpensesAdapter mAdapter;
Button btnEx;
EditText txtExName;
EditText txtExAmount;

public void expenseData() {

    ExRow exs = new ExRow(txtExNa, txtExAm);
    expenseList.add(exs);

    mAdapter.notifyDataSetChanged();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.tab1expense, container, false);
    btnEx = (Button) rootView.findViewById(R.id.btnEx);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

    mAdapter = new ExpensesAdapter(expenseList);
    RecyclerView.LayoutManager mLayoutManager = new RecyclerView.LayoutManager() {
        @Override
        public RecyclerView.LayoutParams generateDefaultLayoutParams() {

            return null;
        }

    };
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);

    btnEx.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            View view = LayoutInflater.from(tab1Expenses.this.getActivity())
                    .inflate(R.layout.add_ex, null);
            txtExName = (EditText) view.findViewById(R.id.exName);
            txtExAmount = (EditText) view.findViewById(R.id.exAmount);
            AlertDialog.Builder add = new AlertDialog.Builder(tab1Expenses.this.getActivity());
                    add.setCancelable(true)
                    .setTitle("Enter Expense:")
                    .setView(view)
                    .setPositiveButton("Add",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    expenseData();
                                }
                            });
            Dialog dialog = add.create();
            dialog.show();
        }
    });

    return rootView;
   }
}

这是相关的XML文件:

<Button
    android:text="Add Expense"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnEx"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:width="800dp"
    android:textAppearance="@style/TextAppearance.AppCompat.Widget.Switch"
    android:background="@android:color/black"
    android:textColor="@android:color/holo_green_light"
    android:textColorLink="@android:color/holo_green_light" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="17dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />

    </LinearLayout>

</ScrollView>

以及用于添加输入的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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/exName"
        android:textColorLink="@android:color/holo_green_light"
        android:textColorHighlight="@android:color/holo_green_light"
        android:editable="false"
        android:hint="Expense Name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/exAmount"
        android:textColorLink="@android:color/holo_green_light"
        android:textColorHighlight="@android:color/holo_green_light"
        android:hint="Expense Amount" />

</LinearLayout>

好的,所以我可以看到问题是由于它找不到名为txtExNametxtExAmount EditText 所以,

剪下这两行:

recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
txtExName = (EditText) rootView.findViewById(R.id.exName); //<---This one
txtExAmount = (EditText) rootView.findViewById(R.id.exAmount); // <--And this one

并将它们替换为:

View view = LayoutInflater.from(tab1Expenses.this.getActivity())
                .inflate(R.layout.add_ex, null);
txtExName = (EditText) view.findViewById(R.id.exName);
txtExAmount = (EditText) view.findViewById(R.id.exAmount);

希望能帮助到你。

您需要从view而不是rootview获取txtExNametxtExAmount的引用。 因此,从您的最新代码中删除这些行

txtExName = (EditText) rootView.findViewById(R.id.exName); 
txtExAmount = (EditText) rootView.findViewById(R.id.exAmount); 

并在同一位置添加这些行。

txtExName = (EditText) view.findViewById(R.id.exName); 
txtExAmount = (EditText) view.findViewById(R.id.exAmount); 

XML文件中您的EditTexts发布在哪里?

可能因为没有找到任何EditText而返回null:

String txtExNa = (txtExAmount.getText()).toString(); //here is crash, i think

对不起,我的英语,希望对您有所帮助!

好的,所以这可能是最终答案。 我将尝试掩盖您要执行的所有操作。

//这是tab1expense.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:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<Button
    android:text="Add Expense"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnEx"
    android:textAppearance="@style/TextAppearance.AppCompat.Widget.Switch"
    android:background="@android:color/black"
    android:textColor="@android:color/holo_green_light"
    android:textColorLink="@android:color/holo_green_light" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</LinearLayout>

//这是add_ex.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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/exName"
        android:textColorLink="@android:color/holo_green_light"
        android:textColorHighlight="@android:color/holo_green_light"
        android:editable="false"
        android:hint="Expense Name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/exAmount"
        android:textColorLink="@android:color/holo_green_light"
        android:textColorHighlight="@android:color/holo_green_light"
        android:hint="Expense Amount" />

</LinearLayout>

//现在有一个名为ExRow.java的类

public class ExRow{

    String str_txtExNa,str_txtExAm;

    ExRow(String str_txtExNa, String str_txtExAm){
        this.str_txtExNa=str_txtExNa;
        this.str_txtExAm=str_txtExAm;
    }

    public String getNa()
    {
       return str_txtExNa;
    }

    public String getAm()
    {
      return str_txtExAm;
    }
}

//现在您的tab1Expenses.java

public class tab1Expenses extends Fragment {

    ArrayList<ExRow> expenseList = new ArrayList<>();
    RecyclerView recyclerView;
    ExpensesAdapter mAdapter;
    Button btnEx;
    EditText txtExName;
    EditText txtExAmount;

    public void expenseData(String txtExNa,String txtExAm) {
        ExRow exs = new ExRow(txtExNa, txtExAm);
        expenseList.add(exs);
        mAdapter.notifyDataSetChanged();
    }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.tab1expense, container, false);
    btnEx = (Button) rootView.findViewById(R.id.btnEx);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

    mAdapter = new ExpensesAdapter(expenseList);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);

    btnEx.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            View view = LayoutInflater.from(tab1Expenses.this.getActivity())
                    .inflate(R.layout.add_ex, null);
            txtExName = (EditText) view.findViewById(R.id.exName);
            txtExAmount = (EditText) view.findViewById(R.id.exAmount);
            AlertDialog.Builder add = new AlertDialog.Builder(tab1Expenses.this.getActivity());
                    add.setCancelable(true)
                    .setTitle("Enter Expense:")
                    .setView(view)
                    .setPositiveButton("Add",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    expenseData(txtExName.getText().toString(),txtExAmount`enter code here`.getText().toString() );
                                }
                            });
            Dialog dialog = add.create();
            dialog.show();
        }
    });

    return rootView;
   }
}

// row_item_view.xml (RecyclerView行视图的布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/row_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"/>

    <TextView
        android:id="@+id/row_amount"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"/>
</LinearLayout>

//最后是适配器类,即ExpensesAdapter.java

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

    private ArrayList<ExRow> expenseList;

    // Constructor of the class
    public ExpensesAdapter(ArrayList<ExRow> expenseList) {
        this.expenseList = expenseList;
    }

    // get the size of the list
    @Override
    public int getItemCount() {
        return expenseList == null ? 0 : expenseList.size();
    }


    // specify the row layout file
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_view, parent, false);
        ViewHolder myViewHolder = new ViewHolder(view);
        return myViewHolder;
    }

    // load data in each row element
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int listPosition) {
        holder.row_name.setText(expenseList.get(listPosition).getNa());
        holder.row_amount.setText(expenseList.get(listPosition).getAm());
    }

    // Static inner class to initialize the views of rows
    static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView row_name,row_amount;
        public ViewHolder(View itemView) {
            super(itemView);
            row_name = (TextView) itemView.findViewById(R.id.row_name);
            row_amount = (TextView) itemView.findViewById(R.id.row_amount);
        }
    }
}

希望这最终能解决问题。

暂无
暂无

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

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