Reputation: 32144
I'm writing an android application with java.
I'm trying to have a list of items and to be able to remove or add items to that list.
in my fragment xml I have a ListView
<ListView android:id="@+id/ingredients_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2">
</ListView>
my fragment's code:
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);
}
}
});
so.. whenever the user click on addIngredientButton, if the lengtn of the text is greater then zero, I add it to the listItems list, then I re-create the adapter and set the new adapter to the ingredientsListView.
first of all.. is this the way to go? do I really need to re-create the adapter each time I create a new element ?
second.. i always see only the first element in the list! so if i add the first element I see that element. if I add another element I only see the first element.
any ideas ?
changes that I made
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);
}
}
});
ok that solves my first problem that I don't need to re-create the adapter. but still I only see the first element in the list.
my add drink fragment that includes the 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>
Upvotes: 0
Views: 142
Reputation: 157487
ArrayAdapter has the add
method that does what you need, without the needs to recreate the adapter every time you add something. Here you can find the documentation
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);
}
}
}
});
Upvotes: 2
Reputation: 1360
Call add()
on the ArrayAdapter
and call notifyDataSetChanged()
on the ArrayAdapter
.
Upvotes: 0