Привет, я работаю с приложением для Android, когда мне нужно проанализировать массив в представлении списка. Я создал специальный адаптер для этого, но мне здесь чего-то не хватает.
public class IndustryAdapter extends ArrayAdapter<Industry> {
Context context;
int resource;
public IndustryAdapter(Context context, ArrayList<Industry> industries) {
super(context, industries);
}
@Override
public View getView(View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
}
TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);
return convertView;
}
}
On the super(context, industries) is showing an error for industries:
required: 'int'
error: incompatible types: ArrayList<Industry> cannot be converted to int
2 ответа
Вы должны просто изменить свой вызов супер конструктора. Потому что он получит ресурс макета, который вы будете раздувать. Как это:
public IndustryAdapter(Context context, ArrayList<Industry> industries) {
super(context, android.R.layout.simple_list_item_1);
}
Используйте базовый адаптер для создания собственного адаптера для вашего списка
class CustomAdapter extends BaseAdapter {
private ArrayList<Industry> industries;
private Context context;
public CustomAdapter(ArrayList<Industry> industries, Context context) {
this.industries = industries;
this.context = context;
}
@Override
public int getCount() {
return industries.size();
}
@Override
public Object getItem(int position) {
return industries.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.your_layout, parent, false);
}
TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);
return convertView;
}
}
Новые вопросы
android
Android - это мобильная операционная система Google, используемая для программирования или разработки цифровых устройств (смартфоны, планшеты, автомобили, телевизоры, одежда, стекло, IoT). Для тем, связанных с Android, используйте специальные теги Android, такие как android-intent, android-activity, android-адаптер и т. Д. Для вопросов, не связанных с разработкой или программированием, но связанных с платформой Android, используйте эту ссылку: https: // android.stackexchange.com .