You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求实现从外部数据库取数据并在ListView自动刷新展示的代码

Hey there! Let's walk through how to build this feature—fetching data from an external database, displaying it in a ListView, and keeping it updated automatically as the database changes. I'll use Android for the examples since ListView is a staple there, but the core concepts translate to other platforms too.

Core Components Needed

To make this work, we need three key parts:

  1. A way to fetch data from your external database
  2. A ListView adapter to bind and display the data
  3. A mechanism to refresh the ListView in real-time (either polling or push notifications)

1. Define Data Model & Database Access Layer

First, create a simple data model class to represent your database records, then a helper class to handle fetching data from the external source.

Data Model Class

public class Item {
    private String id;
    private String name;
    private String content;

    // Constructor, getters, and setters
    public Item(String id, String name, String content) {
        this.id = id;
        this.name = name;
        this.content = content;
    }

    public String getId() { return id; }
    public String getName() { return name; }
    public String getContent() { return content; }
}

Remote Database Helper

This class handles the actual data fetching—replace the mock logic with your real database call (like an API request via Retrofit, JDBC for direct DB access, etc.):

public class RemoteDatabaseHelper {
    // Mock method: Replace with your real data fetch logic
    public static List<Item> fetchLatestData() {
        List<Item> items = new ArrayList<>();
        // Example data—swap this with your database query result
        items.add(new Item("1", "First Item", "Updated content for item 1"));
        items.add(new Item("2", "Second Item", "New content added"));
        return items;
    }
}

2. Build the ListView Adapter

Create a custom ArrayAdapter to bind your data to the ListView rows. This adapter will also handle updating the data when a refresh happens.

Custom Adapter Code

public class ItemAdapter extends ArrayAdapter<Item> {
    private Context context;
    private List<Item> itemList;

    public ItemAdapter(Context context, List<Item> itemList) {
        super(context, R.layout.list_item, itemList);
        this.context = context;
        this.itemList = itemList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // Reuse views for efficiency
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
        }

        Item currentItem = itemList.get(position);
        // Bind data to views
        TextView tvName = convertView.findViewById(R.id.tv_name);
        TextView tvContent = convertView.findViewById(R.id.tv_content);

        tvName.setText(currentItem.getName());
        tvContent.setText(currentItem.getContent());

        return convertView;
    }

    // Method to update the adapter's data and refresh the ListView
    public void updateData(List<Item> newItems) {
        itemList.clear();
        itemList.addAll(newItems);
        notifyDataSetChanged(); // Tells the ListView to redraw with new data
    }
}

List Item Layout (res/layout/list_item.xml)

This is the layout for each row in the ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="@android:color/darker_gray"
        android:layout_marginTop="4dp"/>
</LinearLayout>

3. Implement Auto-Refresh Mechanism

Choose between two common approaches based on your use case:

Option 1: Polling (Simple, Good for Low-Frequency Changes)

Use a Handler to periodically fetch new data and update the ListView. This is easy to implement but uses more resources than push notifications.

Activity Code with Polling

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private ItemAdapter adapter;
    private Handler refreshHandler = new Handler(Looper.getMainLooper());
    private Runnable refreshTask;
    private static final long REFRESH_INTERVAL = 5000; // 5 seconds

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.list_view);
        // Initialize with initial data
        List<Item> initialItems = RemoteDatabaseHelper.fetchLatestData();
        adapter = new ItemAdapter(this, initialItems);
        listView.setAdapter(adapter);

        // Set up auto-refresh
        setupAutoRefresh();
    }

    private void setupAutoRefresh() {
        refreshTask = new Runnable() {
            @Override
            public void run() {
                // Fetch data in a background thread to avoid blocking the UI
                new Thread(() -> {
                    List<Item> latestItems = RemoteDatabaseHelper.fetchLatestData();
                    // Update UI on the main thread
                    runOnUiThread(() -> adapter.updateData(latestItems));
                }).start();
                // Schedule next refresh
                refreshHandler.postDelayed(this, REFRESH_INTERVAL);
            }
        };
        // Start the first refresh immediately
        refreshHandler.post(refreshTask);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Stop the refresh task to prevent memory leaks
        refreshHandler.removeCallbacks(refreshTask);
    }
}

Main Activity Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

Option 2: Push Notifications (Efficient, Good for Frequent Changes)

If your external database supports real-time push (like Firebase Realtime Database, MQTT, or WebSockets), use this approach to get notified instantly when data changes. Here's an example with Firebase:

// Initialize Firebase Database reference (replace with your path)
DatabaseReference itemsRef = FirebaseDatabase.getInstance().getReference("items");

// Listen for real-time data changes
itemsRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        List<Item> updatedItems = new ArrayList<>();
        // Map snapshot to your Item model
        for (DataSnapshot itemSnapshot : snapshot.getChildren()) {
            Item item = itemSnapshot.getValue(Item.class);
            updatedItems.add(item);
        }
        // Update the adapter
        adapter.updateData(updatedItems);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        // Handle errors (e.g., network issues)
        Toast.makeText(MainActivity.this, "Failed to load data", Toast.LENGTH_SHORT).show();
    }
});

Key Notes

  • Network Operations: Always fetch data in a background thread to avoid ANR errors.
  • Polling Interval: Don't set it too short—balance between freshness and battery/network usage.
  • Push vs Polling: Use push for frequent changes (like chat apps) and polling for low-frequency updates (like news feeds).
  • Memory Management: Always cancel background tasks when the activity is destroyed to prevent memory leaks.

内容的提问来源于stack exchange,提问作者Moses Wangira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:13:15