我正在用java编写一个android应用程序。
我正在尝试拥有一个项目列表,并且能够删除或添加该列表中的项目。
在我的片段xml中我有一个ListView
<ListView android:id="@+id/ingredients_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2">
</ListView>
我的片段代码:
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列表,然后我重新创建适配器并将新适配器设置为ingredientsListView。
首先......这是要走的路吗?每次创建新元素时,我真的需要重新创建适配器吗?
第二..我总是只看到列表中的第一个元素!所以,如果我添加第一个元素,我会看到该元素。如果我添加另一个元素,我只看到第一个元素。
任何想法?
我所做的改变
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);
}
}
});
好的,这解决了我不需要重新创建适配器的第一个问题。 但我仍然只看到列表中的第一个元素。
我的添加饮品片段,其中包含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>
答案 0 :(得分:2)
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);
}
}
}
});
答案 1 :(得分:0)
致电add()
上的ArrayAdapter
并致电notifyDataSetChanged()
上的ArrayAdapter
。