3

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 ?

Magnas
  • 869
  • 1
  • 5
  • 18
  • Are you sure you have 3 items in the list? have you debugged that? can you share your activity code where you are getting 3 items in the list and setting that to your adapter – Mukesh Rana Jan 09 '16 at 20:19
  • I've debugged this, but I'll put the code – Magnas Jan 10 '16 at 09:26
  • so you have ListView inside each RecyclerView Item and you are setting adapter for that ListView inside your RecyclerView Item? – Mukesh Rana Jan 10 '16 at 09:59
  • Exactly. I'm using listview mainly for not having to measure a nested recyclerview height (wrap_content) troubles etc. – Magnas Jan 10 '16 at 10:13

1 Answers1

3

I think you shouldn't use Listview inside a RecyclerView. You can't use wrap_content for a ListView (it will shrink to 0 height). So Let's use StickyRecyclerView or something like that. Please refer to this example

https://gist.github.com/gabrielemariotti/e81e126227f8a4bb339c

CodeMonster
  • 300
  • 5
  • 19
  • Thank you, with this and http://stackoverflow.com/a/29394173/3595639 I succeed to fix the trouble ! – Magnas Jan 11 '16 at 09:04