EXP NO:4 ADAPTERS
DATE:
[Link] a ListView that supports multiple selections using a custom BaseAdapter. Allow
users to select multiple items and display the selected items’ names in a TextView when a
Button is clicked.
AIM:
To create a ListView that supports multiple selections using a custom baseadapter allow
users to select multiple items and display the selected items’ names in a textview when a
Button is clicked.
ALGORITHM:
STEP1:Retrieve the text from the EditText when the "Add Item" button is clicked.
STEP2:Check that the text is not empty.
STEP3:Add the text to a list.
STEP4:Update the ListView adapter with the new list.
STEP5:Refresh the ListView to display the updated items.
STEP6:Clear the EditText after adding the item.
STEP7:Handle any edge cases, such as empty inputs or duplicates.
STEP8:Delete the items you wish you want to delete
STEP9:The item that you clicked will disappear once you click the item.
XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/edit_text_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter item" />
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:layout_below="@id/edit_text_item"
android:layout_marginTop="16dp" />
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@id/add_button"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:dividerHeight="1dp"
android:divider="@android:color/darker_gray" />
</RelativeLayout>
JAVAFILE:
package [Link].exp4;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private ListView listView;
private EditText editTextItem;
private Button addButton;
private MyAdapter adapter;
private List<String> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Find views by ID
listView = findViewById([Link].list_view);
editTextItem = findViewById([Link].edit_text_item);
addButton = findViewById([Link].add_button);
// Initialize items list and adapter
items = new ArrayList<>();
adapter = new MyAdapter(items);
[Link](adapter);
// Set up add button click listener
[Link](new [Link]() {
@Override
public void onClick(View v) {
String newItem = [Link]().toString().trim();
if (![Link]()) {
[Link](newItem);
[Link](); // Notify the adapter about the data change
[Link](""); // Clear the input field
// Scroll to the last item
[Link](new Runnable() {
@Override
public void run() {
[Link]([Link]() - 1);
}
});
}
}
});
// Set up item click listener to remove items
[Link](new [Link]() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
[Link](position);
[Link](); // Notify the adapter about the data change
}
});
}
private class MyAdapter extends BaseAdapter {
private List<String> items;
private LayoutInflater inflater;
public MyAdapter(List<String> items) {
[Link] = items;
[Link] = [Link]([Link]);
}
@Override
public int getCount() {
return [Link]();
}
@Override
public Object getItem(int position) {
return [Link](position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = [Link]([Link].simple_list_item_1, parent, false);
}
TextView textView = [Link]([Link].text1);
[Link]([Link](position));
return convertView;
}
}
}
OUTPUT:
Observation(25)
Record(5)
Total(30)
Initial
RESULT:
To create a ListView that supports multiple selections using a custom BaseAdapter that
allows you to add items and select items is implemented.