Theory
ચાલીસ events, એક screen
FestConnect Mobile (તમારું BCA305-02 app) એ handful of events ને LinearLayout માં show કર્યું, દરેક TextView: fine 5 માટે, absurd આ year ના 40 માટે. 40 TextViews ને hard-code કરવું option નથી, અને next year ની list anyway differ થશે.
શું screen ને જોઈએ છે તે widget છે જે data list ને લે છે અને rows ને manufactures કરે છે on demand, scrolling included. Android નો classic answer, અને syllabus નો, એ ListView છે, અને interesting part એ middleman છે જે તે employ કરે છે.
Theory
thali server
ListView એ canteen counter છે fixed number ઓફ thalis (row views) સાથે: ફક્ત જેટલા screen પર fit થાય.
adapter એ server છે જે kitchen (તમારો data array) અને counter ની વચ્ચે stands કરે છે: દરેક position માટે, તે thali ને લે છે, તે item ના food થી તેને fills કરે છે, અને hand over કરે છે. Scroll, અને thali જે top થી slid off થઈ તેને wipe અને REFILLED કરવામાં આવે છે row માટે જે નીચે enter થાય છે: કોઈ 10000 items માટે 10000 plates ને cook નથી કરતું.
Theory
Simple ListView, formally
ત્રણ pieces plain version ને wire કરે છે:
- તમારા layout XML માં ListView
- તમારું data: String array અથવા ArrayList
- ArrayAdapter જે તેમને connect કરે છે, given built-in row layout:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, events);
listView.setAdapter(adapter);
simple_list_item_1 એ Android નો stock one-TextView row છે. Taps position દ્વારા arrive થાય છે: setOnItemClickListener((parent, view, position, id) -> ...): position તમારા data array ને directly index કરે છે.
Practical
event list, simple form
public class EventListActivity extends AppCompatActivity {
String[] events = { "Garba Night", "Coding Contest", "Robo Race" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_list);
ListView list = findViewById(R.id.eventList);
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, events);
list.setAdapter(adapter);
list.setOnItemClickListener((parent, view, position, id) ->
Toast.makeText(this, events[position] + " selected",
Toast.LENGTH_SHORT).show());
}
}
Theory
Custom ListView: તમારું own row
committee દરેક row ને show કરવા માંગે છે name, venue AND seats left: stock one-liner row તેને cannot કરે છે. custom ListView = 2 additions:
- row layout XML જે તમે design કરો (event_row.xml: બે TextViews અને ImageView)
- custom adapter: ArrayAdapter ને extend કરો અને getView(position, convertView, parent) ને override કરો: તમારા row layout ને inflate કરો, તેના pieces ને findViewById કરો, તેમને
positionપર data થી fill કરો, row ને return કરો.
getView એ thali-filling moment છે: તે runs થાય છે once per VISIBLE row, અને ફરીથી દરેક વાર જ્યારે row scroll થઈને view માં આવે.
Practical
custom adapter નું heart: getView recycling સાથે
public class EventAdapter extends ArrayAdapter<Event> {
public EventAdapter(Context c, ArrayList<Event> events) {
super(c, 0, events);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) { // કોઈ recycled thali નથી?
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.event_row, parent, false);
}
Event e = getItem(position); // આ row નું data
TextView name = convertView.findViewById(R.id.rowName);
TextView seats = convertView.findViewById(R.id.rowSeats);
name.setText(e.name);
seats.setText(e.seatsLeft + " seats");
return convertView; // the filled row
}
}
Quiz
getView માં, convertView parameter શું છે, અને શા માટે Android તેને તમને pass કરે છે?
- row જે user એ last tap કર્યું, highlighting માટે
- recycled row view જે screen થી scrolled off થયું, offered for REUSE જેથી તમે inflate કરો ફક્ત જ્યારે તે null હોય
- parent ListView itself
- layout XML ની copy View તરીકે, freshly inflated દરેક call
Show the answer
recycled row view જે screen થી scrolled off થયું, offered for REUSE જેથી તમે inflate કરો ફક્ત જ્યારે તે null હોય
convertView એ wiped thali છે: row object જે screen થી slid off થયું, handed back જેથી તમે તેને refill કરી શકો instead of inflating new one: inflation (parsing XML into views) expensive છે, અને recycling એ શા માટે 10000-row list smoothly scroll થાય છે ફક્ત dozen row objects સાથે alive. canonical pattern એ exactly listing નું છે: inflate if null, else reuse. Option D describe કરે છે શું તમારું code wastefully કરે WITHOUT check: classic laggy-list bug. Options A અને C parameter ને entirely mislabel કરે છે: taps click listener દ્વારા arrive થાય છે, અને parent એ separate argument છે.
Think first
Simple અથવા custom? 3 lists પર Rule
ત્રણ FestConnect screens: (a) plain list of department names filter માટે, (b) events list name + venue + seats + photo સાથે, (c) rules ની list, એક sentence દરેક. Decide કરો simple ArrayAdapter અથવા custom adapter દરેક માટે, tap કરતા પહેલાં.
Show the answer
(a) અને (c): simple ArrayAdapter simple_list_item_1 સાથે: એક string per row એ exactly તેનું shape છે, અને custom adapter ને write કરવું તેના માટે ceremony છે without benefit. (b): custom adapter: multiple fields અને image per row ને તમારા own row layout અને getView ની જરૂર છે. decision rule: એક string per row = stock; કંઈ પણ richer = custom. કહેવું WHY (row layout નું shape drives adapter choice) એ શું answer ને correct થી full-marks સુધી upgrade કરે છે.
Watch out
ListView traps
Inflating દરેક call: convertView null-check ને skip કરવું works, પછી stutters real data પર: THE performance bug જે examiners describe કરે છે અને તમને spot કરવા માંગે છે.
setAdapter ને Forgetting કરવું: ListView without adapter એ silently blank છે: કોઈ error નહીં, કોઈ rows નહીં.
notifyDataSetChanged: data list ને changing કર્યા પછી, adapter.notifyDataSetChanged() ને call કરો અથવા screen stale rows ને show કરતી રહે છે: adapter તમારા ArrayList ને watch નથી કરતો; તમારે તેને ring કરવું પડે.
Theory
adapter idea ListView ને outlive કરે છે
shape ને hold કરો: data + row template + function જે એક row ને fill કરે છે position દ્વારા. Android નું modern RecyclerView એ same idea છે recycling સાથે made compulsory, અને Unit 5 નું Flutter ListView.builder(itemCount, itemBuilder) એ it again છે Dart માં: itemBuilder IS getView ને nicer clothes સાથે. middleman ને once learn કરો અને દરેક list framework જે તમે ever meet કરો તે dialect છે. next lesson: widgets જે dates ને pick કરે છે અને progress ને show કરે છે: DatePicker, TimePicker, ProgressBar.
Summary
Key takeaways
- ListView scrollable rows ને render કરે છે; ADAPTER data ને rows સાથે bridge કરે છે.
- Simple form: ArrayAdapter સાથે android.R.layout.simple_list_item_1 એક string per row માટે.
- Custom form: તમારું own row XML + adapter જે getView(position, convertView, parent) ને override કરે છે.
- Recycle: inflate ફક્ત જ્યારે convertView null હોય, else refill: smooth-scroll secret.
- Row taps position સાથે arrive થાય છે setOnItemClickListener દ્વારા; position તમારા data ને index કરે છે.
- Data changed? adapter.notifyDataSetChanged() અથવા screen stale રહે છે.
- Memory hook: few thalis, one server, endless queue.