我想从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>
答案 0 :(得分:2)
好的,我可以看到问题的原因是它无法找到名为EditText
的{{1}}和txtExName
。所以,
切掉这两行:
txtExAmount
并将其替换为:
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
希望它有所帮助。
答案 1 :(得分:1)
您需要从 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);
答案 2 :(得分:0)
你的XML文件在哪里发布了你的EditTexts?
它可能会返回null,因为它找不到任何EditText:
String txtExNa = (txtExAmount.getText()).toString(); //here is crash, i think
对不起我的英语,我希望这有帮助!
答案 3 :(得分:0)
好的,这可能是最终答案。我会尽力掩盖你想要做的一切。
//这是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);
}
}
}
希望这最终解决了这个问题。