यह पृष्ठ पुर्तगाली से मशीन-अनुवादित है और इसमें त्रुटियाँ हो सकती हैं। पुर्तगाली संस्करण प्रामाणिक है।
8. डेटा लोड करना
आप क्या सीखेंगे: UI को रोके बिना डेटा लाना और बड़ी सूचियों को कुशलता से दिखाना। पूर्वापेक्षाएँ: स्टेट प्रबंधन।
संपूर्ण उदाहरण (कॉपी करें, पेस्ट करें और चलाएँ)
डेटा को एक पृष्ठभूमि Thread में लोड करता है, Component.onUI के साथ paint thread पर लौटता है और एक
वर्चुअलाइज़्ड सूची (VirtualListView + DataAdapter) में दिखाता है जो views को पुनर्चक्रित करती है। जैसा है वैसा ही compile होता है।
package com.exemplo;
import java.util.*;
import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;
public class MainWindow extends Window
{
private final List<Tarefa> tarefas = new ArrayList<>();
private VirtualListView lista;
@Override
public void onShow() throws Exception
{
super.onShow();
setLayout(new VerticalLayout());
lista = new VirtualListView(Relative.ofFont(3f), new DataAdapter()
{
public int getCount() { return tarefas.size(); }
public Component createView() { return new Label(""); } // एक बार बनाता है
public void bindView(Component v, int i) { ((Label) v).setText(tarefas.get(i).titulo); }
});
add(LAY_FILL, lista);
carregar();
}
private void carregar()
{
new Thread(() -> {
var dados = listarDoRepositorio(); // भारी काम, background thread
Component.onUI(() -> { // paint thread पर वापस
tarefas.clear();
tarefas.addAll(dados);
lista.notifyDataChanged(); // वर्चुअलाइज़्ड सूची अपडेट करता है
});
}).start();
}
private List<Tarefa> listarDoRepositorio() // I/O का अनुकरण करता है
{
List<Tarefa> out = new ArrayList<>();
for (int i = 1; i <= 500; i++) out.add(new Tarefa("Tarefa " + i));
return out;
}
}
class Tarefa
{
final String titulo;
Tarefa(String titulo) { this.titulo = titulo; }
}
नीचे के अंश केवल मूल भाग दिखाते हैं (जो
onShow()के भीतर जाता है)। चलाने के लिए, उन्हें ऊपर केMainWindowढाँचे में रखें।
I/O कभी paint thread पर क्यों नहीं होता
UI एक ही thread में चलती है — उसमें कोई भी अवरोध स्क्रीन को जमा देता है। अनिवार्य पैटर्न:
new Thread(() -> {
var dados = repo.listar(); // भारी काम, background thread
Component.onUI(() -> { // paint thread पर वापस
vm.tarefas = dados;
lista.notifyDataChanged(); // वर्चुअलाइज़्ड सूची अपडेट करता है
});
}).start();
पहले से पृष्ठभूमि में किसी UI इवेंट पर प्रतिक्रिया देने के लिए, onActionThread का उपयोग करें (देखें
इनपुट संभालना)।
बड़ी सूचियाँ: VirtualListView + DataAdapter
VirtualListView views को पुनर्चक्रित करता है — केवल दृश्यमान भाग को render करता है। adapter बनाना
(एक बार) को जोड़ना (सस्ता, कई बार) से अलग करता है:
VirtualListView lista = new VirtualListView(Relative.ofFont(3f), new DataAdapter() {
public int getCount() { return vm.tarefas.size(); }
public Component createView() throws Exception { return new CardTarefa(); }
public void bindView(Component v, int i) throws Exception { ((CardTarefa) v).bind(vm.tarefas.get(i)); }
});
add(LAY_FILL, lista);
// जब डेटा बदलता है:
lista.notifyDataChanged();
bindViewscroll के दौरान बहुत चलता है — इसे सस्ता और idempotent रखें (वहाँ कोई I/O नहीं)।
इस चरण में क्या बनाया जाता है
सूची अब पृष्ठभूमि में लोड होने और वर्चुअलाइज़्ड render होने लगती है (अध्याय 3 के CardTarefa का उपयोग करते हुए)।
पुनरावलोकन
Thread में I/O → Component.onUI; बड़ी सूचियाँ VirtualListView के साथ (createView/bindView द्वारा
पुनर्चक्रण); डेटा बदलने पर notifyDataChanged()।