I want to use a listview with a custom adapter extending BaseAdapter
public class PowerAdapter extends BaseAdapter {
List<Power> mPowerList;
public PowerAdapter(List<Power> powers) {
mPowerList = powers;
}
public void bind(ViewHolder holder, int position) {
Power power = mPowerList.get(position);
Context context = holder.mContext;
holder.mPowerName.setText(power.getName());
holder.mPowerLevel.setText(context.getString(R.string.power_level, power.getMinLevel()));
holder.mPowerCost.setText(context.getString(R.string.power_cost, power.getCost()));
holder.mPowerDuration.setText(context.getString(R.string.power_duration, power.getDuration()));
holder.mPowerCooldown.setText(context.getString(R.string.power_cooldown, power.getCoolDown()));
}
@Override
public int getCount() {
return mPowerList.size();
}
@Override
public Object getItem(int position) {
return mPowerList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_power, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
bind(viewHolder, position);
return convertView;
}
public static class ViewHolder {
@Bind(R.id.power_name) TextView mPowerName;
@Bind(R.id.power_level) TextView mPowerLevel;
@Bind(R.id.power_cost) TextView mPowerCost;
@Bind(R.id.power_duration) TextView mPowerDuration;
@Bind(R.id.power_cooldown) TextView mPowerCooldown;
Context mContext;
public ViewHolder(View itemView) {
ButterKnife.bind(this, itemView);
mContext = itemView.getContext();
}
}}
in getView(...) the param position is always 0 (I have 3 items) and I can't figure why at all. I saw a couple of thread about other people having the same issue but they didn't help me to fix this. Any idea ? (P.S : I don't want to use RecyclerView in this case.)
EDIT
Log.d("SIZE", Integer.toString(section.getPowerList().size())); // return 3
holder.mCharacteristics.setAdapter(new PowerAdapter(section.getPowerList()));
and the layout : The activity :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<include layout="@layout/widget_toolbar"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/hero_model"
android:layout_width="match_parent"
android:layout_height="wrap_content" /></LinearLayout>
and the layout of each Recycler's item :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/section_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="16dp" />
<ListView
android:id="@+id/charac_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/></LinearLayout>
EDIT 2 Okey, it doesn't work because my ListView has "wrap_content" size, if I fix the size with "200dp" for example, it works. But I want dynamic size. Anyone know how to fix this ?