gpt4 book ai didi

java - 为什么来自 AlertDialog 的输入不显示在 RecyclerView 中?

转载 作者:搜寻专家 更新时间:2023-11-01 08:29:38 25 4
gpt4 key购买 nike

我想从 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>

最佳答案

您需要获取 txtExName 的引用和 txtExAmount来自 view 不是rootview .因此,从您的最新代码中删除这些行

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);

关于java - 为什么来自 AlertDialog 的输入不显示在 RecyclerView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41878370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com