简体   繁体   English

从AlertDialog输入向RecyclerView添加项目

[英]Add item to RecyclerView from AlertDialog input

I'm currently having trouble with adding a second+ items to a RecyclerView after inputting data in an alertdialog box. 在alertdialog框中输入数据后,我现在无法在RecyclerView添加第二个+项目。

I can enter 1 set of data but when I try to add more, it doesn't do anything. 我可以输入1组数据但是当我尝试添加更多数据时,它什么也没做。

This is my Java file for the fragment i'm working with: 这是我正在使用的片段的Java文件:

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

    public void expenseData() {
        String Na = txtExName.getText().toString();
        String Am = txtExAmount.getText().toString();

        ExRow exs = new ExRow(Na, Am);
        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);
        final RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        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;
    }
}

And the Java file for the adapter: 以及适配器的Java文件:

public class ExpensesAdapter extends RecyclerView.Adapter<ExpensesAdapter.MyViewHolder> {
    private List<ExRow> expenseList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, amount;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.name);
            amount = (TextView) view.findViewById(R.id.amount);
        }
    }

    public ExpensesAdapter(List<ExRow> expenseList) {
        this.expenseList = expenseList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.expense_list, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ExRow expense = expenseList.get(position);
        holder.title.setText(expense.getTitle());
        holder.amount.setText(expense.getAmount());
    }

    @Override
    public int getItemCount() {
        return expenseList.size();
    }
}

The XML to format the recyclerview list items: 用于格式化recyclerview列表项的XML:

<RelativeLayout 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/name"
    android:textColor="@color/title"
    android:textSize="16dp"
    android:textStyle="bold"
    android:layout_alignParentLeft="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/amount"
    android:textColor="#000000"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_height="wrap_content" />
</RelativeLayout>

And this is the XML for the fragment: 这是片段的XML:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ojemz.expensetracker.tab1Expenses"
android:background="@android:color/darker_gray">

<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" />

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


          <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_marginBottom="17dp">

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

        </android.support.v4.widget.NestedScrollView>
    </LinearLayout>

这是我输入信息的方式 这是我目前在添加第二个输入后看到的内容

This is what I currently see AFTER adding a 2nd input 这是我目前在添加第二个输入后看到的内容

You can't do so: 你不能这样做:

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

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

</ScrollView>

Your RecyclerView has 0 height now. 您的RecyclerView现在高度为0。 You need to add a some fixed height to it and set a scroll behaviour. 您需要为其添加一些固定高度并设置滚动行为。 Or you can use NestedScrollView or write LayoutManager with the full height expansion. 或者您可以使用NestedScrollView或使用完全高度扩展编写LayoutManager See this thread for details. 有关详情,请参阅此主题

ADDED 添加
Use this XML instead of your but I wrote it without IDE so it can contain some errors. 使用这个XML而不是你的但我没有IDE就写了它,所以它可能包含一些错误。

<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ojemz.expensetracker.tab1Expenses"
    android:background="@android:color/darker_gray">

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

        <Button
            android:text="Add Expense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnEx"
            android:layout_gravity="bottom|center_horizontal"
            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" />
</FrameLayout>

I solved my problem. 我解决了我的问题。 It was to do with the height of the relative layout for the recycler items. 这与回收物品的相对布局的高度有关。 It was on match parent, i switched to wrap content and it now displays the inputs on the screen 它在匹配父级,我切换到包装内容,它现在显示屏幕上的输入

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

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