Android Adapter क्या है? | Types of Adapter with Example (Hindi)
Adapter Android में एक bridge है जो data source (जैसे Array, List, Database) को UI component (जैसे ListView, RecyclerView) से connect करता है। यह data को UI में show करने का काम करता है।
Adapter क्यों use होता है?
- Dynamic data UI में show करने के लिए।
- Large data sets को efficiently manage करने के लिए।
- UI और data के बीच communication के लिए।
Adapter के Types
- ArrayAdapter → जब data simple array या list में हो।
- BaseAdapter → Custom layouts और complex data के लिए।
- RecyclerView.Adapter → बड़े datasets और high performance UI के लिए।
- CursorAdapter → जब data database (SQLite) से आता हो।
- SimpleAdapter → Key-value pair data show करने के लिए।
Adapter कैसे काम करता है?
Adapter data source से data लेकर हर item के लिए एक View create करता है और उसे UI component को provide करता है।
Example: ArrayAdapter with ListView
Step 1: activity_main.xml
<ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" />
Step 2: MainActivity.java
ListView listView = findViewById(R.id.listView); String[] names = {"Rahul", "Ankit", "Priya", "Neha"}; ArrayAdapter<String> adapter = new ArrayAdapter<>( this, android.R.layout.simple_list_item_1, names ); listView.setAdapter(adapter);
✅ Output: ListView में names का list दिखाई देगा।
Real Life Examples
- Contacts App → RecyclerView Adapter
- WhatsApp Chats → RecyclerView Adapter
- E-commerce Products List → Custom BaseAdapter या RecyclerView Adapter
Comments
Post a Comment