Timon Doo
Timon Doo

Reputation: 153

Custom List adapter in Fragment

I am currently trying to implement a custom listView for my Fragment, but when i try to add the item to the ListView it doesn't show up, and i get no exception or anything.

What i'm trying to do is to create an object with a name and a number by typing, both of these things work, but it seems like i did a mistake with the listView

Here is the Fragment with the ListView

package com.example.myapplicationteeeeeeeeeest.ui.BShoppingRecipe;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.example.myapplicationteeeeeeeeeest.R;

import java.util.ArrayList;
import java.util.List;

public class ShoppingListTab extends ShoppingRecipe {

    private EditText inputIngredient;
    private EditText inputQuantity;
    private Button addButton;
    private ListView listView;
    private ShoppingListAdapter adapterListView;
    private ArrayList<ShoppingList> ShoppingList;




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate( R.layout.tab_fragment_one, container,false );
        listView = (ListView) view.findViewById( R.id.shoppingList_listView );
        addButton = (Button) view.findViewById( R.id.shoppingTab_button);
        inputQuantity = (EditText) view.findViewById( R.id.quantity_editText );
        inputIngredient = (EditText) view.findViewById( R.id.ingredient_editText );


        ShoppingList = new ArrayList<>();
        adapterListView = new ShoppingListAdapter( getActivity(),ShoppingList );
        listView.setAdapter( adapterListView );
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                ShoppingList.remove(position);
                adapterListView.notifyDataSetChanged();
                return true;
            }
        });

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ingredient = inputIngredient.getText().toString().trim();
                String quantitiy = inputQuantity.getText().toString().trim();

                if (!ingredient.isEmpty() && !quantitiy.isEmpty()) {
                    ShoppingList ShoppingListItem = new ShoppingList(ingredient, quantitiy);
                    ShoppingList.add(ShoppingListItem);
                    adapterListView.notifyDataSetChanged();
                    inputIngredient.setText("");
                    inputQuantity.setText("");
                }
            }
        });

        return view;
    }


}

The Fragment Layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/ingredient_editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="80dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:ems="10"
        android:hint="@string/shopping_list_hint_artikel"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/quantity_editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:ems="10"
        android:hint="@string/shopping_list_hint_stückzahl"
        android:inputType="number"
        app:layout_constraintEnd_toStartOf="@+id/shoppingTab_button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ingredient_editText"
        app:layout_constraintHorizontal_bias="0.0"/>


    <Button
        android:id="@+id/shoppingTab_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/shopping_list_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/ingredient_editText" />

    <ListView
        android:id="@+id/shoppingList_listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/quantity_editText"/>

</androidx.constraintlayout.widget.ConstraintLayout>

The Costume ListAdapter

package com.example.myapplicationteeeeeeeeeest.ui.BShoppingRecipe;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.myapplicationteeeeeeeeeest.R;

import java.util.ArrayList;

public class ShoppingListAdapter extends ArrayAdapter<ShoppingList> {

    private ArrayList<ShoppingList> shoppingList;
    private Context context;

    public ShoppingListAdapter(Context context, ArrayList<ShoppingList> taskList) {
        super(context, R.layout.shopping_list_item);


        this.shoppingList = taskList;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if(v == null){
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = layoutInflater.inflate(R.layout.shopping_list_item, null);
        }

        ShoppingList taskItem = shoppingList.get(position);

        if(taskItem != null){
            TextView taskItemText = v.findViewById(R.id.shoppingList_Ingredient);
            TextView taskItemDate = v.findViewById(R.id.shoppingList_Quantity);

            taskItemText.setText(taskItem.getTask());
            taskItemDate.setText(taskItem.getNumber());
        }
        return v;
    }
}

And the Layout for the Item

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >


    <TextView
        android:text="TextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/shoppingList_Ingredient"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/shoppingList_Quantity"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"/>
    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/shoppingList_Quantity"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

Upvotes: 0

Views: 52

Answers (1)

Bhoomi
Bhoomi

Reputation: 172

Try adding this method in your adapter class:

    @Override
    public int getCount() {
        return shoppingList.size();
    }

And also try not to use same name for your list variable and model class. Hope this helps!

Upvotes: 1

Related Questions