Fragment 中的自定义列表适配器

丁满都

我目前正在尝试为我的 Fragment 实现自定义 listView,但是当我尝试将项目添加到 ListView 时,它没有显示出来,而且我没有任何异常或任何异常。

我想要做的是通过键入创建一个带有名称和数字的对象,这两件事都可以工作,但似乎我在 listView 上犯了一个错误

这是带有 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;
    }


}

片段布局

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

服装列表适配器

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

和项目的布局

<?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>
博米

尝试在您的适配器类中添加此方法:

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

并且尽量不要为您的列表变量和模型类使用相同的名称。希望这可以帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

片段中的自定义阵列适配器

来自分类Dev

获取自定义适配器中的项目列表

来自分类Dev

从适配器调用Fragment方法

来自分类Dev

具有自定义适配器的listview中的android更改列表项

来自分类Dev

在自定义可扩展列表适配器中无法充实视图

来自分类Dev

在RecyclerView适配器中设置自定义字体

来自分类Dev

如何使用自定义数组适配器删除自定义列表中的特定列表项(列表行)?

来自分类Dev

如何在Android中自定义适配器?

来自分类Dev

自定义适配器中的setOnItemClickListener

来自分类Dev

自定义视图的适配器中的NullPointerException

来自分类Dev

从Fragment设置适配器时,CustomListAdapter中的NullPointerException

来自分类Dev

自定义适配器中的空指针异常

来自分类Dev

在自定义适配器中找不到ViewByID

来自分类Dev

自定义旋转适配器显示空列表

来自分类Dev

自定义ListView适配器中的NullPointerException

来自分类Dev

具有基本适配器列表项的自定义适配器重复

来自分类Dev

自定义适配器的getview中的NullPointerException

来自分类Dev

getView()中的自定义适配器错误

来自分类Dev

在自定义适配器中遍历ListView

来自分类Dev

Android:从viewPager中的Fragment调用ListView的适配器的getview()

来自分类Dev

如何使用自定义数组适配器删除自定义列表中的特定列表项(列表行)?

来自分类Dev

自定义绑定适配器中的通用lambda

来自分类Dev

自定义适配器不显示列表视图

来自分类Dev

自定义适配器getView中的IndexOutOfBounds异常

来自分类Dev

无法更新自定义适配器中的项目

来自分类Dev

使用自定义 ListView 适配器刷新 Fragment ListView

来自分类Dev

画廊的自定义适配器中的 AsyncTask

来自分类Dev

自定义列表适配器中的 getApplicationContext()

来自分类Dev

自定义适配器中的意图

Related 相关文章

  1. 1

    片段中的自定义阵列适配器

  2. 2

    获取自定义适配器中的项目列表

  3. 3

    从适配器调用Fragment方法

  4. 4

    具有自定义适配器的listview中的android更改列表项

  5. 5

    在自定义可扩展列表适配器中无法充实视图

  6. 6

    在RecyclerView适配器中设置自定义字体

  7. 7

    如何使用自定义数组适配器删除自定义列表中的特定列表项(列表行)?

  8. 8

    如何在Android中自定义适配器?

  9. 9

    自定义适配器中的setOnItemClickListener

  10. 10

    自定义视图的适配器中的NullPointerException

  11. 11

    从Fragment设置适配器时,CustomListAdapter中的NullPointerException

  12. 12

    自定义适配器中的空指针异常

  13. 13

    在自定义适配器中找不到ViewByID

  14. 14

    自定义旋转适配器显示空列表

  15. 15

    自定义ListView适配器中的NullPointerException

  16. 16

    具有基本适配器列表项的自定义适配器重复

  17. 17

    自定义适配器的getview中的NullPointerException

  18. 18

    getView()中的自定义适配器错误

  19. 19

    在自定义适配器中遍历ListView

  20. 20

    Android:从viewPager中的Fragment调用ListView的适配器的getview()

  21. 21

    如何使用自定义数组适配器删除自定义列表中的特定列表项(列表行)?

  22. 22

    自定义绑定适配器中的通用lambda

  23. 23

    自定义适配器不显示列表视图

  24. 24

    自定义适配器getView中的IndexOutOfBounds异常

  25. 25

    无法更新自定义适配器中的项目

  26. 26

    使用自定义 ListView 适配器刷新 Fragment ListView

  27. 27

    画廊的自定义适配器中的 AsyncTask

  28. 28

    自定义列表适配器中的 getApplicationContext()

  29. 29

    自定义适配器中的意图

热门标签

归档