我正在用 java 编写一个 Android 应用程序。
我正在尝试获得一个项目列表,并能够在该列表中删除或添加项目。
在我的 fragment xml 中,我有一个 ListView
<ListView android:id="@+id/ingredients_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2">
</ListView>
我的 fragment 的代码:
Button galleryButton,cameraButton, addIngredientButton, drinkCompleteButton;
EditText ingredientEditText;
ListView ingredientsListView;
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_add_dink, container, false);
ingredientEditText = (EditText) rootView.findViewById(R.id.ingredients_edit);
ingredientsListView = (ListView) rootView.findViewById(R.id.ingredients_listview);
adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems);
ingredientsListView.setAdapter(adapter);
addIngredientButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(),"here",Toast.LENGTH_LONG).show();
final String ingredient = ingredientEditText.getText().toString().trim();
if (ingredient.length() == 0) {
Toast.makeText(getActivity().getApplicationContext(),"ingredient is empty",Toast.LENGTH_LONG).show();
} else {
listItems.add(ingredient);
adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems);
ingredientsListView.setAdapter(adapter);
}
}
});
所以..每当用户单击addIngredientButton时,如果文本的长度大于零,我会将其添加到listItems列表中,然后重新创建适配器并将新适配器设置为成分ListView。
首先..这是要走的路吗?每次创建新元素时我真的需要重新创建适配器吗?
第二..我总是只看到列表中的第一个元素!因此,如果我添加第一个元素,我就会看到该元素。如果我添加另一个元素,我只会看到第一个元素。
有什么想法吗?
更新
我所做的更改
addIngredientButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(),"here",Toast.LENGTH_LONG).show();
final String ingredient = ingredientEditText.getText().toString().trim();
if (TextUtils.isEmpty(ingredient)) {
Toast.makeText(getActivity().getApplicationContext(),"ingredient is empty",Toast.LENGTH_LONG).show();
} else {
adapter.add(ingredient);
}
}
});
好的,这解决了我的第一个问题,我不需要重新创建适配器。 但我仍然只看到列表中的第一个元素。
更新
我的添加饮料 fragment ,其中包括 ListView 'ingredients_listview'
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="50dp"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:id="@+id/drink_brand_textview"
android:labelFor="@+id/drink_brand_edit"
android:text="@string/drink_brand"
/>
<EditText android:id="@+id/drink_brand_edit"
android:inputType="textAutoComplete" />
</TableRow>
<TableRow>
<TextView android:id="@+id/drink_type_textview"
android:labelFor="@+id/drink_type_edit"
android:text="@string/drink_type"
/>
<EditText android:id="@+id/drink_type_edit"
android:inputType="textAutoComplete" />
</TableRow>
<TableRow>
<TextView android:id="@+id/drink_company_textview"
android:labelFor="@+id/drink_company_edit"
android:text="@string/drink_company"
/>
<EditText android:id="@+id/drink_company_edit"
android:inputType="textAutoComplete" />
</TableRow>
<TableRow>
<TextView android:id="@+id/drink_flavor_type_textview"
android:labelFor="@+id/drink_flavor_type_edit"
android:text="@string/drink_flavor_type"
/>
<EditText android:id="@+id/drink_flavor_type_edit"
android:inputType="textAutoComplete" />
</TableRow>
<TableRow>
<TextView android:id="@+id/liquid_color_textview"
android:labelFor="@+id/liquid_color_edit"
android:text="@string/liquid_color"
/>
<EditText android:id="@+id/liquid_color_edit"
android:inputType="textAutoComplete" />
</TableRow>
<TableRow>
<TextView android:id="@+id/liquid_color_is_transparent_textview"
android:text="@string/liquid_color_is_transparent"
/>
<RadioGroup>
<RadioButton
android:id="@+id/is_transparent_yes_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yes"
android:checked="true"/>
<RadioButton
android:id="@+id/is_transparent_no_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no"/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView android:id="@+id/calories_for_100g_textview"
android:labelFor="@+id/calories_for_100g_edit"
android:text="@string/calories_for_100g"
/>
<EditText android:id="@+id/calories_for_100g_edit"
android:inputType="number" />
</TableRow>
<TableRow>
<TextView android:id="@+id/alcohol_volume_textview"
android:labelFor="@+id/alcohol_volume_edit"
android:text="@string/alcohol_volume"
/>
<EditText android:id="@+id/alcohol_volume_edit"
android:inputType="number" />
</TableRow>
<TableRow>
<TextView android:id="@+id/drink_image_textview"
android:text="@string/drink_image"/>
<ImageView android:id="@+id/drink_imageview"/>
</TableRow>
<TableRow>
<Button android:id="@+id/add_drink_image_from_gallery_button"
android:text="@string/gallery"
/>
<Button android:id="@+id/add_drink_image_from_camera_button"
android:text="@string/camera"
/>
</TableRow>
<TableRow>
<TextView android:id="@+id/ingredients_textview"
android:text="@string/ingredients"/>
<EditText android:id="@+id/ingredients_edit"/>
</TableRow>
<TableRow>
<Button android:id="@+id/add_ingredient_button"
android:text="@string/add_ingredient"/>
</TableRow>
<TableRow>
<ListView android:id="@+id/ingredients_listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_span="2">
</ListView>
</TableRow>
<TableRow>
<Button android:id="@+id/drink_complete_button"
android:text="@string/complete"/>
</TableRow>
</TableLayout>
</ScrollView>
</RelativeLayout>
最佳答案
ArrayAdapter 有 add
方法可以满足您的需要,而无需在每次添加内容时重新创建适配器。 Here你可以找到文档
addIngredientButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(),"here",Toast.LENGTH_LONG).show();
final String ingredient = ingredientEditText.getText().toString().trim();
if (!TextUtils.isEmpty(ingredient)) {
adapter.add(ingredient);
}
}
}
});
关于java - 使用 ArrayAdapter 将项目添加到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23723976/