我目前正在尝试为我的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>
答案 0 :(得分:1)
尝试在适配器类中添加此方法:
@Override
public int getCount() {
return shoppingList.size();
}
也请不要对列表变量和模型类使用相同的名称。 希望这会有所帮助!