在Android應用中動態管理ItemList,通常需要使用適配器(Adapter)和列表視圖(ListView 或 RecyclerView)
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;
}
}
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;
}
}
ListView
或RecyclerView
: android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
ListView
或RecyclerView
: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);
}
}
// 添加一個新項
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
。