The location of adapter extension baseadapter getview is 0

I want to use listview with a custom adapter that extends 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 parameter position is always 0 (I have 3 projects). I don't know why. I see other people have the same problem, but they didn't help me solve the problem. Any ideas? (PS: in this case, I don't want to use recyclerview.)

edit

Log.d("SIZE", Integer.toString(section.getPowerList().size())); // return 3
holder.mcharacteristics.setAdapter(new PowerAdapter(section.getPowerList()));

And layout: activities:

<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 recycle bin 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 the size of my listview is "wrap_content". For example, if I fix the size with "200dp", it can work. But I want dynamic size. Does anyone know how to fix this thing?

resolvent:

I don't think you should use listview in recyclerview. You can't use wrap for list view_ Content (it will be reduced to 0 height). Therefore, let's use stickyrecyclerview or something similar. Please refer to this example

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>