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

Posted

技术标签:

【中文标题】片段中的自定义列表适配器【英文标题】:Custom List adapter in Fragment 【发布时间】:2020-01-10 02:31:39 【问题描述】:

我目前正在尝试为我的 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_
    android:layout_>

    <EditText
        android:id="@+id/ingredient_editText"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
    android:layout_
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >


    <TextView
        android:text="TextView"
        android:layout_
        android:layout_
        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_
        android:layout_
        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>

【问题讨论】:

【参考方案1】:

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

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

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

【讨论】:

我还需要将项目的布局更改为 但后来它工作了,非常感谢您的帮助

以上是关于片段中的自定义列表适配器的主要内容,如果未能解决你的问题,请参考以下文章

从自定义适配器获取片段中的 UI 元素 ID

片段中 ListView 的自定义适配器不起作用

如何将最新数据附加到android中的自定义基本适配器列表视图?

如何使用 glide* 使用数组列表中的自定义适配器将图像设置为列表视图

片段中ListView的android自定义适配器

ArrayIndexOutOfBoundsException 与 ListView 中的多个视图的自定义 Android 适配器