2017-01-28 36 views
3

Ich habe derzeit Probleme mit dem Hinzufügen einer zweiten + Elemente zu einem RecyclerView nach Eingabe von Daten in einem alertdialog Feld.Item zu RecyclerView hinzufügen von AlertDialog Eingabe

Ich kann 1 Datensatz eingeben, aber wenn ich versuche, mehr hinzuzufügen, tut es nichts.

Dies ist meine Java-Datei für das Fragment mit arbeite ich:

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

und die Java-Datei für den Adapter:

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

Die XML die recyclerview Listenelemente zu formatieren:

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

Und das ist die XML für das Fragment:

<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 how im inputting information This is what I currently see AFTER adding a 2nd input

Dies ist, was ich derzeit nach dem Hinzufügen eines zweiten Eingang

+0

Es ist seltsam. Vielleicht habe ich etwas nicht bemerkt, aber dieser Code muss funktionieren. Ist es ein vollständiger Adaptercode? –

+0

Ich glaube es ist – Corpse

+0

Bitte, überprüfen Sie, dass es nicht nur leere Elemente zum 'RecyclerView' (ohne Text) hinzufügt. –

Antwort

0

Ich löste mein Problem. Es hatte mit der Höhe des relativen Layouts für die Recycler-Artikel zu tun. Es war auf Übereinstimmung Elternteil, ich wechselte zum Inhalt zu wickeln und es zeigt jetzt die Eingaben auf dem Bildschirm

+0

Also, es hat nur eine leere Elemente hinzugefügt (es ist mehr wahr, unsichtbare Elemente) =) –

1

Sie können nicht so sehen: jetzt

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

Ihre RecyclerView hat 0 Höhe. Sie müssen eine bestimmte feste Höhe hinzufügen und ein Bildlaufverhalten festlegen. Oder Sie können NestedScrollView verwenden oder LayoutManager mit der vollen Höhenausdehnung schreiben. Weitere Informationen finden Sie unter this thread.

ADDED
Verwenden Sie diese XML statt Ihrer, aber ich es ohne IDE geschrieben, so dass es einige Fehler enthalten kann.

<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> 
+0

Ich habe das getan. Ich setze die Höhe auf 800dp und übergeordnetes und verwendetes NestedScrollView und kann immer noch nur 1 Element bekommen. alle Artikel danach erscheinen nicht – Corpse

+0

Bitte eine neue Variante veröffentlichen. Und Sie verwenden falsche XML-Tags für Innenansichten, diese Tags führen in diesem Kontext nichts aus. –

+0

Und was ist für Sie verwenden diese 'ScrollView'. Hast du da noch Ansichten? Veröffentliche sie auch. –

Verwandte Themen