这是我第一次使用自定义布局构建 Listview,所以如果我遗漏了一些明显的东西,请指出。
我遇到的问题是我无法让列表视图在 Oncreate(); 之后使用新信息更新自身;已经用过。所以这个列表是非常静态的。
我正在尝试创建一个看起来像这样的自定义列表视图适配器:
public class MainListCustomBaseAdapter extends BaseAdapter {
static ArrayList<ListItems> DataSomething;
static Context Cont;
public MainListCustomBaseAdapter (ArrayList<ListItems> data, Context c){
DataSomething = data;
Cont = c;
}
public int getCount() {
// TODO Auto-generated method stub
return DataSomething.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return DataSomething.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)Cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainlistlayout, null);
}
ImageView image = (ImageView) v.findViewById(R.id.ListImage);
TextView titleView = (TextView)v.findViewById(R.id.title);
TextView DetailItemView = (TextView)v.findViewById(R.id.DetailItem);
ListItems msg = DataSomething.get(position);
image.setImageResource(msg.icon);
titleView.setText(msg.title);
DetailItemView.setText("ItemDetails: "+msg.ItemDetails);
return v;
}
public void updateResults(ArrayList<MainListCustomBaseAdapter> results){
notifyDataSetChanged();
}
}
我的 Oncreate 看起来像这样:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecipeList = (ListView) findViewById(R.id.mainListView);
ShoppingItems = new ArrayList<ListItems>();
RecipeList.setAdapter(new MainListCustomBaseAdapter(ShoppingItems, this));
ListItems Detail;
Detail = new ListItems();
Detail.setIcon(R.drawable.food);
Detail.setName("Food Stuff");
Detail.setItemDetails("ItemDetailsComp");
ShoppingItems.add(Detail);
}
和 listitem 看起来像这样:
public class ListItems {
public int icon ;
public String title;
public String ItemDetails;
public String getName() {
return title;
}
public void setName(String from) {
this.title = from;
}
public String getItemDetails() {
return ItemDetails;
}
public void setItemDetails(String ItemDetailsComp) {
this.ItemDetails = ItemDetailsComp;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
}
如何让列表视图动态更新?可能有 SetInvalidatedViews() 或 notifyDatasetchanged()?
任何帮助都深表感谢。