【问题标题】:first and last item of listView are samelistView 的第一项和最后一项相同
【发布时间】:2023-03-16 22:44:01
【问题描述】:

我想使用自定义 arrayadapter 显示列表,问题是我的第一个和最后一个项目是相同的,实际上我的 ArrayList 中有四个项目,但第四个项目没有显示,而是显示第一个项目,有时反之亦然。这里是我的代码,我是一个完整的初学者,所以不知道需要在这里发布哪些代码。

Place.java(列表项的自定义类)

/** Image resource ID for the Place */
private int mImageResourceId;
/**Description ID for the Place */
private  int mDescriptionId;
/**Name ID for the Place */
private  int mNameId;

/**
 * Create a new Word object.
 *
 * @param imageResourceId is the image resource ID for the image of place
 * @param nameId is the string resource Id for the name of place
 * @param descriptionId is the  string resource ID for the description associated with this place
 */
public Place(int imageResourceId, int nameId, int descriptionId) {
    mImageResourceId = imageResourceId;
    mNameId= nameId;
    mDescriptionId =descriptionId;
}

public int getImageResourceId (){return mImageResourceId;}

public int getDescriptionId(){return mDescriptionId;}

public  int getNameId() {return mNameId;}}

PlaceAdapter.java(我的 lsit 的自定义适配器)

public class PlaceAdapter extends ArrayAdapter<Place> {


public PlaceAdapter(Context context, ArrayList<Place> place) {
    super(context, 0, place);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);

        Place currentPlace = getItem(position);

        TextView name = (TextView) listItemView.findViewById(R.id.name);
        name.setText(currentPlace.getNameId());

        TextView description = (TextView) listItemView.findViewById(R.id.description);
        description.setText(currentPlace.getDescriptionId());

        ImageView image = (ImageView) listItemView.findViewById(R.id.image);
        image.setImageResource(currentPlace.getImageResourceId());

    }
    return listItemView;
}
}

HistoricFragment.java(片段包含一个列表视图)

public class HistoricFragment extends Fragment {

public HistoricFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.place_list, container, false);

    // Create a list of Place
    final ArrayList<Place> place = new ArrayList<Place>();
    place.add(new Place(R.drawable.badshahi_mosque, R.string.badshahi_name,
            R.string.badshahi_description));
    place.add(new Place(R.drawable.lahore_fort, R.string.fort_name,
            R.string.fort_description));
    place.add(new Place(R.drawable.sheesh_mahal_lahore_fort, R.string.mahal_name,
            R.string.mahal_description));
    place.add(new Place(R.drawable.wazir_mosque, R.string.wazir_khan_name,
            R.string.wazir_mosque_description));

    // Create an {@link PlaceAdapter}, whose data source is a list of {@link Place}s. The
    // adapter knows how to create list items for each item in the list.
    PlaceAdapter adapter = new PlaceAdapter(getActivity(), place);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // place_list.xml layout file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link PlaceAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Place} in the list.
    listView.setAdapter(adapter);

    return  rootView;
}
}

【问题讨论】:

    标签: android listview fragment custom-adapter listitem


    【解决方案1】:

    像这样更改你的代码

    public class PlaceAdapter extends ArrayAdapter<Place> {
    
    
        public PlaceAdapter(Context context, ArrayList<Place> place) {
            super(context, 0, place);
        }
    
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder;
            View listItemView;
            if (convertView == null) {
                listItemView = LayoutInflater.from(getContext()).inflate(
                        R.layout.list_item, parent, false);
    
                holder = new ViewHolder();
                holder.name = (TextView) listItemView.findViewById(R.id.name);
                holder.description = (TextView) listItemView.findViewById(R.id.description);
                holder.image = (ImageView) listItemView.findViewById(R.id.image);
    
                convertView.setTag(holder);
            }
    
            holder = (ViewHolder) convertView.getTag();
    
            Place currentPlace = getItem(position);
            holder.name.setText(currentPlace.getNameId());
            holder.description.setText(currentPlace.getDescriptionId());
            holder.image.setImageResource(currentPlace.getImageResourceId());
            return convertView;
        }
    
        public static class ViewHolder {
            TextView name;
            TextView description;
            ImageView image;
        }
    
    }
    

    【讨论】:

      【解决方案2】:
          @Override
          public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
              View listItemView = convertView;
              if (listItemView == null) {
                  listItemView = LayoutInflater.from(getContext()).inflate(
                      R.layout.list_item, parent, false);
              }
              Place currentPlace = getItem(position);
      
              TextView name = (TextView) listItemView.findViewById(R.id.name);
              name.setText(currentPlace.getNameId());
      
              TextView description = (TextView) listItemView.findViewById(R.id.description);
              description.setText(currentPlace.getDescriptionId());
      
              ImageView image = (ImageView) listItemView.findViewById(R.id.image);
              image.setImageResource(currentPlace.getImageResourceId());
              return listItemView;
      }
      

      这将解决问题。但是你可以谷歌并使用更有效的 Viewholder 模式。

      【讨论】:

        猜你喜欢
        • 2017-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-23
        相关资源
        最近更新 更多