傳播數據,當我遇到困難我收到關於ListView控件的Android在ListView
假設我收到一個列表作爲分發數據我有
list.add("Drink", "Water", "Fresh Water", false)
list.add("Drink", "Cola", "8oZ", false)
list.add("Food", "Burger", "Homemade", false)
等等
現在,當我打電話適配器,該GetView方法被調用。我實現它:
public class EntryRestaurantAdapter extends ArrayAdapter<RestaurantMenu> {
private Context context;
private ArrayList<RestaurantMenu> items;
private LayoutInflater vi;
private static final int TYPE_SECTION = 1;
private static final int TYPE_ITEM = 2;
public EntryRestaurantAdapter(Context context,ArrayList<RestaurantMenu> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return items.get(position).getSection() ? TYPE_SECTION : TYPE_ITEM;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final RestaurantMenu i = items.get(position);
if (!i.getSection()){
RestaurantMenu si = (RestaurantMenu)i;
v = vi.inflate(R.layout.list_item_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getCategory());
for (int a=0; a<items.size(); a++){
// if (items.indexOf(i.getCategory()) == items.lastIndexOf(i.getCategory()))
// v = vi.inflate(R.layout.item_view, null);
if (i.getCategory().equals(items.get(a).getCategory())){
Log.d("Dentro del FOr ",""+ items.get(a).getSection());
items.get(a).setSection(true);
Log.d("Dentro del FOr despues del true ",""+ items.get(a).getSection());
}
}
}
else {
v = vi.inflate(R.layout.item_view, null);
TextView sectionView = (TextView) v.findViewById(R.id.restaurant_name);
RestaurantMenu si = (RestaurantMenu) i;
sectionView.setText(si.getFoodName());
}
Log.d("Entry Section: ", "" + i.getSection() + " ID: " + i.getCategory());
return v;
}
我在我的主要活動稱爲:
EntryRestaurantAdapter adapter = new EntryRestaurantAdapter(this, restaurantMenuList);
listview.setAdapter(adapter);
這工作,但它忽略了每個類別中的第一個。我認爲這是因爲getView方法對每個位置明顯運行ONCE。這會導致它將v作爲類別返回,而不是每個類別第一次嘗試的項目。我怎樣才能解決這個問題?
這是
類別輸出:喝 項目可口可樂(它省略了水項目視圖) 類別:食品(它省略漢堡的項目視圖)
你可以發佈整個適配器,還有你定義列表和適配器的方法嗎? –
當然可以。我將編輯帖子。 – Joseph
總共有多少行?總的來說,我的意思是項目+部分 –