亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

如何在Android應用中動態管理ItemList

小樊
85
2024-09-08 11:51:46
欄目: 編程語言

在Android應用中動態管理ItemList,通常需要使用適配器(Adapter)和列表視圖(ListView 或 RecyclerView)

  1. 創建一個數據模型類(Model Class):
public class Item {
    private String title;
    private String description;

    public Item(String title, String description) {
        this.title = title;
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
  1. 創建一個自定義適配器(Custom Adapter):
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class CustomAdapter extends BaseAdapter {
    private Context context;
    private List<Item> itemList;

    public CustomAdapter(Context context, List<Item> itemList) {
        this.context = context;
        this.itemList = itemList;
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, parent, false);
        }

        TextView titleTextView = convertView.findViewById(R.id.title);
        TextView descriptionTextView = convertView.findViewById(R.id.description);

        titleTextView.setText(itemList.get(position).getTitle());
        descriptionTextView.setText(itemList.get(position).getDescription());

        return convertView;
    }
}
  1. 在布局文件中添加ListViewRecyclerView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
  1. 在Activity或Fragment中初始化適配器并設置給ListViewRecyclerView
import android.os.Bundle;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private CustomAdapter adapter;
    private List<Item> itemList;

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

        listView = findViewById(R.id.listView);

        itemList = new ArrayList<>();
        itemList.add(new Item("Item 1", "This is item 1"));
        itemList.add(new Item("Item 2", "This is item 2"));
        itemList.add(new Item("Item 3", "This is item 3"));

        adapter = new CustomAdapter(this, itemList);
        listView.setAdapter(adapter);
    }
}
  1. 動態添加、刪除或更新列表項:
// 添加一個新項
Item newItem = new Item("New Item", "This is a new item");
itemList.add(newItem);
adapter.notifyDataSetChanged();

// 刪除一個項
itemList.remove(0);
adapter.notifyDataSetChanged();

// 更新一個項
Item updatedItem = itemList.get(0);
updatedItem.setTitle("Updated Title");
updatedItem.setDescription("Updated Description");
adapter.notifyDataSetChanged();

這樣,你就可以在Android應用中動態管理ItemList了。請根據實際情況調整代碼,例如使用RecyclerView替換ListView

0
体育| 宝兴县| 游戏| 富平县| 郧西县| 建平县| 远安县| 清远市| 陈巴尔虎旗| 孝感市| 彰武县| 塘沽区| 扶绥县| 平顶山市| 抚顺市| 铁岭市| 甘谷县| 霍山县| 探索| 尉氏县| 英吉沙县| 东安县| 龙里县| 图们市| 澳门| 遂溪县| 渭源县| 密云县| 徐汇区| 灵川县| 本溪| 崇文区| 兰考县| 章丘市| 长汀县| 通山县| 浮山县| 禹城市| 观塘区| 平昌县| 长丰县|